-
Notifications
You must be signed in to change notification settings - Fork 54
/
demov.hpp
121 lines (115 loc) · 2.85 KB
/
demov.hpp
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef DEMOV_H
#define DEMOV_H
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <string>
#include <tuple>
#include <sys/types.h>
#include <capstone/capstone.h>
#include <signal.h>
#include <memory>
#include "memhlp.hpp"
#include "dishlp.hpp"
#include "stackMachine.hpp"
#include "ctlelem.hpp"
#include "ctlhlp.hpp"
#include "asmhlp.hpp"
enum mov_flags{
MOV_ID = 1,
MOV_FLOW = 2,
MOV_EXTERN = 4,
MOV_LOOP = 8
};
enum demov_state{
ST_REL = 1,
ST_SEG = 2,
ST_INIT = 4,
ST_LOOP = 8,
ST_MAIN = 16,
ST_ANLY = 32,
ST_CTANLY = 64,
ST_END = 128,
};
class demov{
public:
demov();
void set_relocations(std::unordered_map<uint64_t, std::string>*);
void set_segments(std::map<uint64_t,
std::tuple<uint8_t *, uint64_t, int>>*);
void set_entrypoint(uint64_t);
int analyse_sigaction(cs_insn *ins, size_t num, uint32_t **ret);
int parse_entry();
int parse_data();
int resub(uint64_t start, uint64_t length);
void dump_regs();
std::string dump_idc();
void dump_stat();
int init();
int analyse();
std::vector<std::pair<uint32_t, uint32_t>> get_blocks();
int scan();
std::string dump_flow();
std::string dump_calls();
uint64_t analyse_sel_on(cs_insn*);
std::string dump_syms();
void set_patch_call(bool b);
~demov();
private:
int find_on(cs_insn *ins, size_t num);
std::string* get_call_target(cs_insn *ins);
int find_target(cs_insn *i, uint64_t *tar);
cs_insn *find_toggle(cs_insn *i, x86_reg reg);
template<typename c>
int is_ret(std::stack<element, c> st);
int patch_jmp(cs_insn *off, uint64_t tar, uint8_t OP=0xE9);
int patch_ret(cs_insn *off);
int patch_jcc(cs_insn *sel, cs_insn *off, uint64_t tar);
int patch_call(cs_insn *off, uint64_t tar);
int do_switch(cs_insn *ins);
void find_fault();
dishlp dis;
ctlhlp ctl;
asmhlp ash;
csh handle;
uint64_t end;
int state = 0;
int flags;
uint64_t entrypoint;
uint64_t on;
uint64_t sel_on;
uint64_t master_loop;
uint64_t target_reg = 0;
uint64_t tar;
uint32_t stackp = 0;
std::unordered_map<uint64_t, std::string>* relocations;
std::shared_ptr<memhlp> mem;
std::unordered_map<uint64_t, uint64_t> jmp_tar;
std::map<uint64_t, bool> ret_tar;
std::map<uint64_t, size_t> ac_array;
std::map<uint64_t, size_t> ac_dir_r;
std::map<uint64_t, size_t> ac_dir_w;
std::map<uint64_t, size_t> ac_stat;
std::vector<uint32_t> regs;
};
template <typename c>
int demov::is_ret(std::stack<element, c> st) {
if (st.size() != 2) return 0;
element e = st.top();
st.pop();
if (e.type != ELE_MEM ||
e.mem.base == X86_REG_INVALID ||
e.mem.index != X86_REG_INVALID ||
e.mem.disp != 0)
return 0;
e = st.top();
if (e.type != ELE_MEM ||
e.mem.base != X86_REG_INVALID ||
e.mem.index != X86_REG_INVALID ||
e.mem.disp == 0)
return 0;
if (mem->get_sym((uint64_t) e.mem.disp) == SYM_SP)
return 1;
return 0;
}
#endif