質問編集履歴
4
追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -205,4 +205,51 @@
|
|
205
205
|
Console.WriteLine(e2);
|
206
206
|
return false;
|
207
207
|
}
|
208
|
+
```
|
209
|
+
|
210
|
+
##追記3
|
211
|
+
方法2
|
212
|
+
```
|
213
|
+
static class Program
|
214
|
+
{
|
215
|
+
[STAThread]
|
216
|
+
static void Main()
|
217
|
+
{
|
218
|
+
|
219
|
+
|
220
|
+
string filepath = @"C:\WINDOWS\system32\cmd.exe";
|
221
|
+
FileSecurity fs;
|
222
|
+
|
223
|
+
fs = File.GetAccessControl(filepath);
|
224
|
+
|
225
|
+
var takeOwnerShip = new Privirage(PrivirageNames.SeTakeOwnershipPrivilege);
|
226
|
+
takeOwnerShip.Enable();
|
227
|
+
ProcessStartInfo psi = new ProcessStartInfo("whoami.exe", "/priv")
|
228
|
+
{
|
229
|
+
UseShellExecute = false
|
230
|
+
};
|
231
|
+
using (Process pprocess = Process.Start(psi))
|
232
|
+
{
|
233
|
+
pprocess.WaitForExit();
|
234
|
+
}
|
235
|
+
|
236
|
+
var ntAccount = new NTAccount("BUILTIN", "Administrators");
|
237
|
+
fs.SetOwner(ntAccount);
|
238
|
+
|
239
|
+
|
240
|
+
File.SetAccessControl(filepath, fs);
|
241
|
+
|
242
|
+
takeOwnerShip.Revert();
|
243
|
+
|
244
|
+
psi = new ProcessStartInfo("whoami.exe", "/priv")
|
245
|
+
{
|
246
|
+
UseShellExecute = false
|
247
|
+
};
|
248
|
+
using (Process pprocess = Process.Start(psi))
|
249
|
+
{
|
250
|
+
pprocess.WaitForExit();
|
251
|
+
}
|
252
|
+
Console.ReadKey();
|
253
|
+
}
|
254
|
+
}
|
208
255
|
```
|
3
追記
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
#実現したいこと
|
1
2
|
ファイルの所有者を変更して、アクセス権を変更しようと思っています。
|
2
3
|
現在のユーザーアカウントのファイルの所有権をAdministratorsに変更し、ファイルのアクセス権を変更することができました。
|
3
4
|
|
@@ -98,4 +99,110 @@
|
|
98
99
|
|
99
100
|
##環境
|
100
101
|
windows10 pro 1803
|
101
|
-
visualstudio 2017
|
102
|
+
visualstudio 2017
|
103
|
+
|
104
|
+
##追記2
|
105
|
+
System.Reflectionを使用して実装しました。
|
106
|
+
例外を投げずに処理ができたのですが、
|
107
|
+
ファイルのアクセス権および所有者は変更されませんでした。
|
108
|
+
下記のサイトを参考にしました。
|
109
|
+
https://stackoverflow.com/questions/5528888/how-to-enable-the-secreateglobalprivilege-in-net-without-resorting-to-p-invoke?lq=1
|
110
|
+
|
111
|
+
```
|
112
|
+
public static bool DenyFileAccess(string filepath)
|
113
|
+
{
|
114
|
+
|
115
|
+
FileSecurity fs;
|
116
|
+
|
117
|
+
fs = File.GetAccessControl(filepath);
|
118
|
+
|
119
|
+
|
120
|
+
Type privilegeType = Type.GetType("System.Security.AccessControl.Privilege");
|
121
|
+
object privilege;
|
122
|
+
|
123
|
+
bool ownerChanged = false;
|
124
|
+
|
125
|
+
try
|
126
|
+
{
|
127
|
+
|
128
|
+
privilege = Activator.CreateInstance(privilegeType, "SeTakeOwnershipPrivilege");
|
129
|
+
|
130
|
+
// => privilege.Enable();
|
131
|
+
privilegeType.GetMethod("Enable").Invoke(privilege, null);
|
132
|
+
|
133
|
+
var ntAccount = new NTAccount("BUILTIN", "Administrators");
|
134
|
+
fs.SetOwner(ntAccount);
|
135
|
+
|
136
|
+
File.SetAccessControl(filepath, fs);
|
137
|
+
|
138
|
+
|
139
|
+
privilegeType.GetMethod("Revert").Invoke(privilege, null);
|
140
|
+
|
141
|
+
ownerChanged = true;
|
142
|
+
}
|
143
|
+
catch
|
144
|
+
{
|
145
|
+
|
146
|
+
MessageBox.Show("error");
|
147
|
+
|
148
|
+
}
|
149
|
+
|
150
|
+
if (!ownerChanged) return false;
|
151
|
+
|
152
|
+
try
|
153
|
+
{
|
154
|
+
|
155
|
+
|
156
|
+
File.SetAccessControl(filepath, fs);
|
157
|
+
}
|
158
|
+
catch (InvalidOperationException ex)
|
159
|
+
{
|
160
|
+
|
161
|
+
MessageBox.Show("error");
|
162
|
+
return false;
|
163
|
+
}
|
164
|
+
catch (System.UnauthorizedAccessException e3)
|
165
|
+
{
|
166
|
+
MessageBox.Show("error");
|
167
|
+
}
|
168
|
+
|
169
|
+
try
|
170
|
+
{
|
171
|
+
|
172
|
+
|
173
|
+
Debug.WriteLine("Adding access control entry for "
|
174
|
+
+ filepath);
|
175
|
+
|
176
|
+
//管理者権限で起動していても、デスクトップユーザーのアカウント名が返されます。nuget->Cassia
|
177
|
+
ITerminalServicesManager manager = new TerminalServicesManager();
|
178
|
+
ITerminalServicesSession session = manager.CurrentSession;
|
179
|
+
string username = session.UserName;
|
180
|
+
|
181
|
+
// Add the access control entry to the file.
|
182
|
+
string principal = string.Format(@"{0}\{1}", System.Environment.MachineName, username);
|
183
|
+
AddFileSecurity(filepath, principal,
|
184
|
+
FileSystemRights.ReadAndExecute, AccessControlType.Deny);
|
185
|
+
AddFileSecurity(filepath, principal,
|
186
|
+
FileSystemRights.Read, AccessControlType.Deny);
|
187
|
+
|
188
|
+
//Console.WriteLine("Removing access control entry from "
|
189
|
+
// + filepath);
|
190
|
+
|
191
|
+
//// Remove the access control entry from the file.
|
192
|
+
//RemoveFileSecurity(filepath, @"BUILTIN\Administrators",
|
193
|
+
// FileSystemRights.ReadData, AccessControlType.Allow);
|
194
|
+
|
195
|
+
Console.WriteLine("Done.");
|
196
|
+
}
|
197
|
+
|
198
|
+
catch (System.UnauthorizedAccessException e3)
|
199
|
+
{
|
200
|
+
MessageBox.Show("error");
|
201
|
+
}
|
202
|
+
|
203
|
+
catch (Exception e2)
|
204
|
+
{
|
205
|
+
Console.WriteLine(e2);
|
206
|
+
return false;
|
207
|
+
}
|
208
|
+
```
|
2
題名
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
internal sealed class
|
1
|
+
internal sealed class にアクセスできない
|
body
CHANGED
File without changes
|
1
a
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
しかし、ファイルの所有者がTrustedInstallerの場合はうまくいきませんでしたので
|
5
5
|
visualstudioは管理者権限で起動しています。
|
6
6
|
ネットで検索してみると、
|
7
|
-
[ファイルの所有者がTrusted Installerの場合はPrivelegeのクラスを用意しなければならないらしいので](https://stackoverflow.com/questions/12999272/take-ownership-of-a-file-c-sharp)、以下のように実装しました。(ファイルの
|
7
|
+
[ファイルの所有者がTrusted Installerの場合はPrivelegeのクラスを用意しなければならないらしいので](https://stackoverflow.com/questions/12999272/take-ownership-of-a-file-c-sharp)、以下のように実装しました。(ファイルの所有者を変更するところまで記載しています。)
|
8
8
|
|
9
9
|
```
|
10
10
|
|