-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
226 lines (178 loc) · 6.79 KB
/
main.cpp
File metadata and controls
226 lines (178 loc) · 6.79 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include "./pelib/PeLib.h"
#include "./pelib/PeLibAux.h"
#include "./live/crypt_range.h"
#include "./live/pack.h"
#include "./live/iat_import.h"
#include "windows.h" //not portable innit :p
using namespace std;
void pack_pe(char * name);
template<typename T>
std::string toString(T x, char f = '0')
{
std::stringstream ss;
ss << std::setw(sizeof(T)*2) << std::setfill(f) << std::hex << std::uppercase << x;
return ss.str();
}
template<>
std::string toString<PeLib::byte>(PeLib::byte x, char f)
{
std::stringstream ss;
ss << std::setw(2) << std::setfill(f) << std::hex << std::uppercase << (int)x;
return ss.str();
}
std::string formatOutput(const std::string& text, const std::string& val, const std::string& pad = "", unsigned int maxsize = 70)
{
std::stringstream ss;
ss << pad << text << std::setw(maxsize - text.length() - val.length() - pad.length()) << std::setfill(' ') << "";
ss << val;
return ss.str();
}
std::string centerOutput(const std::string& text, unsigned int maxsize = 70)
{
std::stringstream ss;
unsigned int left = (maxsize - text.length()) / 2;
ss << std::setw(left) << std::setfill(' ') << "";
ss << text;
return ss.str();
}
void dump(const std::string& d)
{
std::cout << d << std::endl;
}
class PeRebuilderVisitor : public PeLib::PeFileVisitor
{
public:
virtual void callback(PeLib::PeFile32 &file) {
std::string newFileName = file.getFileName();
newFileName.replace(newFileName.size()-4,4,"_new.exe");
// Delete old file...
remove(newFileName.c_str());
file.peHeader().readSectionsData(file.getFileName());
dump("Adding section yopu");
//add section
int ret;
if((ret=file.peHeader().addSection("yopu", 0x100))!=NO_ERROR)
{
int numberofnewdir=0;
int mtintsize = file.peHeader().size() - file.peHeader().getNumberOfSections() * PeLib::PELIB_IMAGE_SECTION_HEADER::size();
switch(ret)
{
case PeLib::ERROR_NOT_ENOUGH_SPACE:
std::cout << "Pe Header ["<< mtintsize <<"] too small to add another section!" << std::endl;
std::cout << "Trying to remove bindimportdirectory! (most win stuff...)" << std::endl;
while(ret == PeLib::ERROR_NOT_ENOUGH_SPACE && numberofnewdir < 10)
{
//file.peHeader().addDataDirectory();
//file.peHeader().setPointerToRawData(0,file.peHeader().getPointerToRawData(0)+0x100);
//file.peHeader().makeValid(file.mzHeader().getAddressOfPeHeader());
file.peHeader().setIddBoundImportRva(0);
ret=file.peHeader().addSection("yopu", 0x100);
numberofnewdir++;
}
if(ret != NO_ERROR)
{
std::cout << "Bah didn't worked..." << std::endl;
return;
}
break;
default:
std::cout << "Couille dans le potage pour addsection -> "<< ret << std::endl;
return;
}
}
// Recalculate stuffs...
file.peHeader().makeValid(file.mzHeader().getAddressOfPeHeader());
unsigned geo_section_number =file.peHeader().getNumberOfSections()-1;
// fill section with asm
std::vector<char> geo_section(0);
//
// Grab IAT
//
live::IatImport<32> lImport;
lImport.iat_import(
geo_section,
geo_section_number,
file);
//
// Pack first section
//
live::Pack<32> cpack;
int new_eip=cpack.pack(
geo_section,
geo_section_number,
0, // section to compress!
file);
//
// Pack section 2
//
cpack.pack(
geo_section,
geo_section_number,
1, // section to compress!
file);
/*live::CryptRange<32> crange;
crange.crypt(
geo_section, //vector
geo_section_number, //section N°
new_eip, //va_start
geo_section.size() -(new_eip - file.peHeader().getVirtualAddress(geo_section_number) ) , //size
file //PeFile
);*/
// Add section data...Change to addSectionData maybe!
file.peHeader().sectionsData().push_back(geo_section);
file.peHeader().makeValid(file.mzHeader().getAddressOfPeHeader());
dump("Writing to disk...");
file.write(newFileName.c_str());
}
virtual void callback(PeLib::PeFile64 &file) {
}
};
int main(int argc, char *argv[])
{
if (argc <= 1)
{
std::cout << "Usage: filedump <directory>" << std::endl;
return 1;
}
//
// A recoder pour devenir portable...
//
HANDLE hFind;
WIN32_FIND_DATA FindData;
SetCurrentDirectory(argv[1]);
hFind=FindFirstFile("*.exe",&FindData);
do
{
if(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ))
{
//Quand on trouve un fichier...
if(strstr(FindData.cFileName,"_new.exe")==0)
pack_pe(FindData.cFileName);
}
} while(FindNextFile(hFind,&FindData));
//pack_pe(argv[1]);
return 0;
}
void pack_pe(char * name)
{
std::string filename = name;
PeLib::PeFile* pef = PeLib::openPeFile(filename);
if (!pef)
{
std::cout << "Invalid PE File" << std::endl;
return ;
}
pef->readMzHeader();
pef->readPeHeader();
dump(centerOutput("----------------------------------------------"));
dump(centerOutput("PE Loaded"));
dump(centerOutput("----------------------------------------------"));
PeRebuilderVisitor v2;
pef->visit(v2);
delete pef;
}