|
| 1 | +#!/usr/bin/env python |
| 2 | +''' |
| 3 | +OWASP ZSC |
| 4 | +https://www.owasp.org/index.php/OWASP_ZSC_Tool_Project |
| 5 | +https://github.com/zscproject/OWASP-ZSC |
| 6 | +http://api.z3r0d4y.com/ |
| 7 | +https://groups.google.com/d/forum/owasp-zsc [ owasp-zsc[at]googlegroups[dot]com ] |
| 8 | +''' |
| 9 | +import binascii |
| 10 | +from core import stack |
| 11 | +from core import color |
| 12 | +from core.alert import info |
| 13 | +from core.compatible import version |
| 14 | +_version = version() |
| 15 | +replace_values_static = {"48 83 ec 20 " : "sub $0x20,%rsp" |
| 16 | + "48 83 e4 f0 " : "and $0xfffffffffffffff0,%rsp" |
| 17 | + ,"65 4c 8b 24 25 60 00" : "mov %gs:0x60,%r12" |
| 18 | + ,"4d 8b 64 24 18 " : "mov 0x18(%r12),%r12" |
| 19 | + ,"4d 8b 64 24 20 " : "mov 0x20(%r12),%r12" |
| 20 | + ,"4d 8b 24 24 " : "mov (%r12),%r12" |
| 21 | + ,"4d 8b 7c 24 20 " : "mov 0x20(%r12),%r15" |
| 22 | + ,"4d 8b 24 24 " : "mov (%r12),%r12" |
| 23 | + ,"4d 8b 64 24 20 " : "mov 0x20(%r12),%r12" |
| 24 | + ,"ba 98 fe 8a 0e " : "mov $0xe8afe98,%edx" |
| 25 | + ,"4c 89 e1 " : "mov %r12,%rcx" |
| 26 | + ,"4d 89 e4 " : "mov %r12,%r12" |
| 27 | + ,"e8 2a 00 00 00 " : "callq 0x401067" |
| 28 | + ,"eb 1a " : "jmp 0x401059" |
| 29 | + ,"59 " : "pop %rcx" |
| 30 | + ,"ba 01 00 00 00 " : "mov $0x1,%edx" |
| 31 | + ,"ff d0 " : "callq *%rax" |
| 32 | + ,"ba 70 cd 3f 2d " : "mov $0x2d3fcd70,%edx" |
| 33 | + ,"4c 89 f9 " : "mov %r15,%rcx" |
| 34 | + ,"e8 13 00 00 00 " : "callq 0x401067" |
| 35 | + ,"48 31 c9 " : "xor %rcx,%rcx" |
| 36 | + ,"ff d0 " : "callq *%rax" |
| 37 | + ,"e8 e1 ff ff ff " : "callq 0x40103f" |
| 38 | + ,"63 61 6c " : "movslq 0x6c(%rcx),%esp" |
| 39 | + ,"63 2e " : "movslq (%rsi),%ebp" |
| 40 | + ,"65 78 65 " : "gs js 0x4010cb" |
| 41 | + ,"00 49 89 " : "add %cl,-0x77(%rcx)" |
| 42 | + ,"cd 67 " : "int $0x67" |
| 43 | + ,"41 8b 45 3c " : "mov 0x3c(%r13),%eax" |
| 44 | + ,"67 45 8b b4 05 88 00" : "mov 0x88(%r13d,%eax,1),%r14d" |
| 45 | + ,"45 01 ee " : "add %r13d,%r14d" |
| 46 | + ,"67 45 8b 56 18 " : "mov 0x18(%r14d),%r10d" |
| 47 | + ,"67 41 8b 5e 20 " : "mov 0x20(%r14d),%ebx" |
| 48 | + ,"44 01 eb " : "add %r13d,%ebx" |
| 49 | + ,"67 e3 3f " : "jecxz 0x4010ca" |
| 50 | + ,"41 ff ca " : "dec %r10d" |
| 51 | + ,"67 42 8b 34 93 " : "mov (%ebx,%r10d,4),%esi" |
| 52 | + ,"44 01 ee " : "add %r13d,%esi" |
| 53 | + ,"31 ff " : "xor %edi,%edi" |
| 54 | + ,"31 c0 " : "xor %eax,%eax" |
| 55 | + ,"fc " : "cld" |
| 56 | + ,"ac " : "lods %ds:(%rsi),%al" |
| 57 | + ,"84 c0 " : "test %al,%al" |
| 58 | + ,"74 07 " : "je 0x4010a7" |
| 59 | + ,"c1 cf 0d " : "ror $0xd,%edi" |
| 60 | + ,"01 c7 " : "add %eax,%edi" |
| 61 | + ,"eb f4 " : "jmp 0x40109b" |
| 62 | + ,"39 d7 " : "cmp %edx,%edi" |
| 63 | + ,"75 dd " : "jne 0x401088" |
| 64 | + ,"67 41 8b 5e 24 " : "mov 0x24(%r14d),%ebx" |
| 65 | + ,"44 01 eb " : "add %r13d,%ebx" |
| 66 | + ,"31 c9 " : "xor %ecx,%ecx" |
| 67 | + ,"66 67 42 8b 0c 53 " : "mov (%ebx,%r10d,2),%cx" |
| 68 | + ,"67 41 8b 5e 1c " : "mov 0x1c(%r14d),%ebx" |
| 69 | + ,"44 01 eb " : "add %r13d,%ebx" |
| 70 | + ,"67 8b 04 8b " : "mov (%ebx,%ecx,4),%eax" |
| 71 | + ,"44 01 e8 " : "add %r13d,%eax" |
| 72 | + ,"c3 " : "retq", |
| 73 | + } |
| 74 | + |
| 75 | + |
| 76 | +def convert(shellcode): |
| 77 | + shellcode = shellcode.replace('\n\n','\n').replace('\n\n','\n').replace(' ',' ').replace(' ',' ').replace(' ',' ') |
| 78 | + for data in replace_values_static: |
| 79 | + shellcode = shellcode.replace(data,replace_values_static[data]) |
| 80 | + |
| 81 | + new_shellcode = shellcode.rsplit('\n') |
| 82 | + last = 0 |
| 83 | + for line in new_shellcode: |
| 84 | + if 'push $0x' in line: |
| 85 | + if len(line) is 15: |
| 86 | + if _version is 2: |
| 87 | + rep = str('68') + stack.st(str(binascii.a2b_hex(str('0') + str(line.rsplit('$0x')[1])))) |
| 88 | + if _version is 3: |
| 89 | + rep = str('68') + stack.st(str(binascii.a2b_hex(str('0') + line.rsplit('$0x')[1].encode('latin-1')).decode('latin-1'))) |
| 90 | + shellcode = shellcode.replace(line,rep) |
| 91 | + if len(line) is 16: |
| 92 | + if _version is 2: |
| 93 | + rep = str('68') + stack.st(str(binascii.a2b_hex(str(line.rsplit('$0x')[1])))) |
| 94 | + if _version is 3: |
| 95 | + rep = str('68') + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].encode('latin-1')).decode('latin-1'))) |
| 96 | + shellcode = shellcode.replace(line,rep) |
| 97 | + |
| 98 | + if 'mov $0x' in line: |
| 99 | + if '%ecx' in line.rsplit(',')[1]: |
| 100 | + if _version is 2: |
| 101 | + rep = str('b9') + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].rsplit(',')[0]))) |
| 102 | + if _version is 3: |
| 103 | + rep = str('b9') + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].rsplit(',')[0].encode('latin-1')).decode('latin-1'))) |
| 104 | + shellcode = shellcode.replace(line,rep) |
| 105 | + |
| 106 | + if 'mov 0x' in line: |
| 107 | + if '%eax' in line.rsplit(',')[0] and '%eax' in line.rsplit(',')[1]: |
| 108 | + rep = str('8b 40') + stack.toHex(line.rsplit('0x')[1].rsplit('(')[0]) |
| 109 | + shellcode = shellcode.replace(line,rep) |
| 110 | + if '%eax' in line.rsplit(',')[0] and '%esi' in line.rsplit(',')[1]: |
| 111 | + rep = str('8b 70') + stack.toHex(line.rsplit('0x')[1].rsplit('(')[0]) |
| 112 | + shellcode = shellcode.replace(line,rep) |
| 113 | + if '%eax' in line.rsplit(',')[0] and '%ebx' in line.rsplit(',')[1]: |
| 114 | + rep = str('8b 58') + stack.toHex(line.rsplit('0x')[1].rsplit('(')[0]) |
| 115 | + shellcode = shellcode.replace(line,rep) |
| 116 | + if '%ebx' in line.rsplit(',')[0] and '%edx' in line.rsplit(',')[1]: |
| 117 | + rep = str('8b 53') + stack.toHex(line.rsplit('0x')[1].rsplit('(')[0]) |
| 118 | + shellcode = shellcode.replace(line,rep) |
| 119 | + if '%edx' in line.rsplit(',')[0] and '%edx' in line.rsplit(',')[1]: |
| 120 | + rep = str('8b 52') + stack.toHex(line.rsplit('0x')[1].rsplit('(')[0]) |
| 121 | + shellcode = shellcode.replace(line,rep) |
| 122 | + if '%edx' in line.rsplit(',')[0] and '%esi' in line.rsplit(',')[1]: |
| 123 | + rep = str('8b 72') + stack.toHex(line.rsplit('0x')[1].rsplit('(')[0]) |
| 124 | + shellcode = shellcode.replace(line,rep) |
| 125 | + |
| 126 | + if 'mov $0x' in line and len(line.rsplit('$0x')[1].rsplit(',')[0]) == 4: |
| 127 | + if '%cx' in line: |
| 128 | + if _version is 2: |
| 129 | + rep = str('66 b9') + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].rsplit(',')[0]))) |
| 130 | + if _version is 3: |
| 131 | + rep = str('66 b9') + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].rsplit(',')[0].encode('latin-1')).decode('latin-1'))) |
| 132 | + shellcode = shellcode.replace(line,rep) |
| 133 | + if '%dx' in line: |
| 134 | + if _version is 2: |
| 135 | + rep = str('66 ba') + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].rsplit(',')[0]))) |
| 136 | + if _version is 3: |
| 137 | + rep = str('66 ba') + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].rsplit(',')[0].encode('latin-1')).decode('latin-1'))) |
| 138 | + shellcode = shellcode.replace(line,rep) |
| 139 | + |
| 140 | + if 'add' in line: |
| 141 | + if '$0x' in line: |
| 142 | + if '%esp' in line.rsplit(',')[1]: |
| 143 | + if _version is 2: |
| 144 | + rep = str('83 c4') + stack.st(str(binascii.a2b_hex(stack.toHex(line.rsplit('$0x')[1].rsplit(',')[0])))) |
| 145 | + if _version is 3: |
| 146 | + rep = str('83 c4') + stack.st(str(binascii.a2b_hex(stack.toHex(line.rsplit('$0x')[1].rsplit(',')[0]).encode('latin-1')).decode('latin-1'))) |
| 147 | + shellcode = shellcode.replace(line,rep) |
| 148 | + |
| 149 | + if 'cmpl' in line: |
| 150 | + if '(%eax)' == line.rsplit(',')[1]: |
| 151 | + if _version is 2: |
| 152 | + rep = str('81 38') + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].rsplit(',')[0]))) |
| 153 | + if _version is 3: |
| 154 | + rep = str('81 38') + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].rsplit(',')[0].encode('latin-1')).decode('latin-1'))) |
| 155 | + shellcode = shellcode.replace(line,rep) |
| 156 | + if '0x' in line.rsplit(',')[1]: |
| 157 | + if '%eax' in line: |
| 158 | + if _version is 2: |
| 159 | + rep = str('81 78') + stack.st(str(binascii.a2b_hex(stack.toHex(line.rsplit(',0x')[1].rsplit('(')[0])))) + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].rsplit(',')[0]))) |
| 160 | + if _version is 3: |
| 161 | + rep = str('81 78') + stack.st(str(binascii.a2b_hex(stack.toHex(line.rsplit(',0x')[1].rsplit('(')[0]).encode('latin-1')).decode('latin-1'))) + stack.st(str(binascii.a2b_hex(line.rsplit('$0x')[1].rsplit(',')[0].encode('latin-1')).decode('latin-1'))) |
| 162 | + shellcode = shellcode.replace(line,rep) |
| 163 | + |
| 164 | + if 'jne' in line: |
| 165 | + rep = str('75') + hex(int('f4', 16) - last*9)[2:] |
| 166 | + shellcode = shellcode.replace(line,rep,1) |
| 167 | + last += 1 |
| 168 | + shellcode = stack.shellcoder(shellcode.replace('\n','').replace(' ','')) |
| 169 | + return shellcode |
| 170 | + |
0 commit comments