-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmain.go
170 lines (165 loc) · 4.77 KB
/
main.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
package main
import (
"archive/zip"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
"github.com/fatih/color"
)
var (
version string = "1.0.1"
recursiveScanning bool
verboseOutput bool
scanned int
valid int
infectedFiles []string
suspiciousFiles []string
warningColor *color.Color = color.New(color.FgRed)
)
func main() {
helpPage := "(" + version + ") Usage: follina-scanner [OPTION]... [FILE]...\n\n" +
"\t-r, --recursive\t\tRecursively scan files in a directory\n" +
"\t-v, --verbose\t\tDisplay everything that's happening"
if len(os.Args) > 1 {
for index, argument := range os.Args {
if index == 0 {
continue
}
if argument == "-h" || argument == "--help" {
fmt.Println(helpPage)
return
} else if argument == "-r" || argument == "--recursive" {
recursiveScanning = true
} else if argument == "-v" || argument == "--verbose" {
verboseOutput = true
} else {
fileData, err := os.Stat(argument)
if err != nil {
fmt.Printf("[%v] %v\n", argument, err.Error())
continue
}
if fileData.IsDir() {
if recursiveScanning {
err = filepath.Walk(argument,
func(path string, data os.FileInfo, err error) error {
if err != nil {
return err
}
fileData, err := os.Stat(path)
if err == nil {
if !fileData.IsDir() {
scanFile(path)
scanned++
}
return nil
} else {
fmt.Printf("[%v] %v\n", argument, err.Error())
return err
}
})
if err != nil {
fmt.Printf("[%v] %v\n", argument, err.Error())
}
} else {
fmt.Printf("[%v] Is a directory: %v\n", argument, argument)
}
} else {
scanFile(argument)
scanned++
}
}
}
if scanned > 0 {
fmt.Println()
}
fmt.Printf(
color.New(color.FgGreen).Add(color.Bold).Sprintf("Scanned files: ") + strconv.Itoa(scanned) +
color.New(color.FgGreen).Add(color.Bold).Sprintf("\nValid documents: ") + strconv.Itoa(valid) +
color.New(color.FgYellow).Add(color.Bold).Sprintf("\nSuspicious files (%v): ", len(suspiciousFiles)) + strings.Join(suspiciousFiles, ", ") +
color.New(color.FgRed).Add(color.Bold).Sprintf("\nInfected files (%v): ", len(infectedFiles)) + strings.Join(infectedFiles, ", ") + "\n",
)
} else {
fmt.Println(helpPage)
}
}
func scanFile(filePath string) {
document, err := zip.OpenReader(filePath)
if err != nil {
if verboseOutput {
fmt.Printf("[%v] %v\n", filePath, err.Error())
}
return
}
fmt.Printf("[%v] Scanning file as zip...\n", filePath)
valid++
defer document.Close()
for _, zipFile := range document.File {
if strings.Contains(zipFile.Name, "_rels") && !zipFile.FileInfo().IsDir() {
if verboseOutput {
fmt.Printf("[%v] Found %v\n", filePath, zipFile.Name)
}
file, err := zipFile.Open()
if err != nil {
fmt.Printf("[%v] %v\n", filePath, err.Error())
continue
}
defer file.Close()
fileBytes, err := ioutil.ReadAll(file)
if err != nil {
fmt.Printf("[%v] %v\n", filePath, err.Error())
continue
}
if len(strings.TrimSpace(string(fileBytes))) == 0 {
if verboseOutput {
fmt.Printf("[%v] Empty file: %v\n", filePath, zipFile.Name)
}
continue
}
regex := regexp.MustCompile("Target=\"(mhtml:|x-usc:)?https?://.+\\.html.?\"")
matches := regex.FindAll(fileBytes, 1)
if len(matches) == 0 {
if verboseOutput {
fmt.Printf("[%v] No URL found in %v\n", filePath, zipFile.Name)
}
continue
}
match := strings.Replace(string(matches[0]), "mhtml:", "", -1)
url := strings.Split(match[8:len(match)-1], "!")[0]
fmt.Printf("[%v] Found URL in %v: \"%v\"\n", filePath, zipFile.Name, url)
if verboseOutput {
fmt.Printf("[%v] Sending HTTP GET request to %v...\n", filePath, url)
}
client := &http.Client{Timeout: 10 * time.Second}
responseObject, err := client.Get(url)
if err != nil {
warningColor.Printf("[%v] %v\n", filePath, err.Error())
suspiciousFiles = append(suspiciousFiles, filePath)
continue
}
if verboseOutput {
fmt.Printf("[%v] Response received!\n", filePath)
}
responseBytes, err := ioutil.ReadAll(responseObject.Body)
if err != nil {
fmt.Printf("[%v] %v\n", filePath, err.Error())
suspiciousFiles = append(suspiciousFiles, filePath)
continue
}
if strings.Contains(string(responseBytes), "ms-msdt") {
message := fmt.Sprintf("[%v] Found Follina exploit in %v (%v)", filePath, filePath, url)
separator := strings.Repeat("=", len(message))
warningColor.Printf("%v\n%v\n%v\n", separator, message, separator)
infectedFiles = append(infectedFiles, filePath)
} else {
fmt.Printf("[%v] No Follina exploit found\n", filePath)
}
return
}
}
}