-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathForm3.cs
More file actions
202 lines (191 loc) · 12.5 KB
/
Form3.cs
File metadata and controls
202 lines (191 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.IO;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Security.Principal;
namespace MBST_Lab_1
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
[DllImport("C:\\Users\\dzaga\\Desktop\\MBST_Lab_1\\IntegrityLevel.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
public static extern bool SetFileIntegrityLevel(int level, string FileName);
private void LowSetButton_Click(object sender, EventArgs e)
{
SetFileIntegrityLevel(0, this.Tag.ToString());
}
private void MediumSetButton_Click(object sender, EventArgs e)
{
SetFileIntegrityLevel(1, this.Tag.ToString());
}
private void HighSetButton_Click(object sender, EventArgs e)
{
SetFileIntegrityLevel(2, this.Tag.ToString());
}
//при запуске формы аккаунт владельца файла запишем в текстбокс аккаунта
private void set_account(object sender, EventArgs e)
{
//получим текущий аккаунт
var File_Security = File.GetAccessControl(this.Tag.ToString());//аргумент - путь к файлу
var SID = File_Security.GetOwner(typeof(SecurityIdentifier));
var Owner = SID.Translate(typeof(NTAccount));
account.Text = Owner.ToString();
//ПАРАША
var fileInfo = new FileInfo(this.Tag.ToString());
// get the ACL of the directory
var fileSec = fileInfo.GetAccessControl();
// remove inheritance, copying all entries so that they are direct ACEs
fileSec.SetAccessRuleProtection(true, true);
// do the operation on the directory
fileInfo.SetAccessControl(fileSec);
}
//ADD ACL (аккаунт из текстбокса аккаунта)
private void Button1_Click(object sender, EventArgs e)
{
//добавляем запись
if (acl.Text == "AppendData")
AddFileSecurity(this.Tag.ToString(), account.Text/*@"BUILTIN\Пользователи"*/, FileSystemRights.AppendData, AccessControlType.Allow);
else if (acl.Text == "ChangePermissions")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ChangePermissions, AccessControlType.Allow);
else if (acl.Text == "CreateDirectories")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.CreateDirectories, AccessControlType.Allow);
else if (acl.Text == "CreateFiles")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.CreateFiles, AccessControlType.Allow);
else if (acl.Text == "Delete")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Delete, AccessControlType.Allow);
else if (acl.Text == "DeleteSubdirectoriesAndFiles")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Allow);
else if (acl.Text == "ExecuteFile")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ExecuteFile, AccessControlType.Allow);
else if (acl.Text == "FullControl")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.FullControl, AccessControlType.Allow);
else if (acl.Text == "ListDirectory")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ListDirectory, AccessControlType.Allow);
else if (acl.Text == "Modify")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Modify, AccessControlType.Allow);
else if (acl.Text == "Read")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Read, AccessControlType.Allow);
else if (acl.Text == "ReadAndExecute")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ReadAndExecute, AccessControlType.Allow);
else if (acl.Text == "ReadAttributes")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ReadAttributes, AccessControlType.Allow);
else if (acl.Text == "ReadData")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ReadData, AccessControlType.Allow);
else if (acl.Text == "ReadExtendedAttributes")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ReadExtendedAttributes, AccessControlType.Allow);
else if (acl.Text == "ReadPermissions")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ReadPermissions, AccessControlType.Allow);
else if (acl.Text == "Synchronize")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Synchronize, AccessControlType.Allow);
else if (acl.Text == "TakeOwnership")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.TakeOwnership, AccessControlType.Allow);
else if (acl.Text == "Traverse")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Traverse, AccessControlType.Allow);
else if (acl.Text == "Write")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Write, AccessControlType.Allow);
else if (acl.Text == "WriteAttributes")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.WriteAttributes, AccessControlType.Allow);
else if (acl.Text == "WriteData")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.WriteData, AccessControlType.Allow);
else if (acl.Text == "WriteExtendedAttributes")
AddFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.WriteExtendedAttributes, AccessControlType.Allow);
else
MessageBox.Show("incorrect input (Right)!");
return;
}
//DELETE ACL (аккаунт из текстбокса аккаунта)
private void Button2_Click(object sender, EventArgs e)
{
//получим текущий аккаунт
var File_Security = File.GetAccessControl(this.Tag.ToString());//аргумент - путь к файлу
var SID = File_Security.GetOwner(typeof(SecurityIdentifier));
var Owner = SID.Translate(typeof(NTAccount));
//удаляем запись
if (acl.Text == "AppendData")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.AppendData, AccessControlType.Allow);
else if (acl.Text == "ChangePermissions")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ChangePermissions, AccessControlType.Allow);
else if (acl.Text == "CreateDirectories")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.CreateDirectories, AccessControlType.Allow);
else if (acl.Text == "CreateFiles")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.CreateFiles, AccessControlType.Allow);
else if (acl.Text == "Delete")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Delete, AccessControlType.Allow);
else if (acl.Text == "DeleteSubdirectoriesAndFiles")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.DeleteSubdirectoriesAndFiles, AccessControlType.Allow);
else if (acl.Text == "ExecuteFile")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ExecuteFile, AccessControlType.Allow);
else if (acl.Text == "FullControl")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.FullControl, AccessControlType.Allow);
else if (acl.Text == "ListDirectory")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ListDirectory, AccessControlType.Allow);
else if (acl.Text == "Modify")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Modify, AccessControlType.Allow);
else if (acl.Text == "Read")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Read, AccessControlType.Allow);
else if (acl.Text == "ReadAndExecute")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ReadAndExecute, AccessControlType.Allow);
else if (acl.Text == "ReadAttributes")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ReadAttributes, AccessControlType.Allow);
else if (acl.Text == "ReadData")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ReadData, AccessControlType.Allow);
else if (acl.Text == "ReadExtendedAttributes")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ReadExtendedAttributes, AccessControlType.Allow);
else if (acl.Text == "ReadPermissions")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.ReadPermissions, AccessControlType.Allow);
else if (acl.Text == "Synchronize")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Synchronize, AccessControlType.Allow);
else if (acl.Text == "TakeOwnership")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.TakeOwnership, AccessControlType.Allow);
else if (acl.Text == "Traverse")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Traverse, AccessControlType.Allow);
else if (acl.Text == "Write")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.Write, AccessControlType.Allow);
else if (acl.Text == "WriteAttributes")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.WriteAttributes, AccessControlType.Allow);
else if (acl.Text == "WriteData")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.WriteData, AccessControlType.Allow);
else if (acl.Text == "WriteExtendedAttributes")
RemoveFileSecurity(this.Tag.ToString(), account.Text, FileSystemRights.WriteExtendedAttributes, AccessControlType.Allow);
else
MessageBox.Show("incorrect input (Right)!");
return;
}
// Adds an ACL entry on the specified file for the specified account.
public static void AddFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
try
{
// Create a new FileInfo object.
FileInfo fInfo = new FileInfo(FileName);
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = fInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));
// Set the new access settings.
fInfo.SetAccessControl(fSecurity);
}
catch { MessageBox.Show("incorrect input (Account)!"); }
}
// Removes an ACL entry on the specified file for the specified account.
public static void RemoveFileSecurity(string fileName, string account, FileSystemRights rights, AccessControlType controlType)
{
try
{
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = File.GetAccessControl(fileName);
// Remove the FileSystemAccessRule from the security settings.
fSecurity.RemoveAccessRule(new FileSystemAccessRule(account, rights, controlType));
// Set the new access settings.
File.SetAccessControl(fileName, fSecurity);
}
catch { MessageBox.Show("incorrect input (Account)!"); }
}
}
}