-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin_dk.h
More file actions
56 lines (52 loc) · 1.89 KB
/
bin_dk.h
File metadata and controls
56 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include "runtime_env.h"
#include "utils.h"
#include "constants.h"
#include <cstdio>
#include <cstdint>
#include <cstring>
#include <climits>
#include <cerrno>
#include <stdexcept>
#include <algorithm>
using namespace std;
using uchar=unsigned char;
using ushort=unsigned short;
// 机器码处理
const uchar RET=0xc3;
const uchar NOP=0x90;
size_t getFuncCodeSize(void *funcptr,size_t maxsize=SIZE_MAX>>1){
uchar *delta=(uchar *)memchr(funcptr,RET,maxsize);
if(delta==nullptr) return SIZE_MAX;
return (delta-(uchar *)funcptr)+1;
}
/* 判断RET之后的特征指令,避免仅依赖RET指令的导出函数不完整 (备用)
const uchar FEATURE_CODE1=0x66;
const ushort FEATURE_CODE2=0x1f0f; // 0f 1f
size_t getFuncCodeSize(void *funcptr,size_t maxsize=SIZE_MAX>>1){
uchar *fcode=(uchar *)funcptr;
for(size_t i=0;i<maxsize;i++){
uchar byte=fcode[i];
if(byte==RET){ // 判断特征码
if(i<maxsize-1 && fcode[i+1]==FEATURE_CODE1) // NOP)
return i+1;
else if(i<maxsize-2 && *((ushort *)&fcode[i+1])==FEATURE_CODE2)
return i+1;
}
}
return maxsize;
}*/
void dumpFunctoFile(void *funcptr,const char *filename,
size_t maxsize=SIZE_MAX>>1,size_t minsize=0){
// minsize:getFuncCodeSize返回值过小时的最小导出大小,避免导出不完整
size_t size=max(minsize,getFuncCodeSize(funcptr,maxsize));
dumpMemory(funcptr,filename,size);
}
#define DUMP_BIN(func){\
dumpFunctoFile((void *)(func),"bin/"#func".bin");\
printf("Successfully generated bin/"#func".bin.\n");\
}
#define DUMP_BIN_SIZE(func,minsize,maxsize){\
dumpFunctoFile((void *)(func),"bin/"#func".bin",(maxsize),(minsize));\
printf("Successfully generated bin/"#func".bin.\n");\
}
#define DUMP_BIN_MINSIZE(func,minsize) DUMP_BIN_SIZE(func,(minsize),SIZE_MAX>>1)