-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathMain.cs
423 lines (406 loc) · 18 KB
/
Main.cs
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using Sunny.UI;
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
using System.Xml;
using WHC.OrderWater.Commons;
namespace WindowsBaselineAssistant
{
public partial class Main : UIForm
{
public Main()
{
InitializeComponent();
LogHelper.OnError += LogHelper_OnError;
}
XmlNodeList xmlNodeList;
XmlDocument xmlDocument;
XmlElement xmlElement;
/*static string secInfoFile = $"{Util.CurrentDirectory}config.cfg";
static string fortifyFile = $"{Util.CurrentDirectory}fortify.cfg";*/
static string secInfoFile = $"{Util.RoamingDirectory}config.cfg";
static string fortifyFile = $"{Util.RoamingDirectory}fortify.cfg";
string secInfoCmd = $"secedit /export /cfg {Util.RoamingDirectory}config.cfg";
string fortifyCmd = $"secedit /configure /db {Util.RoamingDirectory}fortify.sdb /cfg {Util.RoamingDirectory}fortify.cfg";
string openRegCmd = "regjump.exe {0} /accepteula";
string suggestTip = string.Empty;
readonly string fortifyTip = "在进行加固前,我们需要确认您已知晓该操作对您设备关键性位置进行更改的风险.\n\n" +
"是否了解该操作的风险?";
private void SetPassStyle(int index)
{
BaselineList.Rows[index].Cells[6].Style.ForeColor = Color.Green;
BaselineList.Rows[index].Cells[7].ReadOnly = true;
BaselineList.Rows[index].Cells[7].Style.BackColor = Color.LightGray;
}
private void CheckBtn_Click(object sender, EventArgs e)
{
try
{
xmlDocument = Util.ReadXml(Util.CurrentDirectory + @"\item.xml");
if (xmlDocument == null)
{
UIMessageBox.ShowError("未找到配置文件");
return;
}
if (!Directory.Exists(Util.RoamingDirectory))
{
Directory.CreateDirectory(Util.RoamingDirectory);
}
if (File.Exists(secInfoFile))
{
File.Delete(secInfoFile);
}
//int code = GenerateSecInfo();
if (!Util.ExecutExternalProgram(secInfoCmd, true, ProcessWindowStyle.Hidden).Equals(0))
{
UIMessageBox.ShowError("secedit信息生成失败");
return;
}
BaselineList.Rows.Clear();
int totalCount, passCount = 0;
double rate, ratePercent = 0;
xmlElement = xmlDocument.DocumentElement;
xmlNodeList = xmlElement.ChildNodes;
foreach (XmlNode xmlNode in xmlNodeList)
{
int index = BaselineList.Rows.Add();
/*string name = xmlNode["name"].InnerText;*/
BaselineList.Rows[index].Cells[0].Value = xmlNode["name"].InnerText;
BaselineList.Rows[index].Cells[1].Value = xmlNode["description"].InnerText;
BaselineList.Rows[index].Cells[4].Value = xmlNode["standard"].InnerText;
BaselineList.Rows[index].Cells[8].Value = "string";
if (Util.ContainsElement(xmlNode, "valuetype"))
{
BaselineList.Rows[index].Cells[8].Value = xmlNode["valuetype"].InnerText;
}
string queryType = xmlNode["type"].InnerText;
string reality = string.Empty, section = string.Empty, warning = string.Empty;
//string warning = string.Empty;
string standard = xmlNode["standard"].InnerText;
switch (queryType)
{
case "secedit":
string mark = xmlNode["mark"].InnerText;
(section, reality) = Util.GetResultByMark(mark);
BaselineList.Rows[index].Cells[2].Value = section;
BaselineList.Rows[index].Cells[3].Value = mark;
BaselineList.Rows[index].Cells[5].Value = reality;
break;
case "registry":
BaselineList.Rows[index].Cells[2].Value = xmlNode["registry"].InnerText;
BaselineList.Rows[index].Cells[3].Value = xmlNode["regitem"].InnerText;
reality = RegistryHelper.GetValue(xmlNode["registry"].InnerText, xmlNode["regitem"].InnerText);
BaselineList.Rows[index].Cells[5].Value = reality;
break;
default:
break;
}
string assessment = xmlNode["assessment"].InnerText;
if (Util.ContainsElement(xmlNode, "manual"))
{
BaselineList.Rows[index].Cells[6].Value = "手动加固";
BaselineList.Rows[index].Cells[7].ReadOnly = true;
BaselineList.Rows[index].Cells[6].Style.ForeColor = Color.Orange;
BaselineList.Rows[index].Cells[7].Style.BackColor = Color.Orange;
BaselineList.Rows[index].Cells[7].ToolTipText = "该项不支持自动加固";
passCount++;
continue;
}
if (Util.ContainsElement(xmlNode, "ignore"))
{
BaselineList.Rows[index].Cells[6].Value = "已忽略";
BaselineList.Rows[index].Cells[7].ReadOnly = true;
BaselineList.Rows[index].Cells[6].Style.ForeColor = UIColor.LayuiBlue;
BaselineList.Rows[index].Cells[7].Style.BackColor = UIColor.LayuiBlue;
BaselineList.Rows[index].Cells[7].ToolTipText = "该项配置为忽略";
passCount++;
continue;
}
BaselineList.Rows[index].Cells[6].Value = "不符合";
if (reality.Equals("未设置"))
{
continue;
}
switch (assessment)
{
case "fixed"://注:固定值为不符合
if (!reality.Equals(standard))
{
SetPassStyle(index);
BaselineList.Rows[index].Cells[6].Value = "符合";
passCount++;
}
break;
case "equals":
case "enum":
if (reality.Equals(standard))
{
SetPassStyle(index);
BaselineList.Rows[index].Cells[6].Value = "符合";
passCount++;
}
break;
case "greaternumber":
if (reality.ToInt() >= standard.ToInt())
{
SetPassStyle(index);
BaselineList.Rows[index].Cells[6].Value = "符合";
passCount++;
}
break;
case "lessnumber":
if (reality.ToInt() <= standard.ToInt())
{
SetPassStyle(index);
BaselineList.Rows[index].Cells[6].Value = "符合";
passCount++;
}
break;
case "array":
if (reality.Equals(standard))
{
SetPassStyle(index);
BaselineList.Rows[index].Cells[6].Value = "符合";
passCount++;
}
break;
default:
BaselineList.Rows[index].Cells[6].Value = "未定义的类型";
break;
}
}
//统计数据
totalCount = xmlNodeList.Count;
totalCountLabel.Text = totalCount.ToString();
passCountLabel.Text = passCount.ToString();
failCountLabel.Text = (totalCount - passCount).ToString();
rate = Convert.ToDouble(passCount) / Convert.ToDouble(totalCount);
ratePercent = Math.Round(rate, 2) * 100;
if (ratePercent < 50)
{
rateLabel.ForeColor = Color.Red;
suggestTip = "建议进行加固!";
}
else if (ratePercent >= 50 && ratePercent < 80)
{
rateLabel.ForeColor = Color.Orange;
suggestTip = "建议根据业务需求进行加固!";
}
else
{
rateLabel.ForeColor = Color.Green;
suggestTip = "满足基线要求,可自行判断进行加固!";
}
rateLabel.Text = ratePercent + $"% {suggestTip}";
}
catch (Exception ex)
{
LogHelper.WriteLog(ex.Message.ToString(), ex);
UIMessageBox.ShowError("检测出现异常");
}
}
private void OpenSubKeyToolStripMenuItem_Click(object sender, EventArgs e)
{
int selectedRowIndex = BaselineList.SelectedCells[0].RowIndex;
string cellValue = BaselineList[2, selectedRowIndex].Value.ToString();
if (!cellValue.Contains("HKEY"))
{
UIMessageBox.ShowError("该项不支持打开");
return;
}
if (!Util.ExecutExternalProgram(string.Format(openRegCmd, cellValue), true).Equals(0))
{
UIMessageBox.ShowError("打开注册表项失败");
}
}
private void Main_Load(object sender, EventArgs e)
{
// 获取当前执行的程序集
Assembly assembly = Assembly.GetExecutingAssembly();
// 获取程序集的版本信息
Version version = assembly.GetName().Version;
this.Text += $" v{version}";
OSLabel.Text = Util.GetOSVersion();
foreach (string ip in Util.GetIPAddress())
{
IpListComboBox.Items.Add(ip);
}
IpListComboBox.SelectedIndex = 0;
OSNameLabel.Text = Environment.MachineName;
if (!Directory.Exists(Util.RoamingDirectory))
{
Directory.CreateDirectory(Util.RoamingDirectory);
}
}
private void FortifyBtn_Click(object sender, EventArgs e)
{
try
{
if (BaselineList.RowCount.Equals(0))
{
UIMessageBox.ShowWarning($"无检测数据,请检测后再尝试此操作!");
return;
}
if (!UIMessageDialog.ShowAskDialog(this, fortifyTip, UIStyle.LayuiOrange))
{
return;
}
int fortifyCount = 0;
if (File.Exists(fortifyFile))
{
File.Delete(fortifyFile);
}
IniFile iniFile = new IniFile(fortifyFile);
iniFile.Write("Version", "signature", "$CHICAGO$");
foreach (DataGridViewRow dataGridViewRow in BaselineList.Rows)
{
if (dataGridViewRow.Cells["FortifyColumn"].Value == null || !(bool)dataGridViewRow.Cells["FortifyColumn"].Value)
{
continue;
}
string fortifyItem, fortifyField, fortifyValue, fortifyValueType = string.Empty;
fortifyItem = dataGridViewRow.Cells["ItemColumn"].Value.ToString();
fortifyField = dataGridViewRow.Cells["FieldColumn"].Value.ToString();
fortifyValue = dataGridViewRow.Cells["StandardColumn"].Value.ToString();
fortifyValueType = dataGridViewRow.Cells["ValueTypeColumn"].Value.ToString();
if (fortifyItem.Contains("HKEY"))
{
RegistryHelper.SaveValue(fortifyItem, fortifyField, fortifyValue, fortifyValueType);
fortifyCount++;
continue;
}
//secedit加固
iniFile.Write(fortifyItem, fortifyField, fortifyValue);
iniFile.UpdateFile();
fortifyCount++;
}
int code = Util.ExecutExternalProgram(fortifyCmd, true, ProcessWindowStyle.Hidden);
if (code != 0)
{
UIMessageBox.ShowWarning($"已完成{fortifyCount}项加固操作,secedit加固出现错误.请检查%windir%\\security\\logs\\scesrv.log");
return;
}
checkAllCheckBox.Checked = false;
UIMessageBox.ShowSuccess($"已完成{fortifyCount}项加固操作,请再次进行检测");
}
catch (Exception ex)
{
LogHelper.WriteLog(ex.Message.ToString(), ex);
UIMessageBox.ShowError("加固出现异常");
}
}
private void checkAllCheckBox_CheckedChanged(object sender, EventArgs e)
{
try
{
if (!checkAllCheckBox.Checked)
{
foreach (DataGridViewRow row in BaselineList.Rows)
{
DataGridViewCell checkBoxCell = row.Cells["FortifyColumn"];
/*if (!checkBoxCell.ReadOnly)
{
checkBoxCell.Value = true;
}*/
checkBoxCell.Value = false;
}
return;
}
// 遍历 DataGridView 的所有行,并将 "CheckBoxColumn" 列的复选框设置为选中状态
foreach (DataGridViewRow row in BaselineList.Rows)
{
DataGridViewCell checkBoxCell = row.Cells["FortifyColumn"];
if (!checkBoxCell.ReadOnly)
{
checkBoxCell.Value = true;
}
}
}
catch (Exception ex)
{
UIMessageBox.ShowError(ex.Message);
}
}
[Obsolete]
private void ReportBtn_Click(object sender, EventArgs e)
{
try
{
if (BaselineList.RowCount.Equals(0))
{
UIMessageBox.ShowWarning($"无检测数据,请检测或加固后再尝试此操作!");
return;
}
// 创建一个新的 Excel 工作簿和工作表
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("Sheet1");
string reportFileName = $"Windows安全基线检测加固结果汇总表-{IpListComboBox.SelectedItem.ToString()}.xlsx";
// 在表格最上面添加一行系统信息
Report.AddSystemInfoRow(sheet, OSLabel.Text, IpListComboBox.SelectedItem.ToString(), OSNameLabel.Text);
// 将 DataGridView 中的数据写入 Excel 表格
Report.WriteDataGridViewToExcel(BaselineList, sheet);
// 自动调整列宽
Report.AutoSizeColumns(sheet);
// 保存 Excel 文件
using (FileStream fs = new FileStream(reportFileName, FileMode.Create, FileAccess.Write))
{
workbook.Write(fs);
UIMessageBox.ShowSuccess($"导出成功!\n结果已保存到\"{reportFileName}\"");
}
}
catch (Exception ex)
{
LogHelper.WriteLog(ex.Message.ToString(), ex);
UIMessageBox.ShowError("导出出现异常");
}
}
private void showValueTypeuiCheckBox_CheckedChanged(object sender, EventArgs e)
{
if (!showValueTypeuiCheckBox.Checked)
{
BaselineList.Columns[8].Visible = false;
return;
}
BaselineList.Columns[8].Visible = true;
}
private void AddRulebtn_Click(object sender, EventArgs e)
{
AddRule addRule = new AddRule();
addRule.ShowDialog();
}
private void Logbtn_Click(object sender, EventArgs e)
{
// 启动资源管理器并打开该文件夹
Process.Start("explorer.exe", Util.RoamingDirectory);
Logbtn.ShowTips = false;
}
private void LogHelper_OnError()
{
Invoke(new Action(() =>
{
Logbtn.ShowTips = true;
}));
}
private void BlogAvatar_Click(object sender, EventArgs e)
{
Process.Start(new ProcessStartInfo
{
FileName = "https://saferoad.cc/",
UseShellExecute = true
});
}
private void RepoAvatar_Click(object sender, EventArgs e)
{
Process.Start(new ProcessStartInfo
{
FileName = "https://github.com/DeEpinGh0st/WindowsBaselineAssistant",
UseShellExecute = true
});
}
}
}