-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
212 lines (177 loc) · 7.6 KB
/
Copy pathmain.cpp
File metadata and controls
212 lines (177 loc) · 7.6 KB
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
#include <argparse/argparse.hpp>
#include <string>
#include <vector>
#include <fstream>
#include <fmt/color.h>
#include "hashes/broken_hash.h"
#include "attacks/attack.h"
#include "attacks/dictionary.h"
#include "attacks/brute_force.h"
#include "file_manager.h"
#include "mode.h"
int main(int argc, char **argv) {
argparse::ArgumentParser program("CRACKER++");
program.add_argument("mode")
.help("Mode: dictionary, brute or identify");
program.add_argument("--hash-file", "-hl")
.help("File containing hashes");
program.add_argument("--hash")
.help("Single hash value");
program.add_argument("--wordlist", "-w")
.help("Path to wordlist file");
program.add_argument("--word", "-sw")
.help("Single word");
program.add_argument("--charset")
.help("Character set for brute-force");
program.add_argument("--max-len", "-l")
.help("Maximum length of passwords to try")
.default_value(8)
.scan<'i', int>();
program.add_argument("--threads", "-t")
.help("Number of threads to use")
.default_value(1)
.scan<'i', int>();
program.add_argument("--hash-type", "-ht")
.help("Type of hash algorithm (e.g., sha256, md5, sha1)");
program.add_argument("--verbose", "-vb")
.default_value(false)
.implicit_value(true)
.help("Write all of the possible hash algorithms while identifying");
program.add_argument("--to-file", "-o")
.help("Save output to file");
try {
program.parse_args(argc, argv);
}
catch (const std::runtime_error &err) {
std::cerr << err.what() << std::endl;
std::cerr << program;
return 1;
}
std::string charset = program.present("--charset") ? program.get<std::string>("--charset") : "";
std::string word = program.present("--word") ? program.get<std::string>("--word") : "";
std::string hashList = program.present("--hash-file") ? program.get<std::string>("--hash-file") : "";
std::string hashValue = program.present("--hash") ? program.get<std::string>("--hash") : "";
std::string wordList = program.present("--wordlist") ? program.get<std::string>("--wordlist") : "";
bool verbose = program.get<bool>("--verbose");
int maxLen = program.get<int>("max-len");
int numThreads = program.get<int>("threads");
std::string hashType = program.present("--hash-type") ? program.get<std::string>("--hash-type") : "";
std::string toFile = program.present("--to-file") ? program.get<std::string>("--to-file") : "";
if (hashList.empty() && hashValue.empty()) {
std::cerr << "Either --hash-file or --hash-value must be provided." << std::endl;
return 1;
}
bool notSave = toFile.empty();
Attack *attack = nullptr;
std::string outputString;
auto modeStr = program.get<std::string>("mode");
Mode mode = parseMode(modeStr);
switch (mode) {
case Mode::Dictionary: {
try {
if (!wordList.empty() && !hashList.empty()) {
attack = new DictionaryAttack(hashList, wordList, DataSource::FILE_PATHS, hashType);
} else if (!hashList.empty() && !word.empty()) {
attack = new DictionaryAttack(hashList, word, DataSource::HASH_LIST, hashType);
} else if (!wordList.empty() && !hashValue.empty()) {
attack = new DictionaryAttack(hashValue, wordList, DataSource::WORD_LIST, hashType);
} else if (!word.empty()) {
attack = new DictionaryAttack(hashValue, word, DataSource::STRINGS, hashType);
} else {
std::cerr << "Either --wordlist or --word must be provided." << std::endl;
return 1;
}
} catch (const std::exception &err) {
std::cerr << err.what() << std::endl;
}
break;
}
case Mode::Brute: {
try {
if (!hashList.empty()) {
attack = new BruteForce(hashList, DataSource::HASH_LIST, hashType, maxLen, charset);
} else {
attack = new BruteForce(hashValue, DataSource::STRINGS, hashType, maxLen, charset);
}
} catch (const std::exception &err) {
std::cerr << err.what() << std::endl;
}
break;
}
case Mode::Identify: {
try {
std::stringstream outputStringStream;
if (notSave) {
std::cout << fmt::format(fg(fmt::color::beige), "HASH") << " | ";
std::cout << fmt::format(fg(fmt::color::red), "PROBABLE HASH ALGORITHM") << std::endl << std::endl;
}
if (!hashList.empty()) {
HashIdentifier identifier = HashIdentifier(hashList);
if (verbose) {
outputStringStream << identifier.toStringVerbose();
} else {
outputStringStream << identifier.toString();
}
} else {
std::unordered_set<std::string> set;
set.insert(hashValue);
GroupedHashes hash = GroupedHashes(set);
if (notSave) {
if (verbose) {
outputStringStream << hash.toStringVerbose();
} else {
outputStringStream << hash.toString();
}
} else {
if (verbose) {
outputStringStream << hash.forFileVerbose();
} else {
outputStringStream << hash.forFile();
}
}
}
outputString = outputStringStream.str();
} catch (const std::exception &err) {
std::cerr << err.what() << std::endl;
}
break;
}
case Mode::Unknown:
default: {
std::cerr << "Provided mode \"" << modeStr << "\" is invalid." << std::endl;
std::cerr << program;
return 1;
}
}
if (attack != nullptr) {
attack->startAttack(numThreads);
std::vector<BrokenHash> brokenHashes = attack->getBrokenHashes();
std::stringstream outputStringStream;
if (!brokenHashes.empty()) {
if (notSave) {
outputStringStream << fmt::format("{} | {} | {}",
fmt::format(fg(fmt::color::beige), "HASH"),
fmt::format(fg(fmt::color::green_yellow), "PLAIN TEXT"),
fmt::format(fg(fmt::color::red), "HASH ALGORITHM")) <<
std::endl << std::endl;
for (const auto &brokenHash: brokenHashes) {
outputStringStream << brokenHash.toString();
}
} else {
for (const auto &brokenHash: brokenHashes) {
outputStringStream << brokenHash.forFile();
}
}
} else {
std::cout << fmt::format(fg(fmt::color::red), "NO HASH BROKEN");
}
outputString = outputStringStream.str();
}
std::cout<< std::endl;
if (notSave) {
std::cout << std::endl << outputString;
} else {
FileManager::saveOutput(toFile, outputString);
}
return 0;
}