-
Notifications
You must be signed in to change notification settings - Fork 4
/
bruteforce_filters.h
338 lines (251 loc) · 8.97 KB
/
bruteforce_filters.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
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
#pragma once
#include "common.h"
#include <vector>
#include <tuple>
#include <string>
#include <cuda_runtime_api.h>
constexpr uint8_t KeySizeBytes = sizeof(uint64_t);
constexpr uint8_t KeySizeBits = sizeof(uint64_t) * 8;
/**
* Filters for +1 bruteforce.
* Will apply if `include(value) && !exclude(value)`
*
* Very little performance increase over simple +1 with exclude
* The filters are not optimized quite good and have a lot of `if` blocks
*/
struct BruteforceFilters
{
struct Flags
{
using Type = uint64_t;
enum : Type
{
//
None = 0,
// filter function return true if key has more than 6 consecutive 0 bits
Max6ZerosInARow = (1 << 0),
// filter function return true if key has more than 6 consecutive 1 bits
Max6OnesInARow = (1 << 1),
// filter function return true if key has patterns like 11:22:33:44.. or FF:EE:DD:CC
// 6 bytes by default
BytesIncremental = (1 << 5),
// filter function return true if key has repeating patterns like xx11:11:11:11xx or xxAA:AA:AA:AAxx
BytesRepeat4 = (1 << 6),
// filter function return true if key consist from only ascii numbers
AsciiNumbers = (1 << 11),
// filter function return true if key consist from only letters 'a'-'z' 'A'-'Z'
AsciiAlpha = (1 << 12),
// filter function return true if key consist from ascii letters and numbers
AsciiAlphaNum = AsciiAlpha | AsciiNumbers,
// filter function return true if key consist from only ASCII special symbols like '^%#&*
AsciiSpecial = (1 << 13),
// filter function return true if key consist from only ASCII typed characters
AsciiAny = AsciiAlphaNum | AsciiSpecial,
//
All = (uint64_t)-1,
};
__host__ __device__ static inline bool HasAll(Type test, Type check)
{
return check == (test & check);
}
__host__ __device__ static inline bool HasAny(Type test, Type check)
{
return (test & check);
}
};
public:
std::string toString(Flags::Type flags) const;
std::string toString() const;
// Return true if key pass current filters
__host__ __device__ inline bool Pass(uint64_t key) const;
__host__ __device__ inline static bool check_filters(uint64_t key, Flags::Type filter);
private:
__host__ __device__ static bool all_any_ascii(uint64_t key);
__host__ __device__ static bool all_ascii_num(uint64_t key);
__host__ __device__ static bool all_ascii_alpha(uint64_t key);
__host__ __device__ static bool all_ascii_symbol(uint64_t key);
template<uint8_t ValueMin, uint8_t ValueMax>
__host__ __device__ inline static bool all_min_max(uint64_t key);
template<uint8_t bit, uint8_t MaxCount = 6>
__host__ __device__ inline static bool has_consecutive_bits(uint64_t key);
template<uint8_t MaxCount = 4>
__host__ __device__ inline static bool has_consecutive_bytes(uint64_t key);
template<uint8_t MaxCount = 6>
__host__ __device__ inline static bool has_incremental_pattern(uint64_t key);
private:
static const std::vector<std::tuple<Flags::Type, const char*>> FilterNames;
public:
// Filter for keys to include.
// WARNING:
// Could be executed INFINITELY LONG TIME
// e.g. start: 0x00000000001 filter SmartFilterFlags::AsciiAny
// it will took around trillions and trillions operations just to get to the first valid with simple +1
// In case of specific input - use dictionary, pattern or alphabet
Flags::Type include = Flags::All;
// Filter for keys to exclude
Flags::Type exclude = Flags::None;
// A global for all CUDA threads uint64 value which is used for generating filtered keys
// it is accessed via atomicAdd function.
uint64_t sync_key = 0;
};
template<uint8_t ValueMin, uint8_t ValueMax>
__host__ __device__ bool BruteforceFilters::all_min_max(uint64_t key)
{
// for logical AND start should be with true
bool result = true;
uint8_t* bPtrKey = (uint8_t*)&key;
UNROLL
for (uint8_t i = 0; i < KeySizeBytes; ++i)
{
// TODO: Some vector instruction here
result &= bPtrKey[i] >= ValueMin && bPtrKey[i] <= ValueMax;
}
return result;
}
template<uint8_t bit, uint8_t MaxCount /*= 6*/>
__host__ __device__ bool BruteforceFilters::has_consecutive_bits(uint64_t key)
{
uint8_t result = false;
uint64_t mask = (1 << MaxCount) - 1;
key = bit ? key : ~key;
UNROLL
for (uint8_t i = 0; i < KeySizeBits; ++i)
{
// inverse - filter pass if no consecutive bits
result |= (key & mask) == mask;
key = key >> 1;
}
return result;
}
template<uint8_t MaxCount /*= 4*/>
__host__ __device__ bool BruteforceFilters::has_consecutive_bytes(uint64_t key)
{
// for logical OR start should be with false
bool result = false;
uint8_t index = 0;
uint8_t* bPtrKey = (uint8_t*)&key;
UNROLL
for (uint8_t i = 1; i < KeySizeBytes; ++i)
{
bool equal = bPtrKey[i] == bPtrKey[index];
index = equal * index + (1 - equal) * i;
result |= (i - index) >= (MaxCount - 1);
}
return result;
}
template<uint8_t MaxCount /*= 6*/>
__host__ __device__ bool BruteforceFilters::has_incremental_pattern(uint64_t key)
{
// for logical OR start should be with false
bool result = false;
uint8_t index = 0;
uint8_t* bPtrKey = (uint8_t*)&key;
UNROLL
for (uint8_t i = 1; i < KeySizeBytes; ++i)
{
uint8_t deltaIndex = (i - index);
#ifdef __CUDA_ARCH__
uint8_t asbDeltaValue = __sad(bPtrKey[i], bPtrKey[index], 0);
#else
uint8_t asbDeltaValue = abs(bPtrKey[i] - bPtrKey[index]);
#endif
bool match = asbDeltaValue == (0x11 * deltaIndex);
index = match * index + (1 - match) * i;
result |= deltaIndex >= (MaxCount - 1);
}
return result;
}
__host__ __device__ inline bool BruteforceFilters::check_filters(uint64_t key, Flags::Type filter)
{
bool key_has_any = false;
// fastest should go first
if (!key_has_any && Flags::HasAll(filter, Flags::AsciiAny))
{
key_has_any |= all_any_ascii(key);
}
if (!key_has_any && Flags::HasAny(filter, Flags::AsciiNumbers))
{
key_has_any |= all_ascii_num(key);
}
if (!key_has_any && Flags::HasAny(filter, Flags::AsciiAlpha))
{
key_has_any |= all_ascii_alpha(key);
}
if (!key_has_any && Flags::HasAny(filter, Flags::AsciiSpecial))
{
key_has_any |= all_ascii_symbol(key);
}
//
if (!key_has_any && Flags::HasAny(filter, Flags::Max6OnesInARow))
{
key_has_any |= has_consecutive_bits<1>(key);
}
if (!key_has_any && Flags::HasAny(filter, Flags::Max6ZerosInARow))
{
key_has_any |= has_consecutive_bits<0>(key);
}
if (!key_has_any && Flags::HasAny(filter, Flags::BytesRepeat4))
{
key_has_any |= has_consecutive_bytes(key);
}
if (!key_has_any && Flags::HasAny(filter, BruteforceFilters::Flags::BytesIncremental))
{
key_has_any |= has_incremental_pattern(key);
}
return key_has_any;
}
__host__ __device__ inline bool BruteforceFilters::Pass(uint64_t key) const
{
bool pass = true;
if (include != Flags::All && include != Flags::None)
{
// Include keys match patterns
pass &= check_filters(key, include);
}
if (exclude != Flags::None && exclude != Flags::All)
{
// Exclude keys which match patterns
pass &= !check_filters(key, exclude);
}
return pass;
}
__host__ __device__ inline bool BruteforceFilters::all_any_ascii(uint64_t key)
{
constexpr uint8_t value_min = '!';
constexpr uint8_t value_max = '~';
return all_min_max<value_min, value_max>(key);
}
__host__ __device__ inline bool BruteforceFilters::all_ascii_num(uint64_t key)
{
constexpr uint8_t value_min = '0';
constexpr uint8_t value_max = '9';
return all_min_max<value_min, value_max>(key);
}
__host__ __device__ inline bool BruteforceFilters::all_ascii_alpha(uint64_t key)
{
// for logical AND start should be with true
bool result = true;
uint8_t* bPtrKey = (uint8_t*)&key;
UNROLL
for (uint8_t i = 0; i < KeySizeBytes; ++i)
{
result &= (bPtrKey[i] >= 'a' && bPtrKey[i] <= 'z') || (bPtrKey[i] >= 'A' && bPtrKey[i] <= 'Z');
}
return result;
}
__host__ __device__ inline bool BruteforceFilters::all_ascii_symbol(uint64_t key)
{
// for logical AND start should be with true
bool result = true;
uint8_t* bPtrKey = (uint8_t*)&key;
UNROLL
for (uint8_t i = 0; i < KeySizeBytes; ++i)
{
result &=
(bPtrKey[i] >= '!' && bPtrKey[i] <= '/') ||
(bPtrKey[i] >= ':' && bPtrKey[i] <= '@') ||
(bPtrKey[i] >= '[' && bPtrKey[i] <= '`') ||
(bPtrKey[i] >= '{' && bPtrKey[i] <= '~');
}
return result;
}