-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.cpp
228 lines (173 loc) · 6.43 KB
/
user.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
#include "user.h"
std::map<QString, QString> user::m_halgorithm_types = {
{"1", "MD5"},
{"2a", "Blowfish"},
//{"2y", "Eksblowfish"},
{"5", "SHA-256"},
{"6", "SHA-512"}
};
user::user(const QString& username, const QString& algid, const QString& salt, const QString& hashofpass, QObject *parent) : QObject(parent)
{
Q_UNUSED(parent);
setUser(username, algid, salt, hashofpass);
}
user::user(const QJsonObject& json, QObject *parent) : QObject(Q_NULLPTR)
{
Q_UNUSED(parent);
if (json.contains("User") && json["User"].isString())
m_username = json["User"].toString();
else
throw ParsingException{"\"User\" field not found in saved data"};
if (json.contains("AlgId") && json["AlgId"].isString())
m_algid = json["AlgId"].toString();
else
throw ParsingException{"\"AlgId\" field not found in saved data"};
if (json.contains("Salt") && json["Salt"].isString())
m_salt = json["Salt"].toString();
else
throw ParsingException{"\"Salt\" field not found in saved data"};
if (json.contains("Hash") && json["Hash"].isString())
m_hashofpass = json["Hash"].toString();
else
throw ParsingException{"\"Hash\" field not found in saved data"};
}
user::user(const user& other) : QObject(Q_NULLPTR)
{
setUser(other.getName(), other.getAlgid(), other.getSalt(), other.getHash());
}
user user::operator= (const user& other)
{
user target;
target.setUser(other.getName(), other.getAlgid(), other.getSalt(), other.getHash());
return target;
}
void user::setUser(const QString& username, const QString& algid, const QString& salt, const QString& hashofpass)
{
this->m_username = username;
this->m_algid = algid;
this->m_salt = salt;
this->m_hashofpass = hashofpass;
}
void user::write_to_json(QJsonObject& json) const
{
json["User"] = m_username;
json["AlgId"] = m_algid;
json["Salt"] = m_salt;
json["Hash"] = m_hashofpass;
}
void user::collision_attack()
{
// init
collisionAttackTask_CPU::setNewTarget(this);
// username + dictionary attack
QThreadPool::globalInstance()->start(new collisionAttackTask_CPU(collisionAttackTask_CPU::Heuristic::full));
// restricted attack
limited_attack(1);
// unrestricted eternal attack
unlimited_attack(1);
// wait until interruption
QThreadPool::globalInstance()->waitForDone();
emit signal_attackFinished();
return;
}
void user::resumed_collision_attack()
{
// init
collisionAttackTask_CPU::setNewTarget(this);
/////////////////////////////////
// determine cache threshold (upper bound), start each cached ("below threshold") task with its respective gained savepoint
/////////////////////////////////
// maximum reached milestone
collisionAttackTask_CPU::Heuristic upperbound_heur = collisionAttackTask_CPU::Heuristic::full;
uint upperbound_password_length = 1;
while (true)
{
if (collisionAttackTask_CPU::attackStatus() != 1)
break;
QPair<QString, collisionAttackTask_CPU::Heuristic> task_init = collisionAttackTask_CPU::getCacheInit();
if (task_init.first == QString(""))
break;
switch(task_init.second)
{
case collisionAttackTask_CPU::Heuristic::none:
upperbound_heur = collisionAttackTask_CPU::Heuristic::none;
if (upperbound_password_length < (uint)task_init.first.length())
upperbound_password_length = (uint)task_init.first.length();
break;
case collisionAttackTask_CPU::Heuristic::partial:
if (upperbound_heur != collisionAttackTask_CPU::Heuristic::none)
{
upperbound_heur = collisionAttackTask_CPU::Heuristic::partial;
if (upperbound_password_length < (uint)task_init.first.length())
upperbound_password_length = (uint)task_init.first.length();
}
break;
case collisionAttackTask_CPU::Heuristic::full:
break;
default:
continue;
break;
}
QThreadPool::globalInstance()->start(new collisionAttackTask_CPU(task_init.second, task_init.first.length(), task_init.first));
}
// some cached attack found => go beyond
if (upperbound_password_length != 1)
upperbound_password_length++;
/////////////////////////////////
// start "above threshold" tasks
/////////////////////////////////
// restricted attack
if ((upperbound_heur == collisionAttackTask_CPU::Heuristic::full || upperbound_heur == collisionAttackTask_CPU::Heuristic::partial))
{
limited_attack(upperbound_password_length);
upperbound_password_length = 1;
}
// unrestricted eternal attack
unlimited_attack(upperbound_password_length);
// wait until interruption
QThreadPool::globalInstance()->waitForDone();
emit signal_attackFinished();
return;
}
void user::limited_attack(const uint& initial_password_length)
{
if (initial_password_length <= collisionAttackTask_CPU::getOptimalMaxPL())
{
for (unsigned char pass_length = initial_password_length; pass_length <= collisionAttackTask_CPU::getOptimalMaxPL(); pass_length++)
{
if (collisionAttackTask_CPU::attackStatus() != 1)
break;
QThreadPool::globalInstance()->start(new collisionAttackTask_CPU(collisionAttackTask_CPU::Heuristic::partial, pass_length));
}
}
}
void user::unlimited_attack(const uint& initial_password_length)
{
uint current_password_length = initial_password_length;
while (true)
{
if (collisionAttackTask_CPU::attackStatus() != 1)
break;
else if (QThreadPool::globalInstance()->activeThreadCount() < QThreadPool::globalInstance()->maxThreadCount()) // based on CPUs quant
{
QThreadPool::globalInstance()->start(new collisionAttackTask_CPU(collisionAttackTask_CPU::Heuristic::none, current_password_length));
current_password_length++;
}
else
QThread::sleep(2);
}
}
QString user::getAlgorithmName(const QString& algid)
{
if (! m_halgorithm_types.count(algid))
return QString("");
return m_halgorithm_types.at(algid);
}
void user::attack_interrupt()
{
collisionAttackTask_CPU::stopAttack();
}
void user::attack_save()
{
collisionAttackTask_CPU::saveAttack();
}