bkcrack 1.7.1
Crack legacy zip encryption with Biham and Kocher's known plaintext attack.
Zip.hpp
1#ifndef BKCRACK_ZIP_HPP
2#define BKCRACK_ZIP_HPP
3
4#include "Keys.hpp"
5#include "Progress.hpp"
6
7#include <fstream>
8#include <optional>
9
20class Zip
21{
22public:
24 class Error : public BaseError
25 {
26 public:
28 explicit Error(const std::string& description);
29 };
30
38
40 enum class Compression
41 {
42 Store = 0,
43 Shrink = 1,
44 Implode = 6,
45 Deflate = 8,
46 Deflate64 = 9,
47 BZip2 = 12,
48 LZMA = 14,
49 Zstandard = 93,
50 MP3 = 94,
51 XZ = 95,
52 JPEG = 96,
53 WavPack = 97,
54 PPMd = 98,
55 };
56
58 struct Entry
59 {
60 std::string name;
63 std::uint32_t crc32;
64 std::uint64_t offset;
65 std::uint64_t packedSize;
66 std::uint64_t uncompressedSize;
67 std::uint8_t checkByte;
68 };
69
72 {
73 public:
76 using difference_type = std::ptrdiff_t;
77 using value_type = const Entry;
78 using pointer = const Entry*;
79 using reference = const Entry&;
80 using iterator_category = std::input_iterator_tag;
82
84 constexpr Iterator() noexcept = default;
85
88 explicit Iterator(const Zip& archive);
89
92 auto operator*() const -> const Entry&
93 {
94 return *m_entry;
95 }
96
99 auto operator->() const -> const Entry*
100 {
101 return &(*m_entry);
102 }
103
107
109 auto operator++(int) -> Iterator;
110
112 auto operator==(const Zip::Iterator& other) const -> bool
113 {
114 return (m_is == nullptr) == (other.m_is == nullptr);
115 }
116
118 auto operator!=(const Zip::Iterator& other) const -> bool
119 {
120 return !(*this == other);
121 }
122
123 private:
124 std::istream* m_is = nullptr;
125 std::optional<Entry> m_entry; // optional type allows the end-of-stream iterator to be empty
126 };
127
130 explicit Zip(std::istream& stream);
131
135 explicit Zip(const std::string& filename);
136
138 auto begin() const -> Iterator
139 {
140 return Iterator{*this};
141 }
142
144 auto end() const -> Iterator
145 {
146 return Iterator{};
147 }
148
151 auto operator[](const std::string& name) const -> Entry;
152
155 auto operator[](std::size_t index) const -> Entry;
156
159 static void checkEncryption(const Entry& entry, Encryption expected);
160
163 auto seek(const Entry& entry) const -> std::istream&;
164
167 auto load(const Entry& entry, std::size_t count = std::numeric_limits<std::size_t>::max()) const
168 -> std::vector<std::uint8_t>;
169
172 void changeKeys(std::ostream& os, const Keys& oldKeys, const Keys& newKeys, Progress& progress) const;
173
176 void decrypt(std::ostream& os, const Keys& keys, Progress& progress) const;
177
178private:
179 std::optional<std::ifstream> m_file; // optionally own the stream
180 std::istream& m_is;
181 const std::uint64_t m_centralDirectoryOffset;
182};
183
186void decipher(std::istream& is, std::size_t size, std::size_t discard, std::ostream& os, Keys keys);
187
188#endif // BKCRACK_ZIP_HPP
BaseError(const std::string &type, const std::string &description)
Constructor.
Keys defining the cipher state.
Definition Keys.hpp:10
Structure to report the progress of a long operation or to cancel it.
Definition Progress.hpp:10
Error(const std::string &description)
Constructor.
Single-pass input iterator that reads successive Entry objects.
Definition Zip.hpp:72
auto operator->() const -> const Entry *
Access a member of the current entry.
Definition Zip.hpp:99
std::ptrdiff_t difference_type
Required types for iterators.
Definition Zip.hpp:76
const Entry value_type
Required types for iterators.
Definition Zip.hpp:77
std::input_iterator_tag iterator_category
Required types for iterators.
Definition Zip.hpp:80
auto operator++() -> Iterator &
Read the next central directory record if any or assign end-of-stream iterator.
const Entry & reference
Required types for iterators.
Definition Zip.hpp:79
constexpr Iterator() noexcept=default
Construct end-of-stream iterator.
auto operator++(int) -> Iterator
Read the next central directory record if any or assign end-of-stream iterator.
auto operator==(const Zip::Iterator &other) const -> bool
Test if iterators are equivalent, i.e. both are end-of-stream or both are valid.
Definition Zip.hpp:112
auto operator!=(const Zip::Iterator &other) const -> bool
Test if iterators are not equivalent.
Definition Zip.hpp:118
const Entry * pointer
Required types for iterators.
Definition Zip.hpp:78
auto begin() const -> Iterator
Get an iterator pointing to the first entry.
Definition Zip.hpp:138
auto operator[](const std::string &name) const -> Entry
Get the first entry having the given name.
Compression
Compression algorithm.
Definition Zip.hpp:41
static void checkEncryption(const Entry &entry, Encryption expected)
Check that the given entry uses the expected encryption algorithm.
Zip(const std::string &filename)
Open a zip archive from a file.
auto end() const -> Iterator
Get an end-of-stream iterator.
Definition Zip.hpp:144
auto load(const Entry &entry, std::size_t count=std::numeric_limits< std::size_t >::max()) const -> std::vector< std::uint8_t >
Load at most count bytes of the given entry's raw data.
void changeKeys(std::ostream &os, const Keys &oldKeys, const Keys &newKeys, Progress &progress) const
Copy the zip file into os changing the encrypted data using the given keys.
auto operator[](std::size_t index) const -> Entry
Get the entry at the given index.
auto seek(const Entry &entry) const -> std::istream &
Set the underlying stream's input position indicator at the beginning the given entry's raw data.
Zip(std::istream &stream)
Open a zip archive from an already opened input stream.
void decrypt(std::ostream &os, const Keys &keys, Progress &progress) const
Copy the zip file into os removing encryption using the given keys.
Encryption
Encryption algorithm.
Definition Zip.hpp:33
@ Traditional
Traditional PKWARE encryption (ZipCrypto), vulnerable to known plaintext attack.
Definition Zip.hpp:35
@ None
No encryption.
Definition Zip.hpp:34
@ Unsupported
Other encryption (DES, RC2, 3DES, AES, Blowfish, Twofish, RC4)
Definition Zip.hpp:36
Information about a zip entry.
Definition Zip.hpp:59
Encryption encryption
Encryption method.
Definition Zip.hpp:61
std::uint64_t offset
Offset of local file header.
Definition Zip.hpp:64
std::uint64_t packedSize
Packed data size.
Definition Zip.hpp:65
std::uint8_t checkByte
Last byte of the encryption header after decryption.
Definition Zip.hpp:67
std::uint64_t uncompressedSize
Uncompressed data size.
Definition Zip.hpp:66
std::string name
File name.
Definition Zip.hpp:60
Compression compression
Compression method.
Definition Zip.hpp:62
std::uint32_t crc32
CRC-32 checksum.
Definition Zip.hpp:63