-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcracking.cpp
More file actions
160 lines (133 loc) · 2.56 KB
/
cracking.cpp
File metadata and controls
160 lines (133 loc) · 2.56 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
155
156
157
158
159
160
#include "cracking.hpp"
void cracking_hook_function(int from, int to)
{
int relative = to - (from+5); // +5 is the position of next opcode
memset((void *)from, 0xE9, 1); // JMP-OPCODE
memcpy((void *)(from+1), &relative, 4); // set relative address with endian
}
void cracking_hook_call(int from, int to)
{
int relative = to - (from+5); // +5 is the position of next opcode
memcpy((void *)(from+1), &relative, 4); // set relative address with endian
}
int singleHexToNumber(char hexchar)
{
switch (hexchar)
{
case '0':
return 0;
case '1':
return 1;
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'a':
case 'A':
return 10;
case 'b':
case 'B':
return 11;
case 'c':
case 'C':
return 12;
case 'd':
case 'D':
return 13;
case 'e':
case 'E':
return 14;
case 'f':
case 'F':
return 15;
default:
return -1;
}
}
int hexToBuffer(char *hex, char *buffer, int bufferLen)
{
int len, neededBytes, i, padding, first, pos, leftPart, rightPart;
len = strlen(hex); // every byte of hex is taking 4 bits. F=1111
padding = 0; // just for "a", "abc" etc... "a" = 0x0a, "abc" = 0x0abc
// we dont handle 4-bits for one hex-number, so round up to bytes...
// three bytes will not take 12 bits, they will use 16 bits = 2 bytes
if (len % 2 != 0)
{
padding = 1;
len++;
}
neededBytes = len >> 1; // its like dividing by 2
first = 1;
pos = 0;
for (i=0; i<neededBytes; i++)
{
char twochars[2] = {'0', '0'};
if (first)
{
if (padding)
{
twochars[1] = hex[0];
pos++;
}
else
{
twochars[0] = hex[0];
twochars[1] = hex[1];
pos += 2;
}
first = 0;
}
else
{
twochars[0] = hex[pos];
twochars[1] = hex[pos+1];
pos += 2;
}
leftPart = singleHexToNumber(twochars[0]);
rightPart = singleHexToNumber(twochars[1]);
if (leftPart == -1 || rightPart == -1)
return i;
buffer[i] = (leftPart << 4) + rightPart;
// buffer end:
if (i == bufferLen)
return i;
}
return neededBytes;
}
int cracking_write_hex(int address, char *hex)
{
unsigned char *ptr = (unsigned char *)address;
char buffer[128] = {0};
int bytes;
int i;
bytes = hexToBuffer(hex, buffer, 128);
for (i=0; i<bytes; i++)
ptr[i] = buffer[i];
return bytes;
}
cHook::cHook(int from, int to)
{
this->from = from;
this->to = to;
}
void cHook::hook()
{
memcpy((void *)oldCode, (void *)from, 5);
cracking_hook_function(from, to);
}
void cHook::unhook()
{
memcpy((void *)from, (void *)oldCode, 5);
}