Fare9

Untitled

Feb 7th, 2021
1,354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #-*- coding: utf-8 -*-
  3.  
  4. import sys
  5. import os
  6.  
  7. from qiling import *
  8. from capstone import *
  9.  
  10. base_address = 0x400000
  11.  
  12. offset = 0x402176 - 0x402044
  13.  
  14. address_of_call = 0x4012a7
  15.  
  16. flag = ""
  17. pos = 0
  18.  
  19. def my_fgets(ql):
  20.     '''
  21.    Write a valid size buffer (with incorrect content),
  22.    then set RAX to correct value, this will make that
  23.    program continues until the checks.
  24.    '''
  25.     buff_addr = ql.os.function_arg[0]
  26.     print("[+] Buffer address to write string: 0x%lx" % (buff_addr))
  27.     print("[+] Writing %s" % ('A'*0x26))
  28.     ql.mem.string(buff_addr, 'A'*0x26)
  29.     ql.reg.write('RAX', buff_addr)
  30.  
  31. def patch2(ql):
  32.     '''
  33.    Patch to extract data from the comparison
  34.    '''
  35.     global flag
  36.     global pos
  37.  
  38.     EDX = ql.reg.read("EDX")
  39.     EAX = ql.reg.read("EAX")
  40.     print("[+] Value that should be written in position %d: %x (%c)" % (pos, EDX, chr(EDX)))
  41.     ql.reg.write("EAX", EDX)
  42.     flag += chr(EDX)
  43.     pos += 1
  44.  
  45. def patch1(ql):
  46.     '''
  47.    Patch to extract address to call
  48.    '''
  49.     RCX = ql.reg.read("RCX")
  50.     print("[+] Address with decryption function: 0x%lx" % (RCX))
  51.     print("[+] New address to patch: 0x%lx" % (RCX + offset))
  52.     buf = ql.mem.read((RCX + offset), 2)
  53.     md = Cs(CS_ARCH_X86, CS_MODE_64)
  54.     for i in md.disasm(buf, (RCX + offset)):
  55.         print("[+] Disassembly of patched address: :: 0x%x:\t%s\t%s" %(i.address, i.mnemonic, i.op_str))
  56.     ql.hook_address(callback=patch2, address=(RCX + offset))
  57.  
  58.  
  59. ql = Qiling(filename=["./x-and-or"], archtype="x86-64", ostype="linux", rootfs='/', profile='my_profiler.ql')
  60. ql.hook_address(callback=patch1, address=address_of_call)
  61. ql.set_api('fgets', my_fgets)
  62. ql.run()
  63.  
  64. print("Flag is: '%s'" % (flag))
Advertisement
Add Comment
Please, Sign In to add comment