-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
642 lines (504 loc) · 16.4 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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
// COMPILE_LINE: g++ -g main.cpp -o wifigb -lstdc++ -lssl -lcrypto
//
#include <iostream>
#include <queue>
#include <set>
#include <stdexcept>
#include "Config.h"
#include "sha1hash.h"
#include "memblock.h"
#include "argstream.h"
#include <openssl/evp.h>
class Expression
{
public:
virtual ~Expression() {}
virtual float entropy() const = 0 ; // maximum entropy of the whole expression in bits
virtual MemBlock eval() const = 0 ; // evaluates the expression and produces a stream of bytes.
virtual int length() const = 0 ; // evaluates the expression and produces a stream of bytes.
void show() const { print(0) ; std::cerr << std::endl; }
virtual void initState() = 0; // get to next state. Returns false if not possible.
virtual bool nextState() = 0; // get to next state. Returns false if not possible.
virtual Expression *deepCopy() const=0 ;
virtual Sha1CheckSum topologicalHash() = 0 ; // computes the hash so as to be able to check the equality
virtual void print(int depth) const =0 ; // hierarchical display
protected:
friend class Exp_substring ;
};
class Exp_const: public Expression
{
public:
virtual ~Exp_const() {}
Exp_const(const MemBlock& mem) : mMem(mem) {}
virtual float entropy() const { return mMem.size()*8 ; }
virtual MemBlock eval() const { return mMem ; }
virtual int length() const { mMem.size() ; }
virtual void initState() {} // nothing to do.
virtual bool nextState() { return false ; } // no parameters, so nothing to do.
virtual Expression *deepCopy() const{ return new Exp_const(*this) ; }
virtual Sha1CheckSum topologicalHash() { return Sha1CheckSum((unsigned char*)mMem.toString().c_str(),mMem.size()) ; }
protected:
virtual void print(int depth) const
{
for(int i=0;i<depth*2;++i) std::cerr << " " ;
std::cerr << "Const: " << mMem.toHex() << std::endl;
}
MemBlock mMem ;
};
class Exp_numeric_counter: public Expression
{
public:
virtual ~Exp_numeric_counter() {}
Exp_numeric_counter(uint8_t bytes) : mBytes(bytes),mValue(0) {}
virtual float entropy() const { return mBytes*8 ; }
virtual MemBlock eval() const { MemBlock mem(mBytes,0) ; for(int i=0;i<mBytes;++i) mem[i] = ((mValue >> (8*i)) & 0xff) ; return mem ;}
virtual int length() const { return mBytes ; }
virtual void initState() { mValue = 0 ;} // nothing to do.
virtual bool nextState() { return ++mValue < (1ull << (mBytes*8)) ; } // no parameters, so nothing to do.
virtual Expression *deepCopy() const{ return new Exp_numeric_counter(*this) ; }
virtual Sha1CheckSum topologicalHash() { std::string s = "Numeric counter" ; return Sha1CheckSum((unsigned char*)s.c_str(),s.length()) ; }
protected:
virtual void print(int depth) const
{
for(int i=0;i<depth*2;++i) std::cerr << " " ;
std::cerr << "Counter: " << eval().toHex() << std::endl;
}
uint64_t mValue ;
uint8_t mBytes ;
};
class Exp_ascii_counter: public Expression
{
public:
virtual ~Exp_ascii_counter() {}
Exp_ascii_counter(uint8_t bytes,char base) : mBytes(bytes),mBase(base),mValues(bytes,0) {}
virtual float entropy() const { return mBytes*8 ; }
virtual MemBlock eval() const { MemBlock mem(mBytes,0) ; for(int i=0;i<mBytes;++i) mem[i] = mValues[i] + mBase ; return mem ;}
virtual int length() const { return mBytes ; }
virtual void initState() { mValues.clear(); mValues.resize(mBytes,0) ;} // nothing to do.
virtual bool nextState()
{
for(int i=0;i<mBytes;++i)
if(++mValues[i] < 26)
return true ;
else
mValues[i] = 0 ;
return false ;
} // no parameters, so nothing to do.
virtual Expression *deepCopy() const{ return new Exp_ascii_counter(*this) ; }
virtual Sha1CheckSum topologicalHash() { std::string s = "ASCII"+mBase+('A'+mBytes) ; return Sha1CheckSum((unsigned char*)s.c_str(),s.length()) ; }
protected:
virtual void print(int depth) const
{
for(int i=0;i<depth*2;++i) std::cerr << " " ;
std::cerr << "Ascii Counter: " << eval().toString() << std::endl;
}
std::vector<uint8_t> mValues ;
uint8_t mBytes ;
char mBase ;
};
class Exp_hash_sha1: public Expression
{
public:
Exp_hash_sha1(Expression *e)
{
mArgument = e ;
}
virtual ~Exp_hash_sha1() { delete mArgument ; }
virtual Expression *deepCopy() const{ return new Exp_hash_sha1(mArgument->deepCopy()) ; }
virtual Sha1CheckSum topologicalHash()
{
std::string s = "sha1hash" + mArgument->topologicalHash().toStdString() ;
return Sha1CheckSum((unsigned char*)s.c_str(),s.length()) ;
}
protected:
virtual void print(int depth) const
{
for(int i=0;i<depth*2;++i) std::cerr << " " ;
std::cerr << "SHA1()" << std::endl;
mArgument->print(depth+1) ;
}
virtual float entropy() const
{
return 1+mArgument->entropy();
}
virtual MemBlock eval() const
{
MemBlock m = mArgument->eval() ;
return MemBlock(Sha1CheckSum(m.data(),m.size()).bytes,Sha1CheckSum::HASH_LENGTH) ;
}
virtual int length() const { return Sha1CheckSum::HASH_LENGTH ; }
virtual void initState() {}
virtual bool nextState()
{
return false ;
}
private:
Expression *mArgument ;
};
class Exp_hash_generic: public Expression
{
public:
Exp_hash_generic(Expression *e)
{
mArgument = e ;
mHashId= 0;
}
virtual ~Exp_hash_generic() { delete mArgument ; }
virtual Expression *deepCopy() const { return new Exp_hash_generic(mArgument->deepCopy()) ; }
virtual void initState() { mHashId = 0 ;}
virtual bool nextState()
{
if(mArgument->nextState())
return true ;
mArgument->initState() ;
mHashId = (mHashId+1)%mNumHashes;
return (bool)mHashId;
}
virtual float entropy() const { return mArgument->entropy() + 1; }
virtual Sha1CheckSum topologicalHash()
{
std::string s = digest_names[mHashId] + mArgument->topologicalHash().toStdString() ;
return Sha1CheckSum((unsigned char*)s.c_str(),s.length()) ;
}
virtual MemBlock eval() const
{
MemBlock m = mArgument->eval() ;
unsigned int md_len ;
EVP_MD_CTX *mdctx = EVP_MD_CTX_create();
unsigned char md_value[EVP_MAX_MD_SIZE];
EVP_DigestInit_ex(mdctx, digests[mHashId], NULL);
EVP_DigestUpdate(mdctx, m.bytes(), m.size());
EVP_DigestFinal_ex(mdctx, md_value, &md_len);
EVP_MD_CTX_destroy(mdctx);
return MemBlock(md_value,md_len) ;
}
virtual int length() const { return EVP_MD_size(digests[mHashId]) ; }
virtual void print(int depth) const
{
for(int i=0;i<depth*2;++i) std::cerr << " " ;
std::cerr << digest_names[mHashId] << "()" << std::endl;
mArgument->print(depth+1) ;
}
private:
static const EVP_MD *digests[10] ;
static const std::string digest_names[10] ;
static const int mNumHashes = 10 ;
uint32_t mHashId ;
Expression *mArgument;
};
const EVP_MD *Exp_hash_generic::digests[10] = { EVP_whirlpool(), EVP_md5(), EVP_sha(), EVP_sha224(),EVP_sha1(), EVP_sha384(), EVP_sha256(), EVP_sha512(),EVP_md4(), EVP_ripemd160() } ;
const std::string Exp_hash_generic::digest_names[10] = { "whirlpool","md5","sha","sha224","sha1","sha384", "sha256", "sha512","md4","ripemd160" } ;
class Exp_substring: public Expression
{
public:
Exp_substring(Expression *e,int min_length=1,int max_length=0)
: mMinLength(min_length),mMaxLength(max_length)
{
mArgument = e ;
mStart = 0 ;
mLength = min_length ;
}
virtual ~Exp_substring() { delete mArgument ; }
virtual Expression *deepCopy() const{ return new Exp_substring(mArgument->deepCopy()) ; }
virtual Sha1CheckSum topologicalHash()
{
std::string s = "substring" + mArgument->topologicalHash().toStdString() ;
return Sha1CheckSum((unsigned char*)s.c_str(),s.length()) ;
}
protected:
virtual void print(int depth) const
{
for(int i=0;i<depth*2;++i) std::cerr << " " ;
std::cerr << "Substring(" << mStart << "," << mLength << ")" << std::endl;
mArgument->print(depth+1) ;
}
virtual float entropy() const
{
return 8*mMaxLength ;
}
virtual MemBlock eval() const
{
MemBlock m = mArgument->eval() ;
return m.subblock(mStart,mLength) ;
}
virtual int length() const { return mLength ; }
virtual void initState() { mArgument->initState() ; mStart=0; mLength=mMinLength ;}
virtual bool nextState()
{
if(mStart+mLength < mArgument->length() && (mMaxLength==0 || mLength < mMaxLength))
{
++mLength ;
return true ;
}
else if(mStart < mArgument->length() - 1 - mMinLength)
{
mLength=mMinLength ;
++mStart ;
return true ;
}
else
{
mLength = mMinLength ;
mStart = 0 ;
return mArgument->nextState() ;
}
}
private:
int mStart ; // parameter 1
int mLength ; // parameter 2
int mMinLength ;
int mMaxLength ;
Expression *mArgument ;
};
class Exp_concat: public Expression
{
public:
Exp_concat(Expression *e1,Expression *e2)
{
mArgument1 = e1 ;
mArgument2 = e2 ;
}
virtual ~Exp_concat() { delete mArgument1; delete mArgument2 ; }
virtual Expression *deepCopy() const{ return new Exp_concat(mArgument1->deepCopy(),mArgument2->deepCopy()) ; }
virtual Sha1CheckSum topologicalHash()
{
std::string s = "concat" + mArgument1->topologicalHash().toStdString() + mArgument2->topologicalHash().toStdString() ;
return Sha1CheckSum((unsigned char*)s.c_str(),s.length()) ;
}
protected:
virtual void print(int depth) const
{
for(int i=0;i<depth*2;++i) std::cerr << " " ;
std::cerr << "Concat()" << std::endl;
mArgument1->print(depth+1) ;
mArgument2->print(depth+1) ;
}
virtual float entropy() const
{
return 1 + mArgument1->entropy() + mArgument2->entropy() ;
}
virtual MemBlock eval() const
{
MemBlock m1 = mArgument1->eval() ;
MemBlock m2 = mArgument2->eval() ;
return m1+m2 ;
}
virtual int length() const { return mArgument1->length() + mArgument2->length() ; }
virtual void initState() { mArgument1->initState(); mArgument2->initState(); }
virtual bool nextState()
{
if(mArgument1->nextState())
return true ;
mArgument1->initState() ;
return mArgument2->nextState() ;
}
private:
Expression *mArgument1 ;
Expression *mArgument2 ;
};
static bool MemBlockMatch(const MemBlock& input_hex,const MemBlock& b)
{
if( input_hex == b)
return true;
if(b.size() < input_hex.size())
return false ;
if(input_hex.isSubBlock(b))
return true ;
return false ;
}
// Checks that
static bool AsciiMatch(const MemBlock& input_asc,const MemBlock& b)
{
if(b.size() < input_asc.size())
return false ;
if(input_asc.isSubBlock(b))
return true ;
MemBlock b_asc(b) ;
for(uint32_t i=0;i<b.size();++i)
b_asc[i] = (b[i] % 26)+'A' ;
if(input_asc.isSubBlock(b_asc))
return true ;
return false ;
}
typedef std::pair<float,Expression*> QueueItem ;
bool operator<(const QueueItem& e1,const QueueItem& e2)
{
return e1.first < e2.first ;
}
typedef std::map<Sha1CheckSum,Expression *> ExpressionQueue;
static bool push_into_queue(ExpressionQueue& queue,Expression *e)
{
if(queue.find(e->topologicalHash()) != queue.end())
{
delete e ;
return false ;
}
queue[e->topologicalHash()] = e ;
return true ;
}
Expression *random_select_from_queue(const ExpressionQueue& q)
{
if(q.empty())
return NULL ;
uint32_t n = lrand48()%q.size() ;
ExpressionQueue::const_iterator it(q.begin()) ;
for(int i=0;i<n;++i)
++it ;
return it->second;
}
bool remove_from_queue(ExpressionQueue& q,Expression *e)
{
ExpressionQueue::iterator it = q.find(e->topologicalHash()) ;
if(q.end() == it)
return false ;
q.erase(it) ;
return true ;
}
static bool grosbelu(const std::string& input,Expression *& exp,uint64_t& tries)
{
// read config file.
//
Config conf(input.c_str()) ;
std::vector<std::string> hexa_strings = conf.getMultipleStringValue("HEXA_STRINGS",std::vector<std::string>()) ;
std::vector<std::string> ascii_strings = conf.getMultipleStringValue("ASCII_STRINGS",std::vector<std::string>()) ;
std::string hexa_passphrase_string = conf.getStringValue("HEXA_WPA_PASSPHRASE","") ;
std::string ascii_passphrase_string = conf.getStringValue("ASCII_WPA_PASSPHRASE","") ;
if(hexa_passphrase_string.empty() && ascii_passphrase_string.empty())
throw std::runtime_error("No passphrase string supplied. Please use variable {HEXA,ASCII}_WPA_PASSPHRASE in your config file.") ;
// init queue
//
ExpressionQueue queue ;
tries = 0 ;
MemBlock input_asc = MemBlock::fromString(ascii_passphrase_string) ;
MemBlock input_hex = MemBlock::fromHex(hexa_passphrase_string) ;
std::set<Sha1CheckSum> queue_content ;
// 1 - fill the priority queue with some elements, until enough of them, possibly combining them together.
// push some stuff into the queue
for(uint32_t i=0;i<hexa_strings.size();++i)
{
std::cerr << "Adding hexadecimal constant \"" << hexa_strings[i] << "\"" << std::endl;
push_into_queue(queue,new Exp_const(MemBlock::fromHex(hexa_strings[i]))) ;
}
for(uint32_t i=0;i<ascii_strings.size();++i)
{
std::cerr << "Adding ascii constant \"" << ascii_strings[i] << "\"" << std::endl;
push_into_queue(queue,new Exp_const(MemBlock::fromString(ascii_strings[i]))) ;
}
// add a 2 bytes counter
push_into_queue(queue,new Exp_numeric_counter(4)) ;
push_into_queue(queue,new Exp_hash_generic(new Exp_ascii_counter(4,'a'))) ;
push_into_queue(queue,new Exp_ascii_counter(4,'A')) ;
// add a date counter
//push_into_queue(queue,new Exp_date(2)) ;
for(int i=0;i<30;++i)
{
// take queue content with least entropy and combine them together
Expression *elem1 = random_select_from_queue(queue) ;
if(dynamic_cast<Exp_substring*>(elem1) == NULL) push_into_queue(queue,new Exp_substring(elem1->deepCopy())) ;
push_into_queue(queue,new Exp_hash_generic(elem1->deepCopy())) ; // could be hash(hash(...)), so no check necessary
Expression *elem2 = random_select_from_queue(queue) ;
push_into_queue(queue,new Exp_concat(elem1->deepCopy(),elem2->deepCopy())) ;
}
// remove all combinations that are substrings, since we test them as a postpone match
for(ExpressionQueue::iterator it(queue.begin());it!=queue.end();)
if(dynamic_cast<Exp_substring*>(it->second) != NULL)
{
ExpressionQueue::iterator tmp(it) ;
++tmp;
delete it->second ;
queue.erase(it) ;
it=tmp;
std::cerr << "Removed one expression that is a top substr" <<std::endl;
}
else
++it ;
// init all expressions that depend on parameters
//
std::cerr << "Input queue:" << std::endl;
for(ExpressionQueue::iterator it(queue.begin());it!=queue.end();++it)
{
std::cerr << "Queue element: " << std::endl;
it->second->initState() ;
it->second->show() ;
}
std::cerr << "************* END ***********" << std::endl;
// 2 - take al elements and apply parameters to them, see what we get.
while(!queue.empty())
{
Expression *e = random_select_from_queue(queue) ;
// use new parameter values.
//
int i=0 ;
for(;i<100;++i)
{
MemBlock b = e->eval() ;
if(!hexa_passphrase_string.empty())
{
//std::cerr << e->length() << std::endl;
if(drand48() < 0.00001)
{
std::cerr << "Testing " << input_hex.toHex() << " toward " << b.toHex() << std::endl;
e->show();
}
if(MemBlockMatch(input_hex,b))
{
std::cerr << "matched " << input_hex.toHex() << " with " << b.toHex() << std::endl;
exp = e ;
return true ;
}
}
if(!ascii_passphrase_string.empty())
{
//std::cerr << e->length() << std::endl;
if(drand48() < 0.00001)
{
std::cerr << "Testing ascii " << input_asc.toString() << " toward " << b.toHex() << std::endl;
e->show();
}
if(AsciiMatch(input_asc,b))
{
std::cerr << "matched " << input_asc.toString() << " with " << b.toHex() << std::endl;
exp = e ;
return true ;
}
}
++tries ;
if(!e->nextState())
{
remove_from_queue(queue,e) ;
delete e ;
std::cerr << "Poped expression out of the queue. Queue size now =" << queue.size() << std::endl;
break ;
}
}
}
exp=NULL ;
return false ;
}
int main(int argc,char *argv[])
{
try
{
argstream as(argc,argv) ;
std::string config_file = "example.cfg" ;
as >> parameter('i',"config",config_file,"config file to work on",true)
>> help() ;
as.defaultErrorHandling() ;
OpenSSL_add_all_digests() ;
Expression *exp_found ;
uint64_t tries;
if(grosbelu(config_file,exp_found,tries))
{
std::cerr << "Found! Expression is: " << std::endl;
exp_found->show() ;
}
else
std::cerr << "Nothing found, sorry. Number of expressions tested: " << tries << std::endl;
return 0 ;
}
catch(std::exception& e)
{
std::cerr << "Exception never handled: " << e.what() << std::endl;
return 1 ;
}
}