-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbruteforce_range.h
105 lines (77 loc) · 2.4 KB
/
bruteforce_range.h
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
#pragma once
#include <iostream>
#include <chrono>
#include <array>
#include <fstream>
#include <vector>
#include <unordered_set>
#include <cassert>
enum class PacketStatus : uint32_t {
InProgress = 1,
Done = 2,
};
typedef unsigned char Packet[32];
struct Packets
{
PacketStatus *statuses;
Packet *data;
};
using Block16 = std::array<uint8_t, 16>;
const int SHA_ROUNDS = 64;
const int BATCH_SIZE = 16 * 1024 * 1024;
// const unsigned int PACKETS_SIZE = BATCH_SIZE * sizeof(Packet);
const uint64_t MAX_TICKS_DIFF = 1000;
template <typename T>
class BruteforceParam {
T min_;
T max_;
T current_;
T step_;
public:
BruteforceParam(T min, T max, T step=1) :min_(min), max_(max), current_(min), step_(step) {
assert(step != 0);
assert(min <= max);
assert((max - min) % step == 0);
}
T get() const { return current_; }
T min() const { return min_; }
T max() const { return max_; }
T keyspace() const { return (max_ - min_ + step_) / step_; }
// Increases current. value. Returns false on overflow.
bool next() {
if (current_ < max_) {
current_ += step_;
return true;
}
current_ = min_;
return false;
}
};
class BruteforceRange {
// For work progress calculation
uint32_t tried_;
uint32_t keyspace_;
uint32_t pid_;
std::vector<uint32_t> tids_;
uint64_t start_when_; // start bruting at this chunk
uint64_t done_when_; // stop bruting at this chunk
BruteforceParam<uint32_t> gettickcount_;
BruteforceParam<uint64_t> filetime_;
BruteforceParam<uint64_t> perfcounter_;
BruteforceParam<uint64_t> perfcounter_xor_;
void perfcounter_xor_set();
// Moves the internal state one step forward. Returns false if the range is done.
bool forward();
public:
BruteforceRange( uint32_t pid, std::vector<uint32_t> tids, BruteforceParam<uint32_t> gettickcount,
BruteforceParam<uint64_t> filetime, BruteforceParam<uint64_t> perfcounter);
// Returns false if the range is done, otherwise sets new key in packet.
bool next(Packet target, PacketStatus *status);
// How many keys were tried?
uint64_t current() const;
// How many keys are there to try?
uint64_t keyspace() const;
double progress() const;
static BruteforceRange parse(std::string path);
void limits(uint64_t start, uint64_t end);
};