Ejemplo n.º 1
0
 def test_random_read(self):
     '''
     Test a reading from a random sample, with a random size and a random
     start address
     '''
     sample = random.sample(self.samples, 1)[0]
     self.opts.filename = sample
     self.assertTrue(os.path.exists(sample))
     sample_size = os.path.getsize(sample)
     self.opts.address = random.randrange(sample_size)
     size_range = sample_size - self.opts.address
     self.opts.size = random.randrange(size_range)
     sys.stdout = StringIO()  # Suppress output
     device, memsize = interface.initialize(self.opts, self.module)
     memspace = memory.MemorySpace(device, memsize)
     dump.run(self.opts, memspace)
     sys.stdout = sys.__stdout__  # Restore output
     output_fn = dump.filename
     self.assertTrue(os.path.exists(output_fn))
     md5 = hashlib.md5()
     f = open(sample, 'rb')
     f.seek(self.opts.address)
     read = f.read(self.opts.size)
     md5.update(read)
     self.assertEqual(md5.digest(), self.file_md5(output_fn))
     f.close()
Ejemplo n.º 2
0
 def test_unlock(self):
     for sample in self.samples:
         cfg.startaddress = 0x00000000
         mod_name = sample[0]
         filepath = sample[1]
         try:
             module = SourceFileLoader(mod_name, filepath).load_module()
         except ImportError:
             assert(module)
         self.opts.interface = 'file'
         self.opts.filename = path.join(
             path.dirname(__file__), 'samples/') + mod_name + '.bin'
         foundtarget = False
         for i, target in enumerate(unlock.targets, 1):
             if target.signatures[0].os == module.OS:
                 foundtarget = [target]
                 self.opts.target_number = i
         # print(module.OS)
         self.assertTrue(foundtarget)
         self.assertIsNotNone(self.opts.target_number)
         module.IS_INTRUSIVE = True
         sys.stdout = StringIO()  # Suppress output
         device, memsize = interface.initialize(self.opts, module)
         memspace = memory.MemorySpace(device, memsize)
         address, page = unlock.run(self.opts, memspace)
         sys.stdout = sys.__stdout__  # Restore output
         # print(address & 0x00000fff)
         # print(module.offset)
         #self.assertEqual(address & 0x00000fff, module.offset)
         self.assertEqual(page, module.page)
Ejemplo n.º 3
0
 def test_unlock(self):
     for sample in self.samples:
         cfg.startaddress = 0x00000000
         mod_name = sample[0]
         filepath = sample[1]
         try:
             module = SourceFileLoader(mod_name, filepath).load_module()
         except ImportError:
             assert (module)
         self.opts.interface = 'file'
         self.opts.filename = path.join(path.dirname(__file__),
                                        'samples/') + mod_name + '.bin'
         foundtarget = False
         for i, target in enumerate(unlock.targets, 1):
             if target.signatures[0].os == module.OS:
                 foundtarget = [target]
                 self.opts.target_number = i
         # print(module.OS)
         self.assertTrue(foundtarget)
         self.assertIsNotNone(self.opts.target_number)
         module.IS_INTRUSIVE = True
         sys.stdout = StringIO()  # Suppress output
         device, memsize = interface.initialize(self.opts, module)
         memspace = memory.MemorySpace(device, memsize)
         address, page = unlock.run(self.opts, memspace)
         sys.stdout = sys.__stdout__  # Restore output
         # print(address & 0x00000fff)
         # print(module.offset)
         #self.assertEqual(address & 0x00000fff, module.offset)
         self.assertEqual(page, module.page)
Ejemplo n.º 4
0
 def test_findtag(self):
     target = targets[2]
     sys.stdout = StringIO()  # Suppress output
     device, memsize = interface.initialize(self.opts, self.module)
     memspace = memory.MemorySpace(device, memsize)
     results = memspace.find(target, findtag=True)
     sys.stdout = sys.__stdout__  # Restore output
     self.assertEqual(len(results), 2)
     self.assertEqual(results[1][1], targets[2].signatures[1])
Ejemplo n.º 5
0
 def test_findtag(self):
     target = targets[2]
     sys.stdout = StringIO()  # Suppress output
     device, memsize = interface.initialize(self.opts, self.module)
     memspace = memory.MemorySpace(device, memsize)
     results = memspace.find(target, findtag=True)
     sys.stdout = sys.__stdout__  # Restore output
     self.assertEqual(len(results), 2)
     self.assertEqual(results[1][1], targets[2].signatures[1])
Ejemplo n.º 6
0
 def test_find(self):
     target = targets[0]
     sys.stdout = StringIO()  # Suppress output
     device, memsize = interface.initialize(self.opts, self.module)
     memspace = memory.MemorySpace(device, memsize)
     address, signature, offset = memspace.find(target)
     sys.stdout = sys.__stdout__  # Restore output
     self.assertEqual(address, 672)
     self.assertEqual(signature, target.signatures[0])
     self.assertEqual(offset, target.signatures[0].offsets[0])
Ejemplo n.º 7
0
 def test_find(self):
     target = targets[0]
     sys.stdout = StringIO()  # Suppress output
     device, memsize = interface.initialize(self.opts, self.module)
     memspace = memory.MemorySpace(device, memsize)
     address, signature, offset = memspace.find(target)
     sys.stdout = sys.__stdout__  # Restore output
     self.assertEqual(address, 672)
     self.assertEqual(signature, target.signatures[0])
     self.assertEqual(offset, target.signatures[0].offsets[0])
Ejemplo n.º 8
0
 def test_fulldump(self):
     for sample in self.samples:
         self.opts.interface = 'file'
         self.opts.filename = sample
         sys.stdout = StringIO()  # Suppress output
         device, memsize = interface.initialize(self.opts, self.module)
         memspace = memory.MemorySpace(device, memsize)
         dump.run(self.opts, memspace)
         sys.stdout = sys.__stdout__  # Restore output
         output_fn = dump.filename
         self.assertTrue(os.path.exists(output_fn))
         self.assertEqual(self.file_md5(sample), self.file_md5(output_fn))
Ejemplo n.º 9
0
 def test_findall(self):
     target = targets[1]
     sys.stdout = StringIO()  # Suppress output
     device, memsize = interface.initialize(self.opts, self.module)
     memspace = memory.MemorySpace(device, memsize)
     results = memspace.find(target, findall=True)
     sys.stdout = sys.__stdout__  # Restore output
     self.assertEqual(len(results), 4)
     self.assertEqual(results[0][0], 49)
     self.assertEqual(results[3][0], 16433)
     for result in results:
         self.assertEqual(result[1], target.signatures[0])
Ejemplo n.º 10
0
 def test_findall(self):
     target = targets[1]
     sys.stdout = StringIO()  # Suppress output
     device, memsize = interface.initialize(self.opts, self.module)
     memspace = memory.MemorySpace(device, memsize)
     results = memspace.find(target, findall=True)
     sys.stdout = sys.__stdout__  # Restore output
     self.assertEqual(len(results), 4)
     self.assertEqual(results[0][0], 49)
     self.assertEqual(results[3][0], 16433)
     for result in results:
         self.assertEqual(result[1], target.signatures[0])
Ejemplo n.º 11
0
 def test_patch(self):
     target = targets[3]
     sig = target.signatures[0]
     sys.stdout = StringIO()  # Suppress output
     device, memsize = interface.initialize(self.opts, self.module)
     device.dry_run = False
     memspace = memory.MemorySpace(device, memsize)
     address = 0x00000042
     read = memspace.read(address, 4)
     backup = memspace.patch(address, sig.chunks)
     sys.stdout = sys.__stdout__  # Restore output
     read_back = memspace.read(address, 4)
     # print(read_back)
     self.assertEqual(sig.chunks[0].patch, read_back)
     memspace.write(address, read)
     read_back = memspace.read(address, 4)
     # print(read_back)
     self.assertEqual(read, read_back)
Ejemplo n.º 12
0
 def test_patch(self):
     target = targets[3]
     sig = target.signatures[0]
     sys.stdout = StringIO()  # Suppress output
     device, memsize = interface.initialize(self.opts, self.module)
     device.dry_run = False
     memspace = memory.MemorySpace(device, memsize)
     address = 0x00000042
     read = memspace.read(address, 4)
     backup = memspace.patch(address, sig.chunks)
     sys.stdout = sys.__stdout__  # Restore output
     read_back = memspace.read(address, 4)
     # print(read_back)
     self.assertEqual(sig.chunks[0].patch, read_back)
     memspace.write(address, read)
     read_back = memspace.read(address, 4)
     # print(read_back)
     self.assertEqual(read, read_back)