-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalware_analysis_tool.go
195 lines (167 loc) · 4.49 KB
/
malware_analysis_tool.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
package main
import (
"archive/zip"
"bufio"
"crypto/sha256"
"encoding/hex"
"fmt"
"io/ioutil"
"math"
"os"
"path/filepath"
"strings"
)
// Constants for analysis thresholds
const (
highEntropyThreshold = 7.5 // Adjust this value based on your needs
suspiciousKeyword = "exec" // Example of a suspicious keyword
)
// Main function to run the tool
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run malware_analysis_tool.go <path_to_file>")
return
}
filePath := os.Args[1]
fileType := detectFileType(filePath)
fmt.Println("Analyzing file:", filePath)
switch fileType {
case "PE":
analyzePE(filePath)
case "ELF":
analyzeELF(filePath)
case "JAR":
analyzeJAR(filePath)
case "script":
analyzeScript(filePath)
case "Office":
analyzeOffice(filePath)
default:
fmt.Println("Unsupported file type or file is not a recognized executable.")
}
}
// Function to detect the file type based on extension
func detectFileType(filePath string) string {
ext := strings.ToLower(filepath.Ext(filePath))
switch ext {
case ".exe":
return "PE"
case ".elf":
return "ELF"
case ".jar":
return "JAR"
case ".py", ".sh", ".js":
return "script"
case ".docx", ".xlsx":
return "Office"
default:
return "unknown"
}
}
// Analyze PE files
func analyzePE(filePath string) {
fmt.Println("Analyzing PE file:", filePath)
// Implement your PE file analysis logic here
// For now, we'll just calculate its entropy
calculateEntropy(filePath)
}
// Analyze ELF files
func analyzeELF(filePath string) {
fmt.Println("Analyzing ELF file:", filePath)
// Implement your ELF file analysis logic here
// For now, we'll just calculate its entropy
calculateEntropy(filePath)
}
// Analyze JAR files
func analyzeJAR(filePath string) {
fmt.Println("Analyzing JAR file:", filePath)
// Extract and analyze contents of JAR
file, err := zip.OpenReader(filePath)
if err != nil {
fmt.Println("Error opening JAR file:", err)
return
}
defer file.Close()
fmt.Println("Contents of the JAR file:")
for _, f := range file.File {
fmt.Println(" -", f.Name)
// Further analysis can be done on the extracted files
}
}
// Analyze script files
func analyzeScript(filePath string) {
fmt.Println("Analyzing script file:", filePath)
file, err := os.Open(filePath)
if err != nil {
fmt.Println("Error opening script file:", err)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
suspiciousFound := false
for scanner.Scan() {
line := scanner.Text()
if strings.Contains(line, suspiciousKeyword) || strings.Contains(line, "os.system") {
fmt.Println("Suspicious command found:", line)
suspiciousFound = true
}
}
if err := scanner.Err(); err != nil {
fmt.Println("Error reading script file:", err)
}
// Calculate entropy
entropy := calculateEntropy(filePath)
// Check if the file is malicious based on entropy and suspicious commands
if suspiciousFound || entropy > highEntropyThreshold {
fmt.Println("The script file may be malicious.")
} else {
fmt.Println("The script file appears to be safe.")
}
}
// Analyze Office files
func analyzeOffice(filePath string) {
fmt.Println("Analyzing Office file:", filePath)
// This is a placeholder; implement analysis of Office documents
fmt.Println("Office file analysis is not implemented yet.")
}
// Calculate entropy of a file
func calculateEntropy(filePath string) float64 {
data, err := ioutil.ReadFile(filePath)
if err != nil {
fmt.Println("Error reading file:", err)
return 0
}
freq := make(map[byte]int)
for _, b := range data {
freq[b]++
}
var entropy float64
length := float64(len(data))
for _, count := range freq {
p := float64(count) / length
if p > 0 {
entropy -= p * math.Log2(p)
}
}
fmt.Printf("Entropy of the file: %.2f\n", entropy)
// Additional feature: calculate SHA-256 hash of the file
hash := computeSHA256(filePath)
fmt.Println("SHA-256 Hash of the file:", hash)
// Additional feature: check file size
fileInfo, err := os.Stat(filePath)
if err == nil {
fmt.Println("File size (bytes):", fileInfo.Size())
} else {
fmt.Println("Error getting file size:", err)
}
return entropy
}
// Function to compute SHA-256 hash of a file
func computeSHA256(filePath string) string {
data, err := ioutil.ReadFile(filePath)
if err != nil {
return ""
}
hash := sha256.Sum256(data)
return hex.EncodeToString(hash[:])
}