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
| def generate_shikata_block(shellcode): import random if ARCH == "x86": regs = [ "eax","ebx","ecx","edx","esi","edi" ] stack_base = "ebp" stack_head = "esp" addr_size = 0x4 else: regs = [ "rax","rbx","rcx","rdx","rsi","rdi" ] stack_base = "rbp" stack_head = "rsp" addr_size = 0x8 fpus = [] fpus += [ bytes([0xd9,i]) for i in range(0xe8,0xee+1) ] fpus += [ bytes([0xd9,i]) for i in range(0xc0,0xcf+1) ] fpus += [ bytes([0xda,i]) for i in range(0xc0,0xdf+1) ] fpus += [ bytes([0xdb,i ]) for i in range(0xc0,0xcf+1) ] fpus += [ bytes([0xdd,i ]) for i in range(0xc0,0xcf+1) ]
ks = k.Ks(k.KS_ARCH_X86,MODE)
code = [] def append_code(code,asm=None,bytes_code=None,compile=True): ''' code = [ { "index":"当前指令偏移", "asm":"助记符", "bytes_code":[12,34] # 编译后的整形 } ] '''
if not len(code): index = 0 else: last = code[-1] index = last["index"] + len(last["bytes_code"]) if not compile: code.append({ "index":index, "asm":asm, "bytes_code":[i for i in bytes_code] }) else: try: encoding, count = ks.asm(asm) code.append({ "index":index, "asm":asm, "bytes_code":encoding })
except k.KsError as e: print("ERROR: %s" %e) return [] return code
code = append_code( code, asm="mov {},{}".format( stack_base,stack_head ) ) code = append_code( code, asm = "sub {},{}".format( stack_head, addr_size * 0x4 ) ) reg_caches = []
reg_caches.extend( ["rcx","ecx"] )
reg_1 = random.choice(reg_caches) while reg_1 in reg_caches: reg_1 = random.choice(regs)
code = append_code( code, asm = "mov {},{}".format( reg_1,stack_head) )
code = append_code( code, asm = "fpus", bytes_code = random.choice( fpus ), compile=False )
location_ss = random.randint(3,12) code = append_code( code, asm = "fnstenv [{} - {}]".format(reg_1,hex(location_ss * 4)) ) code = append_code( code, asm="sub {},{}".format( stack_head,hex( (location_ss - 3)*4 ) ) ) code = append_code( code, asm = "pop {}".format(reg_1) )
key_table = [ i for i in range(0x80,0xFF) ] key = bytes([ random.choice( key_table ) for i in range(4) ]) print("[*] the decode key is: {}.".format(key)) key_int = struct.unpack("<I",key)[0]
reg_2 = random.choice( reg_caches ) while reg_2 in reg_caches: reg_2 = random.choice(regs) if reg_2.startswith("r"): reg_2 = reg_2.replace("r","e") code = append_code( code, asm="mov {},{}".format(reg_2,key_int ) )
code = append_code( code, asm="xor ecx,ecx" ) code_length = len(shellcode) print("[*] len of shellcode : {}.".format(code_length)) code_length += 4 + (4 - (code_length & 3)) & 3 print("[*] encode length is: {}.".format(code_length)) code_length //= 4
if (code_length <= 255): code = append_code( code, asm="mov {},{}".format("cl",code_length) ) elif (code_length <= 65536): code = append_code( code, asm="mov {},{}".format("ecx",code_length) ) dd = 0x23 ''' # 查 intel 手册得知 xor [reg+offset],reg # 此变长指令在 offset <= 0x7F 为定长三字节 ''' code = append_code( code, asm="decode: xor [{}+{}],{}".format( reg_1,dd,reg_2 ) )
decode_label = code[-1]["index"]
code = append_code( code, asm = "add {},4".format(reg_1) )
current_index = code[-1]["index"] + len( code[-1]["bytes_code"] ) code = append_code( code, asm="loop decode", bytes_code=b"\xe2" + bytes( [0xFF - (current_index + 2 - decode_label) + 1 ] ), compile=False )
all_code_length = code[-1]["index"] + len(code[-1]["bytes_code"]) fpus_addr = 0
print("[*] original code:") for t,i in enumerate(code): print("\t{}:\t{}\t\t{}".format(i["index"],i["asm"],i["bytes_code"])) asm = i["asm"] index = i["index"] if "fpus" in asm: fpus_addr = index if "decode" in asm: code[t]["bytes_code"][2] = all_code_length - fpus_addr - ( code_length * 4 - len(shellcode) ) break
print("[*] fix code:")
decodeSub = [] for t,i in enumerate(code): print( "\t{}:\t{}\t\t{}".format(i["index"],i["asm"],i["bytes_code"])) decodeSub += i["bytes_code"] return decodeSub,shellcode,code_length*4,key
|