-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconverter.cpp
More file actions
154 lines (130 loc) · 3.05 KB
/
converter.cpp
File metadata and controls
154 lines (130 loc) · 3.05 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
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "converter.h"
#include "idaEx.h"
#include "misc.h"
// idasdk
#include <kernwin.hpp>
static int idaapi WindowConverterChangeCB(int field_id, form_actions_t& fa)
{
if (field_id < 0)
return 1;
int mask = 0;
fa.get_combobox_value(1, &mask);
fa.enable_field(3, !mask);
return 1;
}
void WindowConverter()
{
qstring sig, mask;
int action = 0;
if (ask_form(
"Converter\n"
"%/"
"<Signature :q::64:>\n"
"<Mask :q3::64:>\n"
"<#Code to IDA:R0>\n"
"<#IDA to Code:R1>>\n",
WindowConverterChangeCB,
&sig, &mask, &action) != 1)
return;
switch (action)
{
case 0:
if (CodeToIDA(sig.c_str(), mask, sig))
{
Stage(" Convert code to IDA ");
msg("Signature: %s\n", sig.c_str());
Stage("");
}
else
{
msg("Empty signature or mask\n");
}
break;
case 1:
if (IDAToCode(sig, sig, mask))
{
Stage(" Convert IDA to code ");
msg("Signature: %s\n"
"Mask: %s\n",
sig.c_str(), mask.c_str());
Stage("");
}
else
{
msg("Empty signature\n");
}
break;
}
}
int CreateSignature(const char* ptr, qstring& outBytes, qstring* outMask = nullptr)
{
outBytes.clear();
if (outMask)
outMask->clear();
int count = 0;
char* endptr = nullptr;
while (*ptr != '\0')
{
if (*ptr == '?')
{
outBytes.append(Settings.wildcard);
if (outMask)
outMask->append('?');
ptr++;
count++;
if (*ptr == '?')
ptr++;
}
else if (qisxdigit(*ptr))
{
unsigned long val = strtoul(ptr, &endptr, 16);
if (endptr == ptr)
break;
outBytes.append(static_cast<unsigned char>(val & 0xFF));
if (outMask)
outMask->append('x');
ptr = endptr;
count++;
}
else
{
ptr++;
}
}
return count;
}
bool CodeToIDA(qstring code, const qstring& mask, qstring& outSig)
{
idaEx::ltrim(code);
code.rtrim();
size_t len = mask.length();
if (code.empty() || !len)
return false;
qstring sig;
CreateSignature(code.c_str(), sig);
outSig.clear();
size_t i = -1;
while (++i < len)
{
if (mask[i] == '?')
outSig += "? ";
else
outSig.cat_sprnt("%02X ", static_cast<unsigned char>(sig[i]));
}
return true;
}
bool IDAToCode(qstring sig, qstring& outSig, qstring &outMask)
{
idaEx::ltrim(sig);
sig.rtrim();
if (sig.empty())
return false;
qstring bytes;
outMask.clear();
int count = CreateSignature(sig.c_str(), bytes, &outMask);
outSig.clear();
int i = -1;
while (++i < count)
outSig.cat_sprnt("\\x%02X", static_cast<unsigned char>(bytes[i]));
return true;
}