github.com/n00py/Slackor@v0.0.0-20200610224921-d007fcea1740/impacket/tests/misc/test_dcerpc_v5_ndr.py (about) 1 from __future__ import print_function 2 import unittest 3 from binascii import hexlify, unhexlify 4 5 from impacket.dcerpc.v5.ndr import (NDRSTRUCT, NDRLONG, NDRSHORT, 6 NDRUniFixedArray, 7 NDRUniVaryingArray, 8 NDRUniConformantVaryingArray, 9 NDRVaryingString, 10 NDRConformantVaryingString, 11 NDRPOINTERNULL) 12 13 def hexl(b): 14 hexstr = str(hexlify(b).decode('ascii')) 15 return ' '.join([hexstr[i:i+8] for i in range(0,len(hexstr),8)]) 16 17 class NDRTest(unittest.TestCase): 18 def create(self,data = None, isNDR64 = False): 19 if data is not None: 20 return self.theClass(data, isNDR64 = isNDR64) 21 else: 22 return self.theClass(isNDR64 = isNDR64) 23 24 def do_test(self, isNDR64 = False): 25 a = self.create(isNDR64 = isNDR64) 26 self.populate(a) 27 # packing... 28 a_str = a.getData() 29 self.check_data(a_str, isNDR64) 30 # unpacking... 31 b = self.create(a_str, isNDR64 = isNDR64) 32 b_str = b.getData() 33 self.assertEqual(b_str, a_str) 34 35 def test_false(self): 36 self.do_test(False) 37 38 def test_true(self): 39 # Now the same tests but with NDR64 40 self.do_test(True) 41 42 def check_data(self, a_str, isNDR64): 43 try: 44 hexData = getattr(self, 'hexData64' if isNDR64 else 'hexData') 45 # Regression check 46 self.assertEqual(hexl(a_str), hexData) 47 except AttributeError: 48 # Show result, to aid adding regression check 49 print(self.__class__.__name__, isNDR64, hexl(a_str)) 50 51 class TestUniFixedArray(NDRTest): 52 class theClass(NDRSTRUCT): 53 structure = ( 54 ('Array', NDRUniFixedArray), 55 ) 56 def populate(self, a): 57 a['Array'] = b'12345678' 58 hexData = '31323334 35363738' 59 hexData64 = hexData 60 61 class TestStructWithPad(NDRTest): 62 class theClass(NDRSTRUCT): 63 structure = ( 64 ('long', NDRLONG), 65 ('short', NDRSHORT), 66 ) 67 def populate(self, a): 68 a['long'] = 0xaa 69 a['short'] = 0xbb 70 hexData = 'aa000000 bb00' 71 hexData64 = hexData 72 73 #class TestUniConformantArray(NDRTest): 74 # class theClass(NDRCall): 75 # structure = ( 76 # ('Array', PNDRUniConformantArray), 77 # ('Array2', PNDRUniConformantArray), 78 # ) 79 # def __init__(self, data = None,isNDR64 = False): 80 # NDRCall.__init__(self, None, isNDR64) 81 # self.fields['Array'].fields['Data'].item = RPC_UNICODE_STRING 82 # self.fields['Array2'].fields['Data'].item = RPC_UNICODE_STRING 83 # if data is not None: 84 # self.fromString(data) 85 # 86 # def populate(self, a): 87 # array = [] 88 # strstr = RPC_UNICODE_STRING() 89 # strstr['Data'] = 'ThisIsMe' 90 # array.append(strstr) 91 # strstr = RPC_UNICODE_STRING() 92 # strstr['Data'] = 'ThisIsYou' 93 # array.append(strstr) 94 # a['Array'] = array 95 # a['Array2'] = array 96 97 class TestUniVaryingArray(NDRTest): 98 class theClass(NDRSTRUCT): 99 structure = ( 100 ('Array', NDRUniVaryingArray), 101 ) 102 def populate(self, a): 103 a['Array'] = b'12345678' 104 hexData = '00000000 08000000 31323334 35363738' 105 hexData64 = '00000000 00000000 08000000 00000000 31323334 35363738' 106 107 class TestUniConformantVaryingArray(NDRTest): 108 class theClass(NDRSTRUCT): 109 structure = ( 110 ('Array', NDRUniConformantVaryingArray), 111 ) 112 def populate(self, a): 113 a['Array'] = b'12345678' 114 hexData = '08000000 00000000 08000000 31323334 35363738' 115 hexData64 = '08000000 00000000 00000000 00000000 08000000 00000000 31323334 35363738' 116 117 class TestVaryingString(NDRTest): 118 class theClass(NDRSTRUCT): 119 structure = ( 120 ('Array', NDRVaryingString), 121 ) 122 def populate(self, a): 123 a['Array'] = b'12345678' 124 hexData = '00000000 09000000 31323334 35363738 00' 125 hexData64 = '00000000 00000000 09000000 00000000 31323334 35363738 00' 126 127 class TestConformantVaryingString(NDRTest): 128 class theClass(NDRSTRUCT): 129 structure = ( 130 ('Array', NDRConformantVaryingString), 131 ) 132 133 def populate(self, a): 134 a['Array'] = b'12345678' 135 hexData = '08000000 00000000 08000000 31323334 35363738' 136 hexData64 = '08000000 00000000 00000000 00000000 08000000 00000000 31323334 35363738' 137 138 class TestPointerNULL(NDRTest): 139 class theClass(NDRSTRUCT): 140 structure = ( 141 ('Array', NDRPOINTERNULL), 142 ) 143 def populate(self, a): 144 pass 145 hexData = '00000000' 146 hexData64 = '00000000 00000000' 147 148 if __name__=='__main__': 149 # Hide base class so that unittest.main() will not try to load it 150 del NDRTest 151 unittest.main(verbosity=1)