Add argument parsing and MIT license

yay!
This commit is contained in:
Lily Tsuru 2023-08-22 05:03:49 -04:00
parent 8e657dbdc6
commit 91b3ae0dab
4 changed files with 161 additions and 30 deletions

7
LICENSE Normal file
View File

@ -0,0 +1,7 @@
Copyright 2023 Lily Tsuru <lily.modeco80@protonmail.ch>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@ -1,40 +1,133 @@
#include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp>
#include <format>
#include <thread>
#include "ansi.hpp"
#include "fprint.hpp" #include "fprint.hpp"
#include "worker.hpp" #include "worker.hpp"
namespace asio = boost::asio; struct Arguments {
bool exact { false };
std::uint32_t startLength = 2;
std::uint32_t endLength = 8;
void BruteMain() { std::uint32_t targetHash;
asio::thread_pool pool(swbf::THREAD_COUNT);
swbf::BruteforceWorker::Options options { .targetHash = 0x2c73af55, .exact = true, .startLength = 8 }; /// Parse arguments from the main() argc/argv.
static Arguments FromArgv(int argc, char** argv) {
Arguments args;
args.progname = argv[0];
auto threadIndex = 0u; // no options provided
if(argc == 1) {
// post worker threads to run onto the thread pool & wait for them to complete args.DispHelp();
for(int i = 0; i < swbf::THREAD_COUNT; ++i) std::exit(1);
asio::post(pool, [&, tid = threadIndex++]() {
swbf::BruteforceWorker state(tid, options);
state.Bruteforce('a' + tid);
});
bool done = false;
while(!done) {
done = swbf::DisplayThreadInfo();
std::this_thread::sleep_for(std::chrono::milliseconds(66));
} }
pool.join(); // just in case! // non-pepper getopt(). I'm too lazy to make it any better though
} for(int i = 1; i < argc; ++i) {
if(argv[i][0] == '-' && argv[i][1] != '\0') {
char sw = argv[i][1];
int main() { // hacky. VERY hacky.
BruteMain(); auto cheap_atoi = [](char c) { return c - '0'; };
switch(sw) {
// flag options
case 'X': // eXact
args.exact = true;
break;
// value options
case 's': // start length
if(argv[i + 1] == nullptr || argv[i + 1][0] == '-') {
swbf::fprint(stdout, "Invalid value passed to -s\n");
std::exit(1);
}
args.startLength = cheap_atoi(*argv[i + 1]);
i++;
break;
case 'e': // end length
if(argv[i + 1] == nullptr || argv[i + 1][0] == '-') {
swbf::fprint(stdout, "Invalid value passed to -e\n");
std::exit(1);
}
args.endLength = cheap_atoi(*argv[i + 1]);
i++;
break;
// terminals
case '?':
case 'h': {
args.DispHelp();
std::exit(0);
}
default:
swbf::fprint(stdout, "Unknown command-line switch '-{}'\n", sw);
args.DispHelp();
std::exit(1);
}
} else {
// Assume any non-positional argument is the hash value
// (also, this will take *any* base, not just hex, but hex is the most useful)
args.targetHash = strtol(argv[i], nullptr, 0);
}
}
return args;
}
bool Validate() const {
if(!targetHash) {
swbf::fprint(stdout, "No hash provided\n");
DispHelp();
return false;
}
if((startLength < 2) || (startLength > endLength)) {
swbf::fprint(stdout, "Invalid start/exact length\n");
DispHelp();
return false;
}
// omit checking for -e validity if we don't really need it
if(!exact) {
if((endLength > 8) || (endLength < startLength)) {
swbf::fprint(stdout, "Invalid end length\n");
DispHelp();
return false;
}
}
return true;
}
private:
char* progname {};
void DispHelp() const {
swbf::fprint(stdout,
// clang-format off
"SWSF Bruteforcer, (C) 2023 Lily Tsuru under the MIT License\n"
"Usage: {} [-?/-h] [-X] [-s START_LENGTH] [-e END_LENGTH] <hash to crack, in hexadecimal>\n"
" -X Test exact length. Uses value of -s; -e is ignored in this mode.\n"
" -s Start length to check. Must be greater than or equal to 2 and less than the end size (default 2)\n"
" -e End length to check. Must be greater than the start size, and less than or equal to 8 (default 8)\n"
" -?/-h Show this help message (and exit)\n",
progname
// clang-format on
);
}
};
int main(int argc, char** argv) {
auto args = Arguments::FromArgv(argc, argv);
if(!args.Validate()) {
return 1;
}
swbf::BruteforceWorker::Options options {
.targetHash = args.targetHash, .exact = args.exact, .startLength = args.startLength, .endLength = args.endLength
};
BruteMain(options);
return 0; return 0;
} }

View File

@ -1,12 +1,18 @@
#include "worker.hpp" #include "worker.hpp"
#include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/thread_pool.hpp>
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include <thread>
#include "ansi.hpp" #include "ansi.hpp"
#include "fprint.hpp" #include "fprint.hpp"
#include "scramble.hpp" #include "scramble.hpp"
namespace asio = boost::asio;
namespace swbf { namespace swbf {
/// This class implements the display of progress /// This class implements the display of progress
struct ThreadInfoData { struct ThreadInfoData {
@ -70,7 +76,7 @@ namespace swbf {
if(options.exact) { if(options.exact) {
BruteforceForLength(prefix, options.startLength); BruteforceForLength(prefix, options.startLength);
} else { } else {
for(std::uint32_t i = options.startLength; i <= options.endLength; ++i) for(std::uint32_t i = options.startLength; i < options.endLength; ++i)
BruteforceForLength(prefix, i); BruteforceForLength(prefix, i);
} }
@ -106,4 +112,26 @@ namespace swbf {
return infoData[threadIndex]; return infoData[threadIndex];
} }
void BruteMain(const swbf::BruteforceWorker::Options& options) {
asio::thread_pool pool(swbf::THREAD_COUNT);
auto threadIndex = 0u;
// post worker threads to run onto the thread pool & wait for them to complete
for(int i = 0; i < swbf::THREAD_COUNT; ++i)
asio::post(pool, [&, tid = threadIndex++]() {
swbf::BruteforceWorker state(tid, options);
state.Bruteforce('a' + tid);
});
bool done = false;
while(!done) {
done = swbf::DisplayThreadInfo();
std::this_thread::sleep_for(std::chrono::milliseconds(66));
}
pool.join(); // just in case!
}
} // namespace swbf } // namespace swbf

View File

@ -9,9 +9,6 @@ namespace swbf {
/// private structure /// private structure
struct ThreadInfoData; struct ThreadInfoData;
/// returns true when all threads finished
bool DisplayThreadInfo();
/// Per-thread worker for brute-forcing /// Per-thread worker for brute-forcing
struct BruteforceWorker { struct BruteforceWorker {
/// Options for the worker. /// Options for the worker.
@ -44,4 +41,10 @@ namespace swbf {
std::uint32_t hash; std::uint32_t hash;
}; };
/// returns true when all threads finished
bool DisplayThreadInfo();
void BruteMain(const swbf::BruteforceWorker::Options& options);
} // namespace swbf } // namespace swbf