-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
341 lines (284 loc) · 12.4 KB
/
Program.cs
File metadata and controls
341 lines (284 loc) · 12.4 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
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace aide_clamav;
public class Program
{
public enum Errors
{
success = 0,
help = 1,
confFileNotExists = 101,
reportFileNotExists = 102,
clamscanFileNotExists = 103,
confFileLengthIncorrect = 1011
};
public static readonly string Version = "2023.04.27";
public static int Main(string[] args)
{
var started = DateTime.Now;
if (args.Length < 1 || args[0] == "--help" || args[0] == "-h" || args[0] == "/?")
{
PrintHelp();
return (int)Errors.help;
}
if (args[0] == "?")
{
args[0] = "aide-clamav.conf";
}
var confFile = new FileInfo(args[0]);
confFile.Refresh();
if (!confFile.Exists)
{
Console.Error.WriteLine($"The configuration file not exists: \"{confFile.FullName}\"");
return (int)Errors.confFileNotExists;
}
var confLines = File.ReadAllLines(confFile.FullName);
if (confFile.Length < 3)
{
Console.Error.WriteLine($"Configuration file incorrect: three lines required, but there are {confFile.Length} only");
PrintHelp();
return (int)Errors.confFileLengthIncorrect;
}
var reportFile = new FileInfo(confLines[0]);
reportFile.Refresh();
if (!reportFile.Exists)
{
Console.Error.WriteLine($"The AIDE report.log file not exists: \"{reportFile.FullName}\"");
return (int)Errors.reportFileNotExists;
}
Console.WriteLine($"Begin. Time {started.ToLocalTime()}");
var clamscanFile = confLines[1];
var clamscanArgs = confLines[2];
if (confLines.Length > 3)
{
Int32.TryParse(confLines[3], out clamscanThreads);
if (clamscanThreads < 1 || clamscanThreads > Environment.ProcessorCount)
{
Console.Error.WriteLine("Fourth line in configuration file: clamscanThreads is incorrect. Setted to 1");
}
}
if (clamscanThreads < 1 || clamscanThreads > Environment.ProcessorCount)
{
clamscanThreads = Environment.ProcessorCount;
}
var reportLines = File.ReadAllLines(reportFile.FullName);
var fs = new List<string>(reportLines.Length);
// foreach (var line in reportLines)
Parallel.ForEach
(
reportLines,
delegate (string line, ParallelLoopState _state, long _)
{
// "d" - это директория - их мы не проверяем, только файлы
// "f" - строки всех файлов начинаются на букву "f"
if (!line.Contains(":") || !line.StartsWith("f"))
return;
var sLine = line.Split(':', 2, StringSplitOptions.RemoveEmptyEntries);
if (sLine.Length < 2)
return;
var fileName = sLine[1].TrimStart();
if (fileName.Length <= 0)
return;
// Получаем имя файла для антивирусной проверки
var fi = new FileInfo(fileName);
fi.Refresh();
if (!fi.Exists || fi.Length <= 0) // fi.LinkTarget != null
{
// Console.WriteLine("File skipped: " + fi.FullName);
return;
}
// ExecClamScan(fi, clamscanFile, clamscanArgs);
Interlocked.Add(ref TotalSize, fi.Length);
lock (fs)
{
fs.Add(fi.FullName);
}
}
);
fs.Sort();
Console.WriteLine($"Files to scan: " + fs.Count);
string lastFileName = "";
var count = fs.Count / clamscanThreads;
var curList = new List<string>(count + 1);
var lineLen = 0;
var FilesSize = 0L;
var allCount = 0L;
File.WriteAllText(reportFileName, "");
File.WriteAllText(errorFileName, "");
for (int i = 0; i < fs.Count; i++)
{
// Не повторяем файлы, если вдруг они повторно перечисленны в списке
if (fs[i] == lastFileName)
continue;
curList.Add(fs[i]);
lineLen += fs[i].Length;
FilesSize += new FileInfo(fs[i]).Length;
allCount++;
if (curList.Count > count || lineLen >= MaxCommandLen)
{
ExecClamScan(curList, clamscanFile, clamscanArgs, FilesSize);
curList = new List<string>(count + 1);
lineLen = 0;
FilesSize = 0;
}
PrintExecutionStatus(started, clamscanThreads - 1);
}
if (fs.Count != allCount)
Console.WriteLine("\nAdded to scan " + allCount + " files");
if (curList.Count > 0)
ExecClamScan(curList, clamscanFile, clamscanArgs, FilesSize);
PrintExecutionStatus(started); Console.WriteLine();
if (countOfInfectedFiles > 0)
Console.WriteLine("FOUND INFECTED FILES: " + countOfInfectedFiles);
if (allCount != countOfScannedFiles)
{
Console.WriteLine($"Program ended. FAILURE! Added to scan: {fs.Count} files; scanned: {countOfScannedFiles} files");
}
else
{
Console.WriteLine($"Program successfully ended. Scanned {countOfScannedFiles} files. Infected files: {countOfInfectedFiles}");
}
Console.WriteLine("clamav reports in the file " + Path.GetFullPath(reportFileName));
if (new FileInfo(errorFileName).Length > 0)
Console.WriteLine("errors reports in the file " + Path.GetFullPath(errorFileName));
return (int)Errors.success;
static void PrintExecutionStatus(DateTime started, int maxCountOfTasks = 0)
{
while (countOfTasks > maxCountOfTasks)
{
lock (sync)
Monitor.Wait(sync/*, 60_000*/);
var now = DateTime.Now;
var sp = now - started;
Console.CursorLeft = 0;
Console.Write($"{(sizeOfScanndeFiles*100f/TotalSize).ToString("F1"), 5}% completed. Execution time {sp.TotalMinutes.ToString("F0"), 2} minutes. Time {now.ToLocalTime()}");
Console.CursorLeft = 0;
}
}
}
public readonly static string reportFileName = "clamav-report.log";
public readonly static string errorFileName = "errors.log";
public const int MaxCommandLen = 512*1024 - 4096;
protected static object sync = new Object();
protected static int countOfTasks = 0;
protected static int countOfScannedFiles = 0;
protected static int countOfInfectedFiles = 0;
protected static long TotalSize = 0;
protected static long sizeOfScanndeFiles = 0;
protected static int clamscanThreads = 0;
protected static ConcurrentBag<Process> PIs = new ConcurrentBag<Process>();
public static void ExecClamScan(List<string> FullNames, string clamscanFile, string clamscanArgs, long FilesSize)
{
// Console.WriteLine($"Executed for {FullNames.Count} files");
Interlocked.Increment(ref countOfTasks);
ThreadPool.QueueUserWorkItem
(
delegate
{
var sb = new StringBuilder(1024 * 1024);
sb.Append(clamscanArgs);
foreach (var file in FullNames)
{
sb.Append(" \"");
sb.Append(file);
sb.Append("\"");
}
// File.AppendAllText(reportFileName, sb.ToString());
var psi = new ProcessStartInfo(clamscanFile, sb.ToString()); sb.Clear();
psi.RedirectStandardOutput = true;
using var pi = Process.Start(psi);
if (pi == null)
{
Console.Error.WriteLine($"Executing for command failed: {psi.FileName} {psi.Arguments}");
return;
}
renice(pi);
pi.WaitForExit();
var clamavReport = pi.StandardOutput.ReadToEnd();
lock (sync)
File.AppendAllText(reportFileName, clamavReport + "\n\n----------------------------------------------------------------\n\n");
int scanned = 0;
int infected = 0;
try
{
infected = getIntValue(clamavReport, "Infected files: ");
scanned = getIntValue(clamavReport, "Scanned files: ");
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
Interlocked.Decrement(ref countOfTasks);
Interlocked.Add(ref countOfScannedFiles, scanned);
Interlocked.Add(ref countOfInfectedFiles, infected);
Interlocked.Add(ref sizeOfScanndeFiles, FilesSize);
lock (sync)
{
Monitor.Pulse(sync);
}
if (scanned != FullNames.Count || infected > 0)
{
sb.Clear();
sb.AppendLine($"Error or infected in files. Infected: {infected}; Scanned: {scanned}. Files:");
foreach (var file in FullNames)
sb.AppendLine(file);
lock (sync)
File.AppendAllText(errorFileName, sb.ToString() + "\n\n----------------------------------------------------------------\n\n\n\n");
}
}
);
static int getIntValue(string clamavReport, string sfStr)
{
int scanned;
var index0 = clamavReport.IndexOf(sfStr) + sfStr.Length;
int index1 = -1;
for (int i = index0; i < clamavReport.Length; i++)
{
if (clamavReport[i] >= '0' && clamavReport[i] <= '9')
{
index0 = i;
break;
}
}
for (int i = index0; i < clamavReport.Length; i++)
{
if (clamavReport[i] >= '0' && clamavReport[i] <= '9')
continue;
index1 = i;
break;
}
var str = clamavReport.Substring(index0, index1 - index0);
scanned = Int32.Parse(str);
return scanned;
}
static void renice(Process pi)
{
var psi = new ProcessStartInfo("renice", "-n 19 -p " + pi.Id);
psi.RedirectStandardOutput = true;
using var pi1 = Process.Start(psi);
psi = new ProcessStartInfo("ionice", "-c 3 -p " + pi.Id);
psi.RedirectStandardOutput = true;
using var pi2 = Process.Start(psi);
}
}
public static void PrintHelp()
{
Console.Error.WriteLine("aide_clamav version: " + Version);
Console.Error.WriteLine("https://github.com/VinnySmallUtilities/aide-clamav");
Console.Error.WriteLine();
Console.Error.WriteLine("aide_clamav /path_to_conf_file");
Console.Error.WriteLine("'aide_clamav ?' for use aide-clamav.conf in current directory");
Console.Error.WriteLine();
Console.Error.WriteLine("configuration file format:");
Console.Error.WriteLine("first line: a path to the AIDE report.log");
Console.Error.WriteLine("second line: clamscan command: clamscan");
Console.Error.WriteLine("third line: clamscan arguments");
Console.Error.WriteLine("fourth line (optional): clamscan processes count for parallel execution");
}
}