-
Notifications
You must be signed in to change notification settings - Fork 146
/
main.cpp
283 lines (260 loc) · 6.2 KB
/
main.cpp
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include <unistd.h>
#include <getopt.h>
#include <openssl/sha.h>
#include "SHA1.h"
#include "MD5ex.h"
#include "SHA256.h"
#include "SHA512ex.h"
#include "MD4ex.h"
using namespace std;
vector<unsigned char> StringToVector(unsigned char * str)
{
vector<unsigned char> ret;
for(unsigned int x = 0; x < strlen((char*)str); x++)
{
ret.push_back(str[x]);
}
return ret;
}
void DigestToRaw(string hash, unsigned char * raw)
{
transform(hash.begin(), hash.end(), hash.begin(), ::tolower);
string alpha("0123456789abcdef");
for(unsigned int x = 0; x < (hash.length() / 2); x++)
{
raw[x] = (unsigned char)((alpha.find(hash.at((x * 2))) << 4));
raw[x] |= (unsigned char)(alpha.find(hash.at((x * 2) + 1)));
}
}
vector<unsigned char> * GenerateRandomData()
{
vector<unsigned char> * ret = new vector<unsigned char>();
int length = rand() % 128;
for(int x = 0; x < length; x++)
{
ret->push_back((rand() % (126 - 32)) + 32);
}
return ret;
}
void TestExtender(Extender * sex)
{
//First generate a signature, with randomly generated data
vector<unsigned char> * vkey = GenerateRandomData();
vector<unsigned char> * vmessage = GenerateRandomData();
vector<unsigned char> * additionalData = GenerateRandomData();
unsigned char * firstSig;
unsigned char * secondSig;
sex->GenerateSignature(*vkey, *vmessage, &firstSig);
if(sex->ValidateSignature(*vkey, *vmessage, firstSig))
{
vector<unsigned char> * newData = sex->GenerateStretchedData(*vmessage, vkey->size(), firstSig, *additionalData, &secondSig);
if(sex->ValidateSignature(*vkey, *newData, secondSig))
{
cout << "\tTest passed." << endl;
delete vkey;
delete vmessage;
delete additionalData;
delete newData;
return;
}
else
{
cout << "\tGenerated data failed to be verified as correctly signed." << endl;
delete vkey;
delete vmessage;
delete additionalData;
delete newData;
return;
}
}
else
{
cout << "\tInitial signature check failed." << endl;
delete vkey;
delete vmessage;
delete additionalData;
return;
}
}
Extender * GetExtenderForHash(string sig)
{
if(sig.length() == 40)
{
return new SHA1ex();
}
else if(sig.length() == 64)
{
return new SHA256ex();
}
else if(sig.length() == 32)
{
return new MD5ex();
}
else if(sig.length() == 128)
{
return new SHA512ex();
}
return NULL;
}
void PrintHelp()
{
cout << "HashPump [-h help] [-t test] [-s signature] [-d data] [-a additional] [-k keylength]" << endl;
cout << " HashPump generates strings to exploit signatures vulnerable to the Hash Length Extension Attack." << endl;
cout << " -h --help Display this message." << endl;
cout << " -t --test Run tests to verify each algorithm is operating properly." << endl;
cout << " -s --signature The signature from known message." << endl;
cout << " -d --data The data from the known message." << endl;
cout << " -a --additional The information you would like to add to the known message." << endl;
cout << " -k --keylength The length in bytes of the key being used to sign the original message with." << endl;
cout << " Version 1.0 with MD5, SHA1, SHA256 and SHA512 support." << endl;
cout << " <Developed by bwall(@bwallHatesTwits)>" << endl;
}
int main(int argc, char ** argv)
{
string sig;
string data;
int keylength = 0;
string datatoadd;
Extender * sex = NULL;
bool run_tests = false;
while(1)
{
int option_index = 0;
static struct option long_options[] = {
{"test", no_argument, 0, 0},
{"signature", required_argument, 0, 0},
{"data", required_argument, 0, 0},
{"additional", required_argument, 0, 0},
{"keylength", required_argument, 0, 0},
{"help", no_argument, 0, 0},
{0, 0, 0, 0}
};
int c = getopt_long(argc, argv, "ts:d:a:k:h", long_options, &option_index);
if (c == -1)
break;
switch(c)
{
case 0:
switch(option_index)
{
case 0:
run_tests = true;
break;
case 1:
sig.assign(optarg);
sex = GetExtenderForHash(sig);
if(sex == NULL)
{
cout << "Unsupported signature size." << endl;
return 0;
}
break;
case 2:
data.assign(optarg);
break;
case 3:
datatoadd.assign(optarg);
break;
case 4:
keylength = atoi(optarg);
break;
case 5:
PrintHelp();
return 0;
}
break;
case 't':
run_tests = true;
break;
case 's':
sig.assign(optarg);
sex = GetExtenderForHash(sig);
if(sex == NULL)
{
cout << "Unsupported hash size." << endl;
return 0;
}
break;
case 'd':
data.assign(optarg);
break;
case 'a':
datatoadd.assign(optarg);
break;
case 'k':
keylength = atoi(optarg);
break;
case 'h':
PrintHelp();
return 0;
}
}
if(run_tests)
{
//Just a simple way to force tests
cout << "Testing SHA1" << endl;
TestExtender(new SHA1ex());
cout << "Testing SHA256" << endl;
TestExtender(new SHA256ex());
cout << "Testing SHA512" << endl;
TestExtender(new SHA512ex());
cout << "Testing MD5" << endl;
TestExtender(new MD5ex());
cout << "Testing MD4" << endl;
TestExtender(new MD4ex());
cout << "Testing concluded" << endl;
return 0;
}
if(sig.size() == 0)
{
cout << "Input Signature: ";
cin >> sig;
sex = GetExtenderForHash(sig);
if(sex == NULL)
{
cout << "Unsupported hash size." << endl;
return 0;
}
}
if(data.size() == 0)
{
cout << "Input Data: ";
cin >> data;
}
if(keylength == 0)
{
cout << "Input Key Length: ";
cin >> keylength;
}
if(datatoadd.size() == 0)
{
cout << "Input Data to Add: ";
cin >> datatoadd;
}
vector<unsigned char> vmessage = StringToVector((unsigned char*)data.c_str());
vector<unsigned char> vtoadd = StringToVector((unsigned char*)datatoadd.c_str());
unsigned char firstSig[20];
DigestToRaw(sig, firstSig);
unsigned char * secondSig;
vector<unsigned char> * secondMessage = sex->GenerateStretchedData(vmessage, keylength, firstSig, vtoadd, &secondSig);
for(int x = 0; x < 20; x++)
{
printf("%02x", secondSig[x]);
}
cout << endl;
for(unsigned int x = 0; x < secondMessage->size(); x++)
{
unsigned char c = secondMessage->at(x);
if(c >= 32 && c <= 126)
{
cout << c;
}
else
{
printf("\\x%02x", c);
}
}
delete secondMessage;
cout << endl;
return 0;
}