forked from elibtigha/gitscan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScanXMock.cs
101 lines (91 loc) · 4.02 KB
/
ScanXMock.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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using MS.Ess.Scan.DataContracts.Private;
namespace OctokitDemo
{
public class ScanXMock
{
private static readonly Lazy<ConcurrentDictionary<Guid, MalwareDeterminationResult>> ScanResults =
new Lazy<ConcurrentDictionary<Guid, MalwareDeterminationResult>>(
() =>
{
return new ConcurrentDictionary<Guid, MalwareDeterminationResult>();
});
public Task SendScanRequest(MalwareDeterminationRequest request)
{
// "Fire and forget" (for demo only)
return Process(request);
; }
public async Task<MalwareDeterminationResult> GetResult(Guid requestId)
{
await Task.Delay(500);
ScanResults.Value.TryRemove(requestId, out MalwareDeterminationResult result);
return result;
}
public async Task<bool> TryGetResult(Guid requestId)
{
await Task.Delay(500);
return ScanResults.Value.TryGetValue(requestId, out MalwareDeterminationResult result);
}
private async Task Process(MalwareDeterminationRequest request)
{
// Download ZIP
WebClient webClient = new WebClient();
byte[] response = await webClient.DownloadDataTaskAsync(request.Uri).ConfigureAwait(false);
webClient.Dispose();
// Unzip and try to find "malware" files
List<ConfirmedMalwareInfo> detectedFiles = new List<ConfirmedMalwareInfo>();
int unzippedFilesCount = 0;
Stream unzippedEntryStream; // Unzipped data from a file in the archive
using (Stream data = new MemoryStream(response))
using (ZipArchive archive = new ZipArchive(data))
{
unzippedFilesCount = archive.Entries.Count;
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.Contains("malware"))
{
using (unzippedEntryStream = entry.Open())
{
// convert stream to string
using (StreamReader reader = new StreamReader(unzippedEntryStream))
{
string text = await reader.ReadToEndAsync().ConfigureAwait(false);
// Expected value: <malwarename>:<avengine#1>,...,<avengine#n>
string[] arr = text.Split(new char[] { ':' });
ConfirmedMalwareInfo info = new ConfirmedMalwareInfo();
info.FileName = entry.FullName.Substring(entry.FullName.IndexOf('/'));
info.MalwareInfo = arr[0];
if (arr.Count() > 1)
{
info.AvEngines = arr[1].Split(new char[] { ',' }).ToList();
}
detectedFiles.Add(info);
}
}
}
}
// Compose a scan result
MalwareDeterminationResult detectionResult = new MalwareDeterminationResult();
if (detectedFiles.Count() > 0)
{
detectionResult.ClientId = request.ClientId;
detectionResult.RequestId = request.RequestId;
detectionResult.ConfirmedMalwares = detectedFiles;
detectionResult.WorkStatus = WorkStatus.ConfirmedMalware;
}
else
{
detectionResult.WorkStatus = WorkStatus.Clean;
}
ScanResults.Value.TryAdd(request.RequestId, detectionResult);
}
}
}
}