質問編集履歴

4

追加

2019/07/27 07:11

投稿

mercurian-teto
mercurian-teto

スコア75

test CHANGED
File without changes
test CHANGED
@@ -413,3 +413,97 @@
413
413
  }
414
414
 
415
415
  ```
416
+
417
+
418
+
419
+ ##追記3
420
+
421
+ 方法2
422
+
423
+ ```
424
+
425
+ static class Program
426
+
427
+ {
428
+
429
+ [STAThread]
430
+
431
+ static void Main()
432
+
433
+ {
434
+
435
+
436
+
437
+
438
+
439
+ string filepath = @"C:\WINDOWS\system32\cmd.exe";
440
+
441
+ FileSecurity fs;
442
+
443
+
444
+
445
+ fs = File.GetAccessControl(filepath);
446
+
447
+
448
+
449
+ var takeOwnerShip = new Privirage(PrivirageNames.SeTakeOwnershipPrivilege);
450
+
451
+ takeOwnerShip.Enable();
452
+
453
+ ProcessStartInfo psi = new ProcessStartInfo("whoami.exe", "/priv")
454
+
455
+ {
456
+
457
+ UseShellExecute = false
458
+
459
+ };
460
+
461
+ using (Process pprocess = Process.Start(psi))
462
+
463
+ {
464
+
465
+ pprocess.WaitForExit();
466
+
467
+ }
468
+
469
+
470
+
471
+ var ntAccount = new NTAccount("BUILTIN", "Administrators");
472
+
473
+ fs.SetOwner(ntAccount);
474
+
475
+
476
+
477
+
478
+
479
+ File.SetAccessControl(filepath, fs);
480
+
481
+
482
+
483
+ takeOwnerShip.Revert();
484
+
485
+
486
+
487
+ psi = new ProcessStartInfo("whoami.exe", "/priv")
488
+
489
+ {
490
+
491
+ UseShellExecute = false
492
+
493
+ };
494
+
495
+ using (Process pprocess = Process.Start(psi))
496
+
497
+ {
498
+
499
+ pprocess.WaitForExit();
500
+
501
+ }
502
+
503
+ Console.ReadKey();
504
+
505
+ }
506
+
507
+ }
508
+
509
+ ```

3

追記

2019/07/27 07:11

投稿

mercurian-teto
mercurian-teto

スコア75

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,5 @@
1
+ #実現したいこと
2
+
1
3
  ファイルの所有者を変更して、アクセス権を変更しようと思っています。
2
4
 
3
5
  現在のユーザーアカウントのファイルの所有権をAdministratorsに変更し、ファイルのアクセス権を変更することができました。
@@ -199,3 +201,215 @@
199
201
  windows10 pro 1803
200
202
 
201
203
  visualstudio 2017
204
+
205
+
206
+
207
+ ##追記2
208
+
209
+ System.Reflectionを使用して実装しました。
210
+
211
+ 例外を投げずに処理ができたのですが、
212
+
213
+ ファイルのアクセス権および所有者は変更されませんでした。
214
+
215
+ 下記のサイトを参考にしました。
216
+
217
+ https://stackoverflow.com/questions/5528888/how-to-enable-the-secreateglobalprivilege-in-net-without-resorting-to-p-invoke?lq=1
218
+
219
+
220
+
221
+ ```
222
+
223
+ public static bool DenyFileAccess(string filepath)
224
+
225
+ {
226
+
227
+
228
+
229
+ FileSecurity fs;
230
+
231
+
232
+
233
+ fs = File.GetAccessControl(filepath);
234
+
235
+
236
+
237
+
238
+
239
+ Type privilegeType = Type.GetType("System.Security.AccessControl.Privilege");
240
+
241
+ object privilege;
242
+
243
+
244
+
245
+ bool ownerChanged = false;
246
+
247
+
248
+
249
+ try
250
+
251
+ {
252
+
253
+
254
+
255
+ privilege = Activator.CreateInstance(privilegeType, "SeTakeOwnershipPrivilege");
256
+
257
+
258
+
259
+ // => privilege.Enable();
260
+
261
+ privilegeType.GetMethod("Enable").Invoke(privilege, null);
262
+
263
+
264
+
265
+ var ntAccount = new NTAccount("BUILTIN", "Administrators");
266
+
267
+ fs.SetOwner(ntAccount);
268
+
269
+
270
+
271
+ File.SetAccessControl(filepath, fs);
272
+
273
+
274
+
275
+
276
+
277
+ privilegeType.GetMethod("Revert").Invoke(privilege, null);
278
+
279
+
280
+
281
+ ownerChanged = true;
282
+
283
+ }
284
+
285
+ catch
286
+
287
+ {
288
+
289
+
290
+
291
+ MessageBox.Show("error");
292
+
293
+
294
+
295
+ }
296
+
297
+
298
+
299
+ if (!ownerChanged) return false;
300
+
301
+
302
+
303
+ try
304
+
305
+ {
306
+
307
+
308
+
309
+
310
+
311
+ File.SetAccessControl(filepath, fs);
312
+
313
+ }
314
+
315
+ catch (InvalidOperationException ex)
316
+
317
+ {
318
+
319
+
320
+
321
+ MessageBox.Show("error");
322
+
323
+ return false;
324
+
325
+ }
326
+
327
+ catch (System.UnauthorizedAccessException e3)
328
+
329
+ {
330
+
331
+ MessageBox.Show("error");
332
+
333
+ }
334
+
335
+
336
+
337
+ try
338
+
339
+ {
340
+
341
+
342
+
343
+
344
+
345
+ Debug.WriteLine("Adding access control entry for "
346
+
347
+ + filepath);
348
+
349
+
350
+
351
+ //管理者権限で起動していても、デスクトップユーザーのアカウント名が返されます。nuget->Cassia
352
+
353
+ ITerminalServicesManager manager = new TerminalServicesManager();
354
+
355
+ ITerminalServicesSession session = manager.CurrentSession;
356
+
357
+ string username = session.UserName;
358
+
359
+
360
+
361
+ // Add the access control entry to the file.
362
+
363
+ string principal = string.Format(@"{0}\{1}", System.Environment.MachineName, username);
364
+
365
+ AddFileSecurity(filepath, principal,
366
+
367
+ FileSystemRights.ReadAndExecute, AccessControlType.Deny);
368
+
369
+ AddFileSecurity(filepath, principal,
370
+
371
+ FileSystemRights.Read, AccessControlType.Deny);
372
+
373
+
374
+
375
+ //Console.WriteLine("Removing access control entry from "
376
+
377
+ // + filepath);
378
+
379
+
380
+
381
+ //// Remove the access control entry from the file.
382
+
383
+ //RemoveFileSecurity(filepath, @"BUILTIN\Administrators",
384
+
385
+ // FileSystemRights.ReadData, AccessControlType.Allow);
386
+
387
+
388
+
389
+ Console.WriteLine("Done.");
390
+
391
+ }
392
+
393
+
394
+
395
+ catch (System.UnauthorizedAccessException e3)
396
+
397
+ {
398
+
399
+ MessageBox.Show("error");
400
+
401
+ }
402
+
403
+
404
+
405
+ catch (Exception e2)
406
+
407
+ {
408
+
409
+ Console.WriteLine(e2);
410
+
411
+ return false;
412
+
413
+ }
414
+
415
+ ```

2

題名

2019/07/26 15:50

投稿

mercurian-teto
mercurian-teto

スコア75

test CHANGED
@@ -1 +1 @@
1
- internal sealed class privilegeにアクセスできない(デフォルトでTrustedInstallerが所有権であるファイルの所有権を変更)
1
+ internal sealed class にアクセスできない
test CHANGED
File without changes

1

a

2019/07/26 10:24

投稿

mercurian-teto
mercurian-teto

スコア75

test CHANGED
File without changes
test CHANGED
@@ -10,7 +10,7 @@
10
10
 
11
11
  ネットで検索してみると、
12
12
 
13
- [ファイルの所有者がTrusted Installerの場合はPrivelegeのクラスを用意しなければならないらしいので](https://stackoverflow.com/questions/12999272/take-ownership-of-a-file-c-sharp)、以下のように実装しました。(ファイルの初秋社を変更するところまで記載しています。)
13
+ [ファイルの所有者がTrusted Installerの場合はPrivelegeのクラスを用意しなければならないらしいので](https://stackoverflow.com/questions/12999272/take-ownership-of-a-file-c-sharp)、以下のように実装しました。(ファイルの所有者を変更するところまで記載しています。)
14
14
 
15
15
 
16
16