How to hook a C function? #1523
-
| 
         I try to hook a C function using  def hook_function(ql):
    print("Function hooked!")
base_addr = ql.loader.images[0].base
offset = 0x1234  # From readelf or IDA
real_addr = base_addr + offset
ql.hook_address(hook_function, real_addr)
ql.hook_address(hook_function, offset)I tried many ways, but they all not work. Here are my codes. My environment is amd64/arm64 with ubuntu 24.04. C code: qiling code:  | 
  
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
| 
         Hi, I tried with the minimal example you provided, and it worked. def qiling_test():
    def my_hook(ql):
        print("Function hooked!")
    
    rootfs_path = "/home/xxx/workspace/git/qiling/examples/rootfs/x8664_linux"
    bin_path = "/home/xxx/main_elf"
    ql = Qiling([bin_path, "123"], rootfs_path, console=False, verbose=QL_VERBOSE.DISABLED)
    # image_base = ql.loader.load_address
    image_base = ql.loader.images[0].base
    ql.hook_address(my_hook, image_base + 0x118C)    # the first instruction address of main()
    ql.run()The output is as follows. ~$ python3 qiling_demo.py 
Function hooked!
ret: 1
 A: You should provide a valid virtual address to   | 
  
Beta Was this translation helpful? Give feedback.

Hi, I tried with the minimal example you provided, and it worked.
The output is as follows.