-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaliciousFileDetector.go
603 lines (520 loc) · 17.2 KB
/
MaliciousFileDetector.go
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// This scripts detects suspicious and probably malicious files
// used to attack your system
/*
Copyright (C) 2023 Maurice Lambert
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Compilation on Linux:
// env GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -trimpath -o GetMaliciousFiles.exe GetMaliciousFiles.go
package main
import (
"os"
"fmt"
"math"
"regexp"
"unsafe"
"strings"
"syscall"
"io/ioutil"
"path/filepath"
"container/list"
"encoding/binary"
)
var root_startswith_whitelist []string = []string{
"C:\\Drivers",
"C:\\Program Files",
"C:\\Program Files (x86)",
"C:\\Windows",
"C:\\$Recycle.Bin",
"C:\\Drivers",
"C:\\Intel",
"C:\\PerfLogs",
"C:\\Recovery",
"C:\\temp",
"C:\\Users",
"C:\\Config.Msi",
}
var suspicious_files_counter uint = 0
var malicious_files_counter uint = 0
var analyzed_files_counter uint = 0
var discover_files map[string]bool
var function uintptr = 0
var pourcent byte = 1
/*
This function checks if path is in directories whitelist.
*/
func check_whitelist_paths (path string) bool {
for _, whitelisted_path := range root_startswith_whitelist {
if (strings.HasPrefix(path, whitelisted_path)) {
return true
}
}
return false
}
/*
This function returns matching filenames.
*/
func get_files_by_patterns(pattern string) []string {
matches, error := filepath.Glob(pattern)
if error != nil {
fmt.Fprintf(os.Stderr, "Error getting files: %v\n", error)
return nil
}
files := make([]string, len(matches))
index := 0
for _, filename := range matches {
fileInfo, error := os.Stat(filename)
if error != nil {
fmt.Fprintf(os.Stderr, "Error getting stat file: %v\n", error)
continue
}
if !fileInfo.IsDir() && fileInfo.Size() > 256 {
_, ok := discover_files[filename]
if !ok {
files[index] = filename
index += 1
}
}
}
return files
}
/*
This function returns all suspicious filenames in path and subdirectories.
*/
func get_suspicious_filename(
path string, extensions []string, suspicious_files []string,
) []string {
files := get_subfiles(path)
for file := files.Front(); file != nil; file = file.Next() {
filename := file.Value.(string)
_, ok := discover_files[filename]
if ok {
continue
}
analyzed_files_counter += 1
for _, extension := range extensions {
if strings.HasSuffix(filename, extension) {
suspicious_files = append(suspicious_files, filename)
discover_files[filename] = true
}
}
}
return suspicious_files
}
/*
This function matchs dangerous files (executables, scripts, librairies)
in temp directories, data directories and public directories.
*/
func get_suspicious_file_by_extension () []string {
suspicious_files := []string{}
paths := []string{
"C:\\temp\\",
"C:\\ProgramData\\",
"C:\\Users\\Public\\",
}
extensions := []string{
".dll", ".exe", ".ps1", ".vbs", ".js", ".cmd", ".bat",
}
paths_, error := filepath.Glob("C:\\Users\\*\\AppData\\Local\\Temp\\")
if error != nil {
fmt.Fprintf(os.Stderr, "Error getting files: %v\n", error)
return nil
}
paths = append(paths, paths_...)
paths_length := len(paths)
for index, path := range paths {
pourcent = byte(int(index * 25 / paths_length))
messagef(
fmt.Sprintf("Scanning %s...", path),
"INFO",
)
suspicious_files = get_suspicious_filename(
path, extensions, suspicious_files,
)
}
return suspicious_files
}
/*
This function returns executables files in specific directories.
*/
func get_executable_files(paths []string) []string {
executable_files := []string{}
paths_length := len(paths)
for index, path := range paths {
pourcent = byte(int(index * 13 / paths_length) + 50)
messagef(
fmt.Sprintf("Scanning %s...", path),
"INFO",
)
files := get_subfiles(path)
for file := files.Front(); file != nil; file = file.Next() {
filename := file.Value.(string)
_, ok := discover_files[filename]
if ok {
continue
}
analyzed_files_counter += 1
if is_executable(filename) {
executable_files = append(executable_files, filename)
discover_files[filename] = true
}
}
}
return executable_files
}
/*
This function returns executables files in specific root directories.
*/
func get_root_executable_files(executable_files []string) []string {
root_directories, error := os.ReadDir("C:\\")
if error != nil {
fmt.Fprintf(os.Stderr, "Error getting files: %v\n", error)
return executable_files
}
paths_length := len(root_directories)
for index, path := range root_directories {
pathname := "C:\\" + path.Name()
if check_whitelist_paths(pathname) {
continue
}
pourcent = byte(int(index * 12 / paths_length) + 63)
messagef(
fmt.Sprintf("Scanning %s...", pathname),
"INFO",
)
files := get_subfiles(pathname)
for file := files.Front(); file != nil; file = file.Next() {
filename := file.Value.(string)
_, ok := discover_files[filename]
if ok {
continue
}
analyzed_files_counter += 1
if is_executable(filename) {
executable_files = append(executable_files, filename)
discover_files[filename] = true
}
}
}
return executable_files
}
/*
This function returns executable files in TEMP directories, data
directories, root directories and public directories.
*/
func get_suspicious_executable_files () []string {
paths := []string{
"C:\\temp",
"C:\\Users\\Public",
"C:\\ProgramData",
}
paths_, error := filepath.Glob("C:\\Users\\*\\AppData\\Roaming")
if error != nil {
fmt.Fprintf(os.Stderr, "Error getting files: %v\n", error)
return nil
}
paths = append(paths, paths_...)
paths_, error = filepath.Glob("C:\\Users\\*\\AppData\\Local")
if error != nil {
fmt.Fprintf(os.Stderr, "Error getting files: %v\n", error)
return nil
}
paths = append(paths, paths_...)
executable_files := get_executable_files(paths)
executable_files = get_root_executable_files(executable_files)
return executable_files
}
/*
This function returns files recursively.
*/
func get_subfiles(directory string) *list.List {
files := list.New()
filepath.Walk(
directory,
func(path string, file os.FileInfo, error error) error {
if error != nil || file.Size() < 256 {
if error != nil {
fmt.Fprintf(
os.Stderr,
"Error getting path recursively (%s): %v\n",
path,
error,
)
}
return nil
}
if !file.IsDir() {
files.PushFront(path)
}
return nil
},
)
return files
}
/*
This function tests if file is a Windows executable file.
*/
func is_executable(filename string) bool {
file, error := os.Open(filename)
if error != nil {
fmt.Fprintf(os.Stderr, "Error opening file %s: %v\n", filename, error)
return false
}
defer file.Close()
bytes := make([]byte, 64)
_, error = file.Read(bytes)
if error != nil {
fmt.Fprintf(os.Stderr, "Error reading file %s: %v\n", filename, error)
return false
}
if bytes[0] != 0x4d || bytes[1] != 0x5a {
return false
}
_, error = file.Seek(
int64(binary.LittleEndian.Uint32(bytes[0x3c:0x40])), 0,
)
if error != nil {
fmt.Fprintf(os.Stderr, "Error seeking file %s: %v\n", filename, error)
return false
}
bytes = make([]byte, 4)
_, error = file.Read(bytes)
if error != nil {
fmt.Fprintf(os.Stderr, "Error reading file %s: %v\n", filename, error)
return false
}
if bytes[0] != 0x50 || bytes[1] != 0x45 ||
bytes[2] != 0x00 || bytes[3] != 0x00 {
return false
}
return true
}
/*
This function checks for long encoded strings in executable.
*/
func have_long_encoded_data(content []byte) bool {
/*regex_base16, error := regexp.Compile("[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}[0-9a-fA-F]{255}")
if error != nil {
fmt.Fprintf(os.Stderr, "Error compiling base16 regex: %v\n", error)
return false
}
regex_base32, error := regexp.Compile("[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}[A-Z0-7]{255}")
if error != nil {
fmt.Fprintf(os.Stderr, "Error compiling base32 regex: %v\n", error)
return false
}*/ // base64 regex match base32 and base16, there is few executables that match theses regex so delete this regex is probably more optimized.
regex_base64, error := regexp.Compile("[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}[A-Za-z0-9+/_-]{255}")
if error != nil {
fmt.Fprintf(os.Stderr, "Error compiling base64 regex: %v\n", error)
return false
}
/*if regex_base16.FindIndex(content) != nil {
return true
}
if regex_base32.FindIndex(content) != nil {
return true
}*/
if regex_base64.FindIndex(content) != nil {
return true
}
return false
}
/*
This function checks file for very high entropy.
*/
func have_suspicious_entropy(content []byte) bool {
frequencies := make(map[byte]int)
for _, character := range content {
frequencies[character]++
}
var score float64
length := len(content)
for _, frequency := range frequencies {
p := float64(frequency) / float64(length)
score += -p * math.Log2(p)
}
if score > 7.2 {
return true
} else {
return false
}
}
/*
This function checks content for malicious payload.
*/
func have_suspicious_content (filename string, executable bool) bool {
if executable || is_executable(filename) {
content, error := ioutil.ReadFile(filename)
if error != nil {
fmt.Fprintf(
os.Stderr, "Error reading file %s: %v\n", filename, error,
)
return false
}
if have_suspicious_entropy(content) {
return true
}
if have_long_encoded_data(content) {
return true
}
} else {
fileInfo, error := os.Stat(filename)
if error != nil {
fmt.Fprintf(os.Stderr, "Error getting stat file: %v\n", error)
return false
}
if fileInfo.Size() > 100000 {
return true
}
}
return false
}
/*
This function loads the TerminalMessages DLL.
*/
func getTerminalMessages() (uintptr, error) {
executable, error := os.Executable()
if error == nil {
terminalMessages, error := syscall.LoadLibrary(
filepath.Join(filepath.Dir(executable), "TerminalMessages.dll"),
)
if error == nil {
function, error = syscall.GetProcAddress(
terminalMessages, "messagef",
)
if error == nil {
return function, nil
}
}
}
directory, error1 := os.Getwd()
if error1 == nil {
terminalMessages, error1 := syscall.LoadLibrary(
filepath.Join(directory, "TerminalMessages.dll"),
)
if error1 == nil {
function, error = syscall.GetProcAddress(
terminalMessages, "messagef",
)
if error1 == nil {
return function, nil
}
}
}
return 0, error
}
/*
This function is a wrapper for TerminalMessages.messagef function.
*/
func messagef(message string, state string) {
if function == 0 {
if state != "INFO" {
fmt.Println(message)
}
return
}
var add_progress_bar byte = 1
if pourcent == 0 {
add_progress_bar = 0
}
messagebytes := append([]byte(message), 0)
statebytes := append([]byte(state), 0)
_, _, error := syscall.Syscall9(
function,
8,
uintptr(unsafe.Pointer(&messagebytes[0])),
uintptr(unsafe.Pointer(&statebytes[0])),
uintptr(pourcent),
0,
0,
0,
uintptr(add_progress_bar),
0,
0,
)
if error != 0 && state != "INFO" {
fmt.Println(message)
}
}
func parse_args() {
if len(os.Args) != 2 && len(os.Args) != 1 {
fmt.Fprintf(os.Stderr, "USAGES: %s [-c/--no-color]\n", os.Args[0])
os.Exit(1)
}
var error error = nil
if len(os.Args) == 2 {
if os.Args[1] != "-c" && os.Args[1] != "--no-color" {
fmt.Fprintf(os.Stderr, "USAGES: %s [-c/--no-color]\n", os.Args[0])
os.Exit(1)
}
} else {
function, error = getTerminalMessages()
}
if error != nil {
fmt.Fprintf(os.Stderr, "Warning loading DLL: %v\n", error)
}
}
/*
This is the main fonction to run this script.
*/
func main() {
parse_args()
discover_files = make(map[string]bool)
messagef("Start suspicious filenames...", "INFO")
files := get_suspicious_file_by_extension()
file_length := len(files)
for index, file := range files {
pourcent = byte(index * 25 / file_length) + 25
if have_suspicious_content(file, false) {
messagef(fmt.Sprintf("Malicious: %s", file), "ERROR")
malicious_files_counter += 1
} else {
messagef(fmt.Sprintf("Suspicious: %s", file), "NOK")
suspicious_files_counter += 1
}
}
pourcent = byte(50)
messagef("Search suspicious executables...", "INFO")
files = get_suspicious_executable_files()
file_length = len(files)
for index, file := range files {
pourcent = byte(index * 25 / file_length) + 75
if (strings.HasSuffix(file, ".exe") ||
strings.HasSuffix(file, ".mui") ||
strings.HasSuffix(file, ".so") ||
strings.HasSuffix(file, ".sys") ||
strings.HasSuffix(file, ".vdm") ||
strings.HasSuffix(file, ".lkg") ||
strings.HasSuffix(file, ".efi") ||
strings.HasSuffix(file, ".pyd") ||
strings.HasSuffix(file, ".node") ||
strings.HasSuffix(file, ".dll")) &&
!have_suspicious_content(file, true) {
messagef(fmt.Sprintf("Suspicious: %s", file), "NOK")
suspicious_files_counter += 1
} else {
messagef(fmt.Sprintf("Malicious: %s", file), "ERROR")
malicious_files_counter += 1
}
}
pourcent = byte(100)
messagef(
fmt.Sprintf(
"Analyzed files: %d, Detected files: %d, Suspicious files: %d, Malicious files: %d",
analyzed_files_counter,
len(discover_files),
suspicious_files_counter,
malicious_files_counter,
),
"OK",
)
}