构建so库
参看https://blog.csdn.net/qq_26914291/article/details/118609288
IDA 反编译查看
项目结构
.
├── CMakeLists.txt
├── libdemo002.so
├── main.c
└── unicorn
├── include
│?? ├── list.h
│?? ├── qemu.h
│?? ├── uc_priv.h
│?? └── unicorn
│?? ├── arm.h
│?? ├── arm64.h
│?? ├── m68k.h
│?? ├── mips.h
│?? ├── platform.h
│?? ├── sparc.h
│?? ├── unicorn.h
│?? └── x86.h
└── libunicorn.a
main.c
#include <stdio.h>
#include <stdint.h>
#include "unicorn/unicorn.h"
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#define ADDRESS 0x8000
#define STACK_BASE 0x0
#define STACK_SIZE 0x7000
#define MEM_SIZE 20*1024*1024//20M
int main() {
uc_engine *uc;
uint32_t r2;
int fd;
struct stat file_info;
uint32_t in1=20000,in2=-349,sp=STACK_SIZE;
uc_hook hh;
//so file
stat("./libdemo002.so",&file_info);
printf("file size:%ld\n",file_info.st_size);
fd=open("./libdemo002.so",O_RDONLY);
void* data=malloc(file_info.st_size);
read(fd,data,file_info.st_size);
close(fd);
//unicorn
uc_open(UC_ARCH_ARM,UC_MODE_ARM,&uc);
//sp
uc_mem_map(uc,STACK_BASE,STACK_SIZE,UC_PROT_ALL);
uc_reg_write(uc,UC_ARM_REG_SP,&sp);
//call so
uc_mem_map(uc,ADDRESS,MEM_SIZE,UC_PROT_ALL);
uc_mem_write(uc,ADDRESS,data,file_info.st_size);
uc_reg_write(uc, UC_ARM_REG_R0, &in1);
uc_reg_write(uc, UC_ARM_REG_R1, &in2);
uc_emu_start(uc,ADDRESS+0x430, ADDRESS+0x44C,0,0);
//ret
uc_reg_read(uc,UC_ARM_REG_R0,&r2);
printf("r2=%d\n",r2);
//close
free(data);
uc_close(uc);
return 0;
}
?CMakeLists.txt
# cmake_minimum_required(VERSION <specify CMake version here>)
cmake_minimum_required(VERSION 3.16.3)
project(demo003 C)
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
include_directories(unicorn/include)
link_directories(unicorn)
add_executable(demo003 main.c)
target_link_libraries(demo003 libunicorn.a m)
编译及运行(WSL)
?完整项目下载
https://download.csdn.net/download/qq_26914291/20212194
参考
https://bbs.pediy.com/thread-266869.htm
http://c.biancheng.net/cpp/html/326.html
http://c.biancheng.net/cpp/html/238.html
https://blog.csdn.net/xuehuafeiwu123/article/details/53817161
|