使用qiling运行程序

一、简介

Qiling 框架不仅仅是一个仿真平台或逆向工程工具。它将二进制检测和二进制仿真整合到一个框架中,解决了应用程序并非在真空中运行且高度依赖操作系统的问题。凭借广泛的操作系统支持,Qiling 框架为二进制分析开辟了无限的可能性和潜力。借助 Qiling 框架,它能够:

  • 跨平台:Windows、MacOS、Linux、BSD、UEFI、DOS
  • 跨架构:X86、X86_64、ARM、ARM64、MIPS、8086
  • 多种文件格式:PE、MachO、ELF、COM
  • 在隔离环境中模拟和沙盒机器代码
  • 支持跨架构和平台的调试能力
  • 提供高级 API 来设置和配置沙盒
  • 细粒度检测:允许在各个级别进行挂钩(指令/基本块/内存访问/异常/系统调用/IO/等)
  • 允许动态热补丁即时运行代码,包括加载的库
  • Python 中的真正框架,可轻松在其上构建定制的安全分析工具

Qiling Framework 能够模拟:

  • Windows X86 32/64位
  • Linux X86 32/64位、ARM、AARCH64、MIPS
  • MacOS X86 32/64位
  • FreeBSD X86 32/64位
  • UEFI
  • DOS
  • MBR

Qiling Framework 能够在 Linux/FreeBSD/MacOS/Windows (WSL2) 上运行,不受 CPU 架构限制。

二、安装

官方网站:

https://qiling.io/
https://github.com/qilingframework/qiling

通常可以通过pip进行安装,如果要安装特定版本,可以加入“==版本号”

pip install qiling

qiling安装的时候可能对python版本的库有要求,可以使用conda安装特定版本的python,然后再安装

# 浏览器下载
https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-5.3.1-Linux-x86_64.sh

# 安装
chmod +x Anaconda3-5.3.1-Linux-x86_64.sh
./Anaconda3-5.3.1-Linux-x86_64.sh

# 创建虚拟环境
conda create -n py310 python=3.10

# 激活环境
conda activate py310

# pip 安装qiling
pip install unicorn==2.0.1 qiling==1.4.6

三、使用方法

官方仓库有一些示例可以参考

https://github.com/qilingframework/qiling/tree/master/examples

1、执行可执行程序

除了常规的可执行程序以外,也可以模拟嵌入式设备,如网件路由器R6220里面的mini_httpd程序的运行(examples/netgear_6220.py)

执行日志,如果不想打印调试的信息,可以将QL_VERBOSE.DEBUG修改为QL_VERBOSE.OFF。

2、执行shellcode

除了执行可执行程序以外,还能单独运行shellcode(examples/shellcode_run.py)

执行的日志:

3、执行bootloader

可以执行u-boot、efi、MBR等计算机设备开机的启动代码,如下所示启动u-boot(examples\hello_arm_uboot.py)

执行结果:

4、执行其他系统

可以执行其他的非常规操作系统,如下所示启动rtos系统(examples/mcu/stm32f411_freertos.py)

启动日志

打印的字符:

四、qltool

Qiling 提供了一个名为 qltool 的强大工具,可以快速模拟出目标 Shellcode 或可执行文件。

首先需要选择运行的模式,常用的主要是run、code,对应的是可执行程序、shellcode模式。

$ qltool -h
usage: qltool [-h] [--version] {run,code,examples,qltui} ...

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit

sub commands:
  select execution mode

  {run,code,examples,qltui}
    run                 run a program
    code                execute a shellcode
    examples            show examples and exit
    qltui               show qiling Terminal User Interface

运行可执行程序的模式

$ qltool run -h
usage: qltool run [-h] [-f FILE] --rootfs ROOTFS [--args ...]
                  [-v {disabled,off,default,debug,disasm,dump}] [--env FILE]
                  [-g [SERVER:PORT]] [--qdb] [--rr] [--profile PROFILE]
                  [--no-console] [-e REGEXP] [--log-file LOG_FILE] [--log-plain]
                  [--root] [--debug-stop] [-m] [--timeout TIMEOUT] [-c COVERAGE_FILE]
                  [--coverage-format {drcov,drcov_exact}] [--json] [--libcache]
                  ...

positional arguments:
  run_args

options:
  -h, --help            show this help message and exit
  -f FILE, --filename FILE
                        filename
  --rootfs ROOTFS       emulated rootfs
  --args ...            args
  -v {disabled,off,default,debug,disasm,dump}, --verbose {disabled,off,default,debug,disasm,dump}
                        set verbosity level
  --env FILE            pickle file containing an environment dictionary
  -g [SERVER:PORT], --gdb [SERVER:PORT]
                        enable gdb server
  --qdb                 attach Qdb at entry point, it's MIPS, ARM(THUMB) supported
                        only for now
  --rr                  switch on record and replay feature in qdb, only works with
                        --qdb
  --profile PROFILE     define a customized profile
  --no-console          do not emit output to console
  -e REGEXP, --filter REGEXP
                        apply a filtering regexp on log output
  --log-file LOG_FILE   write log to a file
  --log-plain           do not use colors in log output
  --root                enable sudo required mode
  --debug-stop          stop running on error; requires verbose to be set to either
                        "debug" or "dump"
  -m, --multithread     run in multithread mode
  --timeout TIMEOUT     set emulation timeout
  -c COVERAGE_FILE, --coverage-file COVERAGE_FILE
                        code coverage file name
  --coverage-format {drcov,drcov_exact}
                        code coverage file format
  --json                print a json report of the emulation
  --libcache            enable dll caching for windows

运行shellcode的模式

$ qltool code -h
usage: qltool code [-h] [-f FILE] [-i INPUT] --arch
                   {x86,x8664,arm,arm64,mips,a8086,evm,cortex_m,riscv,riscv64,ppc}
                   [--thumb] [--endian {little,big}] --os
                   {linux,freebsd,macos,windows,uefi,dos,evm,qnx,mcu,blob}
                   [--rootfs ROOTFS] [--format {asm,hex,bin}]
                   [-v {disabled,off,default,debug,disasm,dump}] [--env FILE]
                   [-g [SERVER:PORT]] [--qdb] [--rr] [--profile PROFILE]
                   [--no-console] [-e REGEXP] [--log-file LOG_FILE] [--log-plain]
                   [--root] [--debug-stop] [-m] [--timeout TIMEOUT]
                   

options:
  -h, --help            show this help message and exit
  -f FILE, --filename FILE
                        filename
  -i INPUT, --input INPUT
                        input hex value
  --arch {x86,x8664,arm,arm64,mips,a8086,evm,cortex_m,riscv,riscv64,ppc}
  --thumb               specify thumb mode for ARM
  --endian {little,big}
                        specify endianess for bi-endian archs
  --os {linux,freebsd,macos,windows,uefi,dos,evm,qnx,mcu,blob}
  --rootfs ROOTFS       emulated root filesystem, that is where all libraries reside
  --format {asm,hex,bin}
                        input file format
  -v {disabled,off,default,debug,disasm,dump}, --verbose {disabled,off,default,debug,disasm,dump}
                        set verbosity level
  --env FILE            pickle file containing an environment dictionary
  -g [SERVER:PORT], --gdb [SERVER:PORT]
                        enable gdb server
  --qdb                 attach Qdb at entry point, it's MIPS, ARM(THUMB) supported
                        only for now
  --rr                  switch on record and replay feature in qdb, only works with
                        --qdb
  --profile PROFILE     define a customized profile
  --no-console          do not emit output to console
  -e REGEXP, --filter REGEXP
                        apply a filtering regexp on log output
  --log-file LOG_FILE   write log to a file
  --log-plain           do not use colors in log output
  --root                enable sudo required mode
  --debug-stop          stop running on error; requires verbose to be set to either
                        "debug" or "dump"
  -m, --multithread     run in multithread mode
  --timeout TIMEOUT     set emulation timeout
  -c COVERAGE_FILE, --coverage-file COVERAGE_FILE
                        code coverage file name
  --coverage-format {drcov,drcov_exact}
                        code coverage file format
  --json                print a json report of the emulation
  --libcache            enable dll caching for windows

测试powerpc架构的程序运行情况,最后能打印“Hello World!”。有的程序有参数才能启动,如果有参数就使用--args后面接上对应的参数。

五、参考链接

https://qiling.io/2023/08/04/intro/

https://blog.csdn.net/freeking101/article/details/128522901

留下评论

您的电子邮箱地址不会被公开。 必填项已用*标注