bkcrack: src/cli/Arguments.hpp Source File

bkcrack 1.8.1 Crack legacy zip encryption with Biham and Kocher's known plaintext attack.

Arguments.hpp
1#ifndef BKCRACK_ARGUMENTS_HPP
2#define BKCRACK_ARGUMENTS_HPP
3
4#include <bkcrack/Data.hpp>
5#include <bkcrack/Keys.hpp>
6#include <bkcrack/types.hpp>
7
8#include <limits>
9#include <map>
10#include <optional>
11#include <unordered_map>
12
14 class Arguments
15{
16public:
18 class Error : public BaseError
19 {
20 public:
22 explicit Error(const std::string& description);
23 };
24
27 Arguments(int argc, const char* const argv[]);
28
33 auto loadData() const -> Data;
34
35 std::optional<std::string> cipherFile;
36 std::optional<std::size_t> cipherIndex;
37 std::optional<std::string> cipherArchive;
38
39 std::optional<std::string> plainFile;
40 std::optional<std::size_t> plainIndex;
41 std::optional<std::string> plainArchive;
42
47 std::size_t plainFilePrefix = 1 << 20;
48
50 int offset = 0;
51
54 std::map<int, std::uint8_t> extraPlaintext;
55
57 bool ignoreCheckByte = false;
58
60 int attackStart = 0;
61
63 std::optional<std::string> password;
64
66 std::optional<Keys> keys;
67
69 std::optional<std::string> decipheredFile;
70
72 bool keepHeader = false;
73
75 std::optional<std::string> decryptedArchive;
76
78 struct ChangePassword
79 {
80 std::string unlockedArchive;
81 std::string newPassword;
82 };
83
84 std::optional<ChangePassword> changePassword;
85
90 struct ChangeKeys
91 {
92 std::string unlockedArchive;
93 Keys newKeys;
94 };
95
96 std::optional<ChangeKeys> changeKeys;
97
99 std::optional<std::vector<std::uint8_t>> bruteforce;
100
102 struct LengthInterval
103 {
105 std::size_t minLength{0};
106
108 std::size_t maxLength{std::numeric_limits<std::size_t>::max()};
109
111 auto operator&(const LengthInterval& other) const -> LengthInterval;
112 };
113
114 std::optional<LengthInterval> length;
115
117 std::optional<std::vector<std::vector<std::uint8_t>>> mask;
118
120 std::string recoveryStart;
121
123 int jobs;
124
126 bool exhaustive = false;
127
129 std::optional<std::string> infoArchive;
130
132 bool version = false;
133
135 bool help = false;
136
137private:
138 const char* const* m_current;
139 const char* const* const m_end;
140
141 std::unordered_map<char, std::bitset<256>> m_charsets;
142 std::unordered_map<char, std::string> m_rawCharsets;
143
144 // Resolve the set of characters denoted by the given charset specification,
145 // recursively resolving referenced charsets from m_rawCharsets and caching results in m_charsets.
146 auto resolveCharset(const std::string& rawCharset) -> std::bitset<256>;
147
148 std::optional<std::string> m_rawBruteforce;
149 std::optional<std::string> m_rawMask;
150
151 auto finished() const -> bool;
152
153 void parseArgument();
154
155 enum class Option
156 {
157 cipherFile,
158 cipherIndex,
159 cipherArchive,
160 plainFile,
161 plainIndex,
162 plainArchive,
163 plainFilePrefix,
164 offset,
165 extraPlaintext,
166 ignoreCheckByte,
167 attackStart,
168 password,
169 keys,
170 decipheredFile,
171 keepHeader,
172 decryptedArchive,
173 changePassword,
174 changeKeys,
175 bruteforce,
176 length,
177 recoverPassword,
178 mask,
179 charset,
180 recoveryStart,
181 jobs,
182 exhaustive,
183 infoArchive,
184 version,
185 help
186 };
187
188 auto readString(const std::string& description) -> std::string;
189 auto readOption(const std::string& description) -> Option;
190 auto readInt(const std::string& description) -> int;
191 auto readSize(const std::string& description) -> std::size_t;
192 auto readHex(const std::string& description) -> std::vector<std::uint8_t>;
193 auto readKey(const std::string& description) -> std::uint32_t;
194 auto readRawCharset(const std::string& description) -> std::string;
195};
196
197#endif // BKCRACK_ARGUMENTS_HPP
Arguments::Error::Error
Error(const std::string &description)
Constructor.
Arguments::infoArchive
std::optional< std::string > infoArchive
Zip archive about which to display information.
Definition Arguments.hpp:129
Arguments::cipherFile
std::optional< std::string > cipherFile
File containing the ciphertext.
Definition Arguments.hpp:35
Arguments::decipheredFile
std::optional< std::string > decipheredFile
File to write the deciphered text corresponding to cipherFile.
Definition Arguments.hpp:69
Arguments::Arguments
Arguments(int argc, const char *const argv[])
Constructor parsing command line arguments.
Arguments::plainFile
std::optional< std::string > plainFile
File containing the known plaintext.
Definition Arguments.hpp:39
Arguments::recoveryStart
std::string recoveryStart
Starting point for password recovery.
Definition Arguments.hpp:120
Arguments::plainIndex
std::optional< std::size_t > plainIndex
Index of the zip entry containing plaintext.
Definition Arguments.hpp:40
Arguments::version
bool version
Tell whether version information is needed or not.
Definition Arguments.hpp:132
Arguments::decryptedArchive
std::optional< std::string > decryptedArchive
File to write an unencrypted copy of the encrypted archive.
Definition Arguments.hpp:75
Arguments::keys
std::optional< Keys > keys
Internal password representation.
Definition Arguments.hpp:66
Arguments::keepHeader
bool keepHeader
Tell whether to keep the encryption header or discard it when writing the deciphered text.
Definition Arguments.hpp:72
Arguments::mask
std::optional< std::vector< std::vector< std::uint8_t > > > mask
Mask for password recovery, alternative to bruteforce and length.
Definition Arguments.hpp:117
Arguments::password
std::optional< std::string > password
Password from which to derive the internal password representation.
Definition Arguments.hpp:63
Arguments::attackStart
int attackStart
Starting point of the attack on Z values remaining after reduction.
Definition Arguments.hpp:60
Arguments::jobs
int jobs
Number of threads to use for parallelized operations.
Definition Arguments.hpp:123
Arguments::changePassword
std::optional< ChangePassword > changePassword
Arguments needed to change an archive's password.
Definition Arguments.hpp:84
Arguments::exhaustive
bool exhaustive
Tell whether to try all candidates (keys or passwords) exhaustively or stop after the first success.
Definition Arguments.hpp:126
Arguments::ignoreCheckByte
bool ignoreCheckByte
Tell not to use the check byte derived from ciphertext entry metadata as known plaintext.
Definition Arguments.hpp:57
Arguments::changeKeys
std::optional< ChangeKeys > changeKeys
Arguments needed to change an archive's internal password representation.
Definition Arguments.hpp:96
Arguments::offset
int offset
Plaintext offset relative to ciphertext without encryption header (may be negative).
Definition Arguments.hpp:50
Arguments::plainArchive
std::optional< std::string > plainArchive
Zip archive containing plainFile.
Definition Arguments.hpp:41
Arguments::extraPlaintext
std::map< int, std::uint8_t > extraPlaintext
Definition Arguments.hpp:54
Arguments::plainFilePrefix
std::size_t plainFilePrefix
Maximum number of bytes of plaintext to read from plainFile.
Definition Arguments.hpp:47
Arguments::loadData
auto loadData() const -> Data
Load the data needed for an attack based on parsed arguments.
Arguments::bruteforce
std::optional< std::vector< std::uint8_t > > bruteforce
Characters to generate password candidates.
Definition Arguments.hpp:99
Arguments::cipherIndex
std::optional< std::size_t > cipherIndex
Index of the zip entry containing ciphertext.
Definition Arguments.hpp:36
Arguments::help
bool help
Tell whether help message is needed or not.
Definition Arguments.hpp:135
Arguments::cipherArchive
std::optional< std::string > cipherArchive
Zip archive containing cipherFile.
Definition Arguments.hpp:37
Arguments::length
std::optional< LengthInterval > length
Range of password lengths to try during password recovery.
Definition Arguments.hpp:114
BaseError::BaseError
BaseError(const std::string &type, const std::string &description)
Constructor.
Keys
Keys defining the cipher state.
Definition Keys.hpp:10
recoverPassword
auto recoverPassword(const Keys &keys, const std::vector< std::uint8_t > &charset, std::size_t minLength, std::size_t maxLength, std::string &start, int jobs, bool exhaustive, Progress &progress) -> std::vector< std::string >
Try to recover the password associated with the given keys.
Arguments::ChangeKeys
Arguments needed to change an archive's internal password representation.
Definition Arguments.hpp:91
Arguments::ChangeKeys::newKeys
Keys newKeys
Internal password representation chosen to generate the new archive.
Definition Arguments.hpp:93
Arguments::ChangeKeys::unlockedArchive
std::string unlockedArchive
File to write the new encrypted archive.
Definition Arguments.hpp:92
Arguments::ChangePassword
Arguments needed to change an archive's password.
Definition Arguments.hpp:79
Arguments::ChangePassword::unlockedArchive
std::string unlockedArchive
File to write the new encrypted archive.
Definition Arguments.hpp:80
Arguments::ChangePassword::newPassword
std::string newPassword
Password chosen to generate the new archive.
Definition Arguments.hpp:81
Arguments::LengthInterval
Range of password lengths to try during password recovery.
Definition Arguments.hpp:103
Arguments::LengthInterval::maxLength
std::size_t maxLength
Greatest password length to try (inclusive).
Definition Arguments.hpp:108
Arguments::LengthInterval::minLength
std::size_t minLength
Smallest password length to try (inclusive).
Definition Arguments.hpp:105
Arguments::LengthInterval::operator&
auto operator&(const LengthInterval &other) const -> LengthInterval
Compute the intersection between this interval and the given other interval.
Data
Structure to hold the data needed for an attack.
Definition Data.hpp:10
types.hpp
Useful types, constants and utility functions.