-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVirusTotal_Folder_Monitor.ps1
More file actions
382 lines (321 loc) · 13.1 KB
/
VirusTotal_Folder_Monitor.ps1
File metadata and controls
382 lines (321 loc) · 13.1 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
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
# Function to calculate multiple hashes of a file
function Get-MultipleFileHashes {
param (
[string]$FilePath,
[string[]]$Algorithms = @("MD5", "SHA1", "SHA256", "SHA384", "SHA512")
)
$hashes = @{}
foreach ($Algorithm in $Algorithms) {
$hash = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm)
$stream = [System.IO.File]::OpenRead($FilePath)
$fileHash = $hash.ComputeHash($stream)
$stream.Close()
$hashes[$Algorithm] = [BitConverter]::ToString($fileHash) -replace '-', ''
}
return $hashes
}
# Function to check hash on VirusTotal
function Check-VirusTotal {
param (
[string]$Hash,
[string]$ApiKey
)
$url = "https://www.virustotal.com/api/v3/files/$Hash"
$headers = @{
"x-apikey" = $ApiKey
}
try {
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
return $response
}
catch {
return $null
}
}
# Function to upload file to VirusTotal
function Upload-To-VirusTotal {
param (
[string]$FilePath,
[string]$ApiKey
)
$url = "https://www.virustotal.com/api/v3/files"
$headers = @{
"x-apikey" = $ApiKey
}
$fileContent = Get-Content -Path $FilePath -Raw -Encoding Byte
$body = @{
file = [System.IO.File]::OpenRead($FilePath)
}
try {
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Post -Body $body
return $response
}
catch {
return $null
}
}
# Function to get additional file information from VirusTotal
function Get-AdditionalFileInfo {
param (
[string]$Hash,
[string]$ApiKey
)
$url = "https://www.virustotal.com/api/v3/files/$Hash"
$headers = @{
"x-apikey" = $ApiKey
}
try {
$response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
$attributes = $response.data.attributes
$additionalInfo = @{
TrID = $attributes.trid
DetectItEasy = $attributes.detectiteasy
Magic = $attributes.magic
Magika = $attributes.magika
PEiDPacker = $attributes.packers.peid
ImpHash = $attributes.pe_info.imphash
}
return $additionalInfo
}
catch {
return $null
}
}
# Function to process files in a folder
function Process-Files {
param (
[string]$Path,
[string[]]$Algorithms,
[string]$ApiKey,
[bool]$Verbose
)
$dateTime = Get-Date -Format 'yyyyMMdd_HHmmss'
$hashLogFile = Join-Path ([Environment]::GetFolderPath("Desktop")) "HashLog_$dateTime.txt"
$vtLogFile = Join-Path ([Environment]::GetFolderPath("Desktop")) "VirusTotalLog_$dateTime.txt"
$hashLogContent = "Date: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`r`n"
$hashLogContent += "Path: $Path`r`n"
$hashLogContent += "Algorithms: $($Algorithms -join ', ')`r`n`r`n"
$vtLogContent = "Date: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`r`n"
$vtLogContent += "Path: $Path`r`n"
$vtLogContent += "Algorithms: $($Algorithms -join ', ')`r`n`r`n"
if (Test-Path -Path $Path -PathType Leaf) {
$files = @(Get-Item -Path $Path)
}
elseif (Test-Path -Path $Path -PathType Container) {
$files = Get-ChildItem -Path $Path -Recurse -File
}
else {
Write-Host "Invalid path. Please enter a valid file or folder path."
return
}
$fileCount = $files.Count
$currentFile = 0
$filesToUpload = @()
foreach ($file in $files) {
$currentFile++
$percentComplete = [math]::Round(($currentFile / $fileCount) * 100, 2)
Write-Progress -Activity "Processing files" -Status "$percentComplete% Complete" -PercentComplete $percentComplete
$hashes = Get-MultipleFileHashes -FilePath $file.FullName -Algorithms $Algorithms
$hashLogContent += "File: $($file.FullName)`r`n"
foreach ($algo in $Algorithms) {
$hashLogContent += "{0}: {1}`r`n" -f $algo, $hashes[$algo]
}
$hashLogContent += "`r`n"
$vtResult = Check-VirusTotal -Hash $hashes["SHA256"] -ApiKey $ApiKey
$vtLogContent += "File: $($file.FullName)`r`n"
$vtLogContent += "SHA256: $($hashes["SHA256"])`r`n"
if ($vtResult -ne $null) {
$detectionCount = $vtResult.data.attributes.last_analysis_stats.malicious
$totalEngines = $vtResult.data.attributes.last_analysis_stats.malicious + $vtResult.data.attributes.last_analysis_stats.undetected
$fileType = $vtResult.data.attributes.type_description
$vtLogContent += "Detection: $detectionCount / $totalEngines`r`n"
$vtLogContent += "File Type: $fileType`r`n"
$vtLogContent += "First Seen: $($vtResult.data.attributes.first_submission_date)`r`n"
$vtLogContent += "Last Seen: $($vtResult.data.attributes.last_submission_date)`r`n"
if ($Verbose) {
$additionalInfo = Get-AdditionalFileInfo -Hash $hashes["SHA256"] -ApiKey $ApiKey
if ($additionalInfo -ne $null) {
$vtLogContent += "TrID: $($additionalInfo.TrID)`r`n"
$vtLogContent += "Detect It Easy: $($additionalInfo.DetectItEasy)`r`n"
$vtLogContent += "Magic: $($additionalInfo.Magic)`r`n"
$vtLogContent += "Magika: $($additionalInfo.Magika)`r`n"
$vtLogContent += "PEiD Packer: $($additionalInfo.PEiDPacker)`r`n"
$vtLogContent += "ImpHash: $($additionalInfo.ImpHash)`r`n"
}
}
}
else {
$vtLogContent += "VirusTotal: No results found or API error`r`n"
$filesToUpload += $file.FullName
}
$vtLogContent += "`r`n"
}
# Save initial log files
$hashLogContent | Out-File -FilePath $hashLogFile -Encoding utf8
$vtLogContent | Out-File -FilePath $vtLogFile -Encoding utf8
Write-Host "Hash log file saved to: $hashLogFile"
Write-Host "VirusTotal log file saved to: $vtLogFile"
if ($filesToUpload.Count -gt 0) {
Write-Host "Some files are not available on VirusTotal:"
$filesToUpload | ForEach-Object { Write-Host $_ }
$userChoice = Read-Host "Do you want to upload these files to VirusTotal? (yes/no/all/exit)"
if ($userChoice -eq "yes") {
$fileToUpload = Read-Host "Enter the full path of the file you want to upload"
if ($filesToUpload -contains $fileToUpload) {
$uploadResult = Upload-To-VirusTotal -FilePath $fileToUpload -ApiKey $ApiKey
if ($uploadResult -ne $null) {
$vtLogContent += "File: $fileToUpload`r`n"
$vtLogContent += "Upload Status: Uploaded successfully`r`n"
$vtLogContent += "`r`n"
} else {
$vtLogContent += "File: $fileToUpload`r`n"
$vtLogContent += "Upload Status: Failed to upload`r`n"
$vtLogContent += "`r`n"
}
} else {
Write-Host "Invalid file path."
}
} elseif ($userChoice -eq "all") {
foreach ($fileToUpload in $filesToUpload) {
$uploadResult = Upload-To-VirusTotal -FilePath $fileToUpload -ApiKey $ApiKey
if ($uploadResult -ne $null) {
$vtLogContent += "File: $fileToUpload`r`n"
$vtLogContent += "Upload Status: Uploaded successfully`r`n"
$vtLogContent += "`r`n"
} else {
$vtLogContent += "File: $fileToUpload`r`n"
$vtLogContent += "Upload Status: Failed to upload`r`n"
$vtLogContent += "`r`n"
}
}
} elseif ($userChoice -eq "exit") {
Write-Host "Exiting the process."
return
}
}
# Save final VirusTotal log file with upload results
$vtLogContent | Out-File -FilePath $vtLogFile -Encoding utf8
Write-Host "Updated VirusTotal log file saved to: $vtLogFile"
}
# Function to monitor a folder for new files
function Monitor-Folder {
param (
[string]$Path,
[string[]]$Algorithms,
[string]$ApiKey,
[bool]$Verbose
)
Write-Host "Monitoring folder: $Path"
$fileHashes = @{}
$newFiles = @()
while ($true) {
$files = Get-ChildItem -Path $Path -Recurse -File
$newFilesDetected = $false
foreach ($file in $files) {
if (-not $fileHashes.ContainsKey($file.FullName)) {
$fileHashes[$file.FullName] = $true
$newFiles += $file.FullName
$newFilesDetected = $true
}
}
if ($newFilesDetected) {
Write-Host "`nNew files detected:"
$newFiles | ForEach-Object { Write-Host $_ }
$mergeChoice = Read-Host "`nDo you want to merge all log files before processing new files? (yes/no)"
if ($mergeChoice -eq "yes") {
$formatChoice = Read-Host "In which format do you want to combine the logs? (json/text)"
Combine-LogFiles -Format $formatChoice
}
$processChoice = Read-Host "`nDo you want to process these files? (yes/no)"
if ($processChoice -eq "yes") {
foreach ($newFile in $newFiles) {
Process-Files -Path $newFile -Algorithms $Algorithms -ApiKey $ApiKey -Verbose $Verbose
}
}
$newFiles = @()
}
Start-Sleep -Seconds 10
Write-Host "`nChecking for new files..."
}
}
# Function to combine log files
function Combine-LogFiles {
param (
[string]$Format = "text"
)
$desktopPath = [Environment]::GetFolderPath("Desktop")
$combinedLogFile = Join-Path $desktopPath "VTHashLog.$Format"
$logFiles = Get-ChildItem -Path $desktopPath -Filter "*Log_*.txt"
if ($Format -eq "json") {
$combinedContent = @{
CreatedOn = Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
Logs = @()
}
foreach ($file in $logFiles) {
$fileContent = Get-Content -Path $file.FullName -Raw
$combinedContent.Logs += @{
FileName = $file.Name
Content = $fileContent
}
}
$combinedContent | ConvertTo-Json -Depth 10 | Out-File -FilePath $combinedLogFile -Encoding utf8
}
else {
$combinedContent = "Combined Log File`r`n"
$combinedContent += "Created on: $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')`r`n`r`n"
foreach ($file in $logFiles) {
$combinedContent += "=== Contents of $($file.Name) ===`r`n"
$combinedContent += Get-Content -Path $file.FullName -Raw
$combinedContent += "`r`n`r`n"
}
$combinedContent | Out-File -FilePath $combinedLogFile -Encoding utf8
}
Write-Host "Combined log file saved to: $combinedLogFile"
# Ask if user wants to delete individual log files
$deleteChoice = Read-Host "Do you want to delete the individual log files? (yes/no)"
if ($deleteChoice -eq "yes") {
$logFiles | ForEach-Object { Remove-Item -Path $_.FullName -Force }
Write-Host "Individual log files have been deleted."
}
}
# Main script loop
# [All previous functions remain unchanged]
# Main script loop
while ($true) {
$path = Read-Host "Enter the path of the file or folder"
$algorithmChoice = Read-Host "Which hash algorithms do you want to use? (all/md5/sha1/sha256/sha384/sha512, separate multiple choices with commas)"
$vtApiKey = Read-Host "Enter your VirusTotal API key"
$verboseChoice = Read-Host "Do you want verbose information from VirusTotal? (yes/no)"
$verbose = $verboseChoice -eq "yes"
if ($algorithmChoice -eq "all" -or $algorithmChoice -eq "") {
$algorithms = @("MD5", "SHA1", "SHA256", "SHA384", "SHA512")
}
else {
$algorithms = $algorithmChoice.Split(',') | ForEach-Object { $_.Trim().ToUpper() }
}
if (Test-Path -Path $path -PathType Leaf) {
Process-Files -Path $path -Algorithms $algorithms -ApiKey $vtApiKey -Verbose $verbose
}
elseif (Test-Path -Path $path -PathType Container) {
$initialFiles = Get-ChildItem -Path $path -Recurse -File
Process-Files -Path $path -Algorithms $algorithms -ApiKey $vtApiKey -Verbose $verbose
$monitorChoice = Read-Host "Do you want to monitor this folder for new files? (yes/no)"
if ($monitorChoice -eq "yes") {
Monitor-Folder -Path $path -Algorithms $algorithms -ApiKey $vtApiKey -Verbose $verbose
}
}
else {
Write-Host "Invalid path. Please enter a valid file or folder path."
continue
}
$userChoice = Read-Host "Do you want to scan another folder or exit? (scan/exit)"
if ($userChoice -eq "exit") {
$combineChoice = Read-Host "Do you want to combine all log files? (yes/no)"
if ($combineChoice -eq "yes") {
$formatChoice = Read-Host "In which format do you want to combine the logs? (json/text)"
Combine-LogFiles -Format $formatChoice
}
Write-Host "Exiting the process."
break
}
}