bkcrack 1.8.0
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 "Data.hpp"
5#include "Keys.hpp"
6#include "types.hpp"
7
8#include <limits>
9#include <map>
10#include <optional>
11#include <unordered_map>
12
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* 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
79 {
80 std::string unlockedArchive;
81 std::string newPassword;
82 };
83
84 std::optional<ChangePassword> changePassword;
85
91 {
92 std::string unlockedArchive;
94 };
95
96 std::optional<ChangeKeys> changeKeys;
97
99 std::optional<std::vector<std::uint8_t>> bruteforce;
100
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** m_current;
139 const char** 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 {
160 plainFile,
164 offset,
168 password,
169 keys,
176 length,
177 recoverPassword,
178 mask,
179 charset,
181 jobs,
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
Error(const std::string &description)
Constructor.
std::optional< std::string > infoArchive
Zip archive about which to display information.
Definition Arguments.hpp:129
std::optional< std::string > cipherFile
File containing the ciphertext.
Definition Arguments.hpp:35
Arguments(int argc, const char *argv[])
Constructor parsing command line arguments.
std::optional< std::string > decipheredFile
File to write the deciphered text corresponding to cipherFile.
Definition Arguments.hpp:69
std::optional< std::string > plainFile
File containing the known plaintext.
Definition Arguments.hpp:39
std::string recoveryStart
Starting point for password recovery.
Definition Arguments.hpp:120
std::optional< std::size_t > plainIndex
Index of the zip entry containing plaintext.
Definition Arguments.hpp:40
bool version
Tell whether version information is needed or not.
Definition Arguments.hpp:132
std::optional< std::string > decryptedArchive
File to write an unencrypted copy of the encrypted archive.
Definition Arguments.hpp:75
std::optional< Keys > keys
Internal password representation.
Definition Arguments.hpp:66
bool keepHeader
Tell whether to keep the encryption header or discard it when writing the deciphered text.
Definition Arguments.hpp:72
std::optional< std::vector< std::vector< std::uint8_t > > > mask
Mask for password recovery, alternative to bruteforce and length.
Definition Arguments.hpp:117
std::optional< std::string > password
Password from which to derive the internal password representation.
Definition Arguments.hpp:63
int attackStart
Starting point of the attack on Z values remaining after reduction.
Definition Arguments.hpp:60
int jobs
Number of threads to use for parallelized operations.
Definition Arguments.hpp:123
std::optional< ChangePassword > changePassword
Arguments needed to change an archive's password.
Definition Arguments.hpp:84
bool exhaustive
Tell whether to try all candidates (keys or passwords) exhaustively or stop after the first success.
Definition Arguments.hpp:126
bool ignoreCheckByte
Tell not to use the check byte derived from ciphertext entry metadata as known plaintext.
Definition Arguments.hpp:57
std::optional< ChangeKeys > changeKeys
Arguments needed to change an archive's internal password representation.
Definition Arguments.hpp:96
int offset
Plaintext offset relative to ciphertext without encryption header (may be negative)
Definition Arguments.hpp:50
std::optional< std::string > plainArchive
Zip archive containing plainFile.
Definition Arguments.hpp:41
std::map< int, std::uint8_t > extraPlaintext
Definition Arguments.hpp:54
std::size_t plainFilePrefix
Maximum number of bytes of plaintext to read from plainFile.
Definition Arguments.hpp:47
auto loadData() const -> Data
Load the data needed for an attack based on parsed arguments.
std::optional< std::vector< std::uint8_t > > bruteforce
Characters to generate password candidates.
Definition Arguments.hpp:99
std::optional< std::size_t > cipherIndex
Index of the zip entry containing ciphertext.
Definition Arguments.hpp:36
bool help
Tell whether help message is needed or not.
Definition Arguments.hpp:135
std::optional< std::string > cipherArchive
Zip archive containing cipherFile.
Definition Arguments.hpp:37
std::optional< LengthInterval > length
Range of password lengths to try during password recovery.
Definition Arguments.hpp:114
BaseError(const std::string &type, const std::string &description)
Constructor.
Keys defining the cipher state.
Definition Keys.hpp:10
Arguments needed to change an archive's internal password representation.
Definition Arguments.hpp:91
Keys newKeys
Internal password representation chosen to generate the new archive.
Definition Arguments.hpp:93
std::string unlockedArchive
File to write the new encrypted archive.
Definition Arguments.hpp:92
Arguments needed to change an archive's password.
Definition Arguments.hpp:79
std::string unlockedArchive
File to write the new encrypted archive.
Definition Arguments.hpp:80
std::string newPassword
Password chosen to generate the new archive.
Definition Arguments.hpp:81
Range of password lengths to try during password recovery.
Definition Arguments.hpp:103
std::size_t maxLength
Greatest password length to try (inclusive)
Definition Arguments.hpp:108
std::size_t minLength
Smallest password length to try (inclusive)
Definition Arguments.hpp:105
auto operator&(const LengthInterval &other) const -> LengthInterval
Compute the intersection between this interval and the given other interval.
Structure to hold the data needed for an attack.
Definition Data.hpp:10
Useful types, constants and utility functions.