|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import logging |
| 4 | +import os |
| 5 | +import unittest |
| 6 | + |
| 7 | +from smda.utility.FileLoader import FileLoader |
| 8 | +from smda.common.BinaryInfo import BinaryInfo |
| 9 | +from smda.Disassembler import Disassembler |
| 10 | +from smda.common.SmdaReport import SmdaReport |
| 11 | +from smda.common.SmdaFunction import SmdaFunction |
| 12 | +from .context import config |
| 13 | + |
| 14 | +LOG = logging.getLogger(__name__) |
| 15 | +logging.basicConfig(level=logging.INFO, format="%(asctime)-15s %(message)s") |
| 16 | +logging.disable(logging.CRITICAL) |
| 17 | + |
| 18 | + |
| 19 | +class SmdaIntegrationTestSuite(unittest.TestCase): |
| 20 | + """Run a full example on a memory dump""" |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def setUpClass(cls): |
| 24 | + super(SmdaIntegrationTestSuite, cls).setUpClass() |
| 25 | + |
| 26 | + def testPeParsingWithCutwail(self): |
| 27 | + disasm = Disassembler(config) |
| 28 | + # load encrypted malicious win.cutwail |
| 29 | + with open(os.path.join(config.PROJECT_ROOT, "tests", "cutwail_xored"), "rb") as f_binary: |
| 30 | + binary = f_binary.read() |
| 31 | + decrypted_cutwail = bytearray() |
| 32 | + for index, byte in enumerate(binary): |
| 33 | + if isinstance(byte, str): |
| 34 | + byte = ord(byte) |
| 35 | + decrypted_cutwail.append(byte ^ (index % 256)) |
| 36 | + cutwail_binary = bytes(decrypted_cutwail) |
| 37 | + # run FileLoader and disassemble as file |
| 38 | + loader = FileLoader("/", map_file=True) |
| 39 | + loader._loadFile(cutwail_binary) |
| 40 | + file_content = loader.getData() |
| 41 | + binary_info = BinaryInfo(file_content) |
| 42 | + binary_info.raw_data = loader.getRawData() |
| 43 | + binary_info.file_path = "" |
| 44 | + binary_info.base_addr = loader.getBaseAddress() |
| 45 | + binary_info.bitness = loader.getBitness() |
| 46 | + binary_info.code_areas = loader.getCodeAreas() |
| 47 | + binary_info.oep = binary_info.getOep() |
| 48 | + cutwail_binary_info = binary_info |
| 49 | + cutwail_disassembly = disasm._disassemble(binary_info) |
| 50 | + cutwail_unmapped_disassembly = disasm.disassembleUnmappedBuffer(cutwail_binary) |
| 51 | + assert cutwail_unmapped_disassembly.num_functions == 33 |
| 52 | + |
| 53 | + def testElfParsingWithBase64(self): |
| 54 | + disasm = Disassembler(config) |
| 55 | + # load encrypted benign /bin/cat |
| 56 | + with open(os.path.join(config.PROJECT_ROOT, "tests", "cat_xored"), "rb") as f_binary: |
| 57 | + binary = f_binary.read() |
| 58 | + decrypted_cat = bytearray() |
| 59 | + for index, byte in enumerate(binary): |
| 60 | + if isinstance(byte, str): |
| 61 | + byte = ord(byte) |
| 62 | + decrypted_cat.append(byte ^ (index % 256)) |
| 63 | + cat_binary = bytes(decrypted_cat) |
| 64 | + # run FileLoader and disassemble as file |
| 65 | + loader = FileLoader("/", map_file=True) |
| 66 | + loader._loadFile(cat_binary) |
| 67 | + file_content = loader.getData() |
| 68 | + binary_info = BinaryInfo(file_content) |
| 69 | + binary_info.raw_data = loader.getRawData() |
| 70 | + binary_info.file_path = "" |
| 71 | + binary_info.base_addr = loader.getBaseAddress() |
| 72 | + binary_info.bitness = loader.getBitness() |
| 73 | + binary_info.code_areas = loader.getCodeAreas() |
| 74 | + binary_info.oep = binary_info.getOep() |
| 75 | + cat_binary_info = binary_info |
| 76 | + cat_disassembly = disasm._disassemble(binary_info) |
| 77 | + cat_unmapped_disassembly = disasm.disassembleUnmappedBuffer(cat_binary) |
| 78 | + assert cat_unmapped_disassembly.num_functions == 150 |
| 79 | + |
| 80 | + def testMacOsParsingWithKomplex(self): |
| 81 | + disasm = Disassembler(config) |
| 82 | + # load encrypted malicious osx.komplex |
| 83 | + with open(os.path.join(config.PROJECT_ROOT, "tests", "komplex_xored"), "rb") as f_binary: |
| 84 | + binary = f_binary.read() |
| 85 | + decrypted_komplex = bytearray() |
| 86 | + for index, byte in enumerate(binary): |
| 87 | + if isinstance(byte, str): |
| 88 | + byte = ord(byte) |
| 89 | + decrypted_komplex.append(byte ^ (index % 256)) |
| 90 | + komplex_binary = bytes(decrypted_komplex) |
| 91 | + # run FileLoader and disassemble as file |
| 92 | + loader = FileLoader("/", map_file=True) |
| 93 | + loader._loadFile(komplex_binary) |
| 94 | + file_content = loader.getData() |
| 95 | + binary_info = BinaryInfo(file_content) |
| 96 | + binary_info.raw_data = loader.getRawData() |
| 97 | + binary_info.file_path = "" |
| 98 | + binary_info.base_addr = loader.getBaseAddress() |
| 99 | + binary_info.bitness = loader.getBitness() |
| 100 | + binary_info.code_areas = loader.getCodeAreas() |
| 101 | + binary_info.oep = binary_info.getOep() |
| 102 | + komplex_binary_info = binary_info |
| 103 | + komplex_disassembly = disasm._disassemble(binary_info) |
| 104 | + komplex_unmapped_disassembly = disasm.disassembleUnmappedBuffer(komplex_binary) |
| 105 | + komplex_unmapped_disassembly.num_functions == 208 |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == '__main__': |
| 109 | + unittest.main() |
0 commit comments