-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamsi_test.go
103 lines (89 loc) · 2.35 KB
/
amsi_test.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
package amsi_test
import (
"fmt"
"os"
"github.com/jonas-koeritz/amsi"
)
func Example_scanBuffer() {
// Read the file contents into a byte slice
data, err := os.ReadFile("test.exe")
if err != nil {
fmt.Printf("Failed to read input file: %s\n", err)
return
}
// Initialize an AMSI context
amsiContext, err := amsi.Initialize("amsiscan")
if err != nil {
fmt.Println(err)
return
}
defer amsiContext.Close()
// Open an AMSI session, this is not needed and the context can be used to scan data already
amsiSession, err := amsiContext.OpenSession()
if err != nil {
fmt.Println(err)
return
}
defer amsiSession.Close()
// Provide the data to AMSI through ScanBuffer
result, err := amsiSession.ScanBuffer(data, "Content name")
if err != nil {
fmt.Println(err)
return
}
// Check if the result considers the data to be malware
if result.IsMalware() {
fmt.Printf("File contains malware. AMSI result: %d\n", result)
} else {
fmt.Printf("File seems to be clean. AMSI result: %d\n", result)
}
}
func Example_scanString() {
// Initialize an AMSI context
amsiContext, err := amsi.Initialize("amsiscan")
if err != nil {
fmt.Println(err)
return
}
defer amsiContext.Close()
// Open an AMSI session
amsiSession, err := amsiContext.OpenSession()
if err != nil {
fmt.Println(err)
return
}
defer amsiSession.Close()
// Scan a string for malware through ScanString
result, err := amsiSession.ScanString("<string to be scanned for malware>", "It's just a string")
if err != nil {
fmt.Println(err)
return
}
// Check if the result considers the data to be malware
if result.IsMalware() {
fmt.Printf("File contains malware. AMSI result: %d\n", result)
} else {
fmt.Printf("File seems to be clean. AMSI result: %d\n", result)
}
}
func Example_withoutSession() {
// Initialize an AMSI context
amsiContext, err := amsi.Initialize("amsiscan")
if err != nil {
fmt.Println(err)
return
}
defer amsiContext.Close()
// Scan a string for malware through ScanString
result, err := amsiContext.ScanString("<string to be scanned for malware>", "It's just a string", nil)
if err != nil {
fmt.Println(err)
return
}
// Check if the result considers the data to be malware
if result.IsMalware() {
fmt.Printf("File contains malware. AMSI result: %d\n", result)
} else {
fmt.Printf("File seems to be clean. AMSI result: %d\n", result)
}
}