forked from wolfvan/YaraRET
-
Notifications
You must be signed in to change notification settings - Fork 2
/
yaraPipe.go
204 lines (155 loc) · 5.88 KB
/
yaraPipe.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
/*
Open Source Initiative OSI - The MIT License (MIT):Licensing
The MIT License (MIT)
Copyright (c) 2013 DutchCoders <http://github.com/dutchcoders/>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package main
import (
"fmt"
// "encoding/gob"
// "net"
"bufio"
"sort"
"strconv"
"log"
"strings"
"os"
yara "github.com/hillu/go-yara"
)
func extractFromYara(rawdisk string, yarafile string, maxsize string, filetype string) {
//var v []result
var offsetExtracted []string
count := 0
count2 := 0
//supported := []string{"threegp","sevenzip","amazonkindleupdate","appleworks5","appleworks6","avi","bmp","bzip","canonraw","crx","dalvik","dat","dba","deb","dmg","doc","elf64","flac","flash","gif","gzip","is","javaclass","jpg","kodakcineon","macho","microsoftOffice","midi","mkv","mp3","mpeg","ost","pcap","pcapng","pdf","pe","png","pds","pst","pyc","rar","rpm","rtf","tape","tar","tarzip","tiff","utf8","vmdk","wasm","wav","woff","xar","xml","xz","zip","zlib"}
fmt.Println("[+] Running Yara..")
resultsToT := searchYara(rawdisk, yarafile)
maxsizeInt, err := strconv.ParseFloat(maxsize, 64)
if err != nil {
fmt.Println(err)
}
if len(resultsToT)==0{
fmt.Println("[!] Not results found")
}else{
fmt.Println("[+] " + strconv.Itoa(len(resultsToT)) + " match found")
fmt.Println("[-] Looking for magic numbers")
resultsMagic := findAllData(rawdisk)
for _, result := range resultsToT{
count2 = count2 +1
fmt.Println("[-] Looking for magic around "+result.rule+"_"+strconv.Itoa(count2))
top := result.offset - uint64(maxsizeInt)
for _, magic := range resultsMagic{
if magic.offset > top {
if magic.offset < result.offset{
if stringInSlice(strconv.Itoa(int(magic.offset)), offsetExtracted){
}else{
count = count +1
dumpRadareFooter(rawdisk, magic.offset, magic.offset+2*uint64(maxsizeInt), strings.Split(result.rule, "_")[0], strconv.Itoa(count))
fmt.Println("[+] Dumped file:\n\t[-] Rule: " + strings.Split(result.rule, "_")[0] + "\n\t[-] Name: " + result.rule + "\n\t[-] FileType: " + strings.Split(magic.rule, "_")[0])
offsetExtracted = append(offsetExtracted, strconv.Itoa(int(magic.offset)))
}
}
}
}
}
}
}
func searchYara(rawdisk string, yarafile string) []result {
var v []result
//var sortResult []result
comp, err := yara.NewCompiler()
f, err := os.Open(yarafile)
if err != nil {
log.Fatalf("Could not open rule file: %s", err)
}
comp.AddFile(f, "")
rules, err := comp.GetRules()
if err != nil {
//log.Fatalf("Failed to initialize YARA compiler: %s", err)
fmt.Println(err)
}
matches, err := rules.ScanFile(rawdisk, 0, 0)
if err != nil {
//fmt.Println("Error en el compilador")
fmt.Println(err)
}
for _, matches := range matches {
for _, stringf := range matches.Strings {
s := result{rule: matches.Rule, offset: stringf.Offset, Data: stringf.Data}
v = append(v, s)
//fmt.Println(s)
//fmt.Println("[+] Match! "+s.rule)
}
}
//sortResult :=sortResultsByOffset(v)
//fmt.Println(v)
//sort.Sort
sort.Sort(offsetSorter(v))
//sortResult =sort.Sort(offsetSorter(v))
//fmt.Println(v)
return v
}
func runHash(rawdisk string, filetype string, hashes string) {
var tmpslice []newResult
var count int
yarafileHeader := workspace + "/magicrules/" + filetype + "_header.yar"
yarafileFooter := workspace + "/magicrules/" + filetype + "_footer.yar"
//var lastResult result
//var newR newResult
resultsHash := searchYara(rawdisk, yarafileHeader)
resultsHash2 := searchYara(rawdisk, yarafileFooter)
sliceNewResults := generateNewResult(resultsHash)
for index, elem := range sliceNewResults {
count = count + 1
result := newResult{Rule: elem.Rule, OffsetHeader: sliceNewResults[index].OffsetHeader, OffsetFooter: resultsHash2[index].offset + uint64(len(resultsHash2[index].Data)-1), Index: elem.Index}
tmpslice = append(tmpslice, result)
}
//fmt.Println("[+] "+strconv.Itoa(count)+" "+filetype+" found")
file, err := os.Open(hashes)
if err != nil {
fmt.Println(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
// This is our buffer now
var lines []string
for scanner.Scan() {
lines = append(lines, scanner.Text())
}
for _, elem3 := range tmpslice {
//fmt.Println("[+] Header "+strconv.Itoa(int(elem3.OffsetHeader))+" Offset:"+strconv.Itoa(int(elem3.OffsetFooter))+" :")
hashTmp := getHashReturn(rawdisk, elem3.OffsetHeader, elem3.OffsetFooter)
for _, elem5 := range lines {
if elem5 == hashTmp {
fmt.Println("[+] Match " + elem5 + " at " + strconv.Itoa(int(elem3.OffsetHeader)))
}
}
}
//fmt.Println(resultsHash)
//fmt.Println(sliceNewResults)
}
func buildRuleOneliner(offset1 string, offset2 string, filetype string) string{
body := returnBody(filetype)
body1 := strings.Replace(body,"COUNT",offset1,-1)
//fmt.Println(body1)
condition := returnCondition(offset1, offset2)
//fmt.Println(condition)
rule := body1+condition
return rule
}