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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
|
/*************************************************************************/
/* Copyright (c) 2015 dx, http://kaimi.ru */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files (the "Software"), to deal in the Software without */
/* restriction, including without limitation the rights to use, */
/* copy, modify, merge, publish, distribute, sublicense, and/or */
/* sell copies of the Software, and to permit persons to whom the */
/* Software is furnished to do so, subject to the following conditions: */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
#include <string.h>
#include "pe_tls.h"
#include "pe_properties_generic.h"
namespace pe_bliss
{
using namespace pe_win;
//TLS
//Default constructor
tls_info::tls_info()
:start_rva_(0), end_rva_(0), index_rva_(0), callbacks_rva_(0),
size_of_zero_fill_(0), characteristics_(0)
{}
//Returns start RVA of TLS raw data
uint32_t tls_info::get_raw_data_start_rva() const
{
return start_rva_;
}
//Returns end RVA of TLS raw data
uint32_t tls_info::get_raw_data_end_rva() const
{
return end_rva_;
}
//Returns TLS index RVA
uint32_t tls_info::get_index_rva() const
{
return index_rva_;
}
//Returns TLS callbacks RVA
uint32_t tls_info::get_callbacks_rva() const
{
return callbacks_rva_;
}
//Returns size of zero fill
uint32_t tls_info::get_size_of_zero_fill() const
{
return size_of_zero_fill_;
}
//Returns characteristics
uint32_t tls_info::get_characteristics() const
{
return characteristics_;
}
//Returns raw TLS data
const std::string& tls_info::get_raw_data() const
{
return raw_data_;
}
//Returns TLS callbacks addresses
const tls_info::tls_callback_list& tls_info::get_tls_callbacks() const
{
return callbacks_;
}
//Returns TLS callbacks addresses
tls_info::tls_callback_list& tls_info::get_tls_callbacks()
{
return callbacks_;
}
//Adds TLS callback
void tls_info::add_tls_callback(uint32_t rva)
{
callbacks_.push_back(rva);
}
//Clears TLS callbacks list
void tls_info::clear_tls_callbacks()
{
callbacks_.clear();
}
//Recalculates end address of raw TLS data
void tls_info::recalc_raw_data_end_rva()
{
end_rva_ = static_cast<uint32_t>(start_rva_ + raw_data_.length());
}
//Sets start RVA of TLS raw data
void tls_info::set_raw_data_start_rva(uint32_t rva)
{
start_rva_ = rva;
}
//Sets end RVA of TLS raw data
void tls_info::set_raw_data_end_rva(uint32_t rva)
{
end_rva_ = rva;
}
//Sets TLS index RVA
void tls_info::set_index_rva(uint32_t rva)
{
index_rva_ = rva;
}
//Sets TLS callbacks RVA
void tls_info::set_callbacks_rva(uint32_t rva)
{
callbacks_rva_ = rva;
}
//Sets size of zero fill
void tls_info::set_size_of_zero_fill(uint32_t size)
{
size_of_zero_fill_ = size;
}
//Sets characteristics
void tls_info::set_characteristics(uint32_t characteristics)
{
characteristics_ = characteristics;
}
//Sets raw TLS data
void tls_info::set_raw_data(const std::string& data)
{
raw_data_ = data;
}
//If image does not have TLS, throws an exception
const tls_info get_tls_info(const pe_base& pe)
{
return pe.get_pe_type() == pe_type_32
? get_tls_info_base<pe_types_class_32>(pe)
: get_tls_info_base<pe_types_class_64>(pe);
}
//TLS Rebuilder
const image_directory rebuild_tls(pe_base& pe, const tls_info& info, section& tls_section, uint32_t offset_from_section_start, bool write_tls_callbacks, bool write_tls_data, tls_data_expand_type expand, bool save_to_pe_header, bool auto_strip_last_section)
{
return pe.get_pe_type() == pe_type_32
? rebuild_tls_base<pe_types_class_32>(pe, info, tls_section, offset_from_section_start, write_tls_callbacks, write_tls_data, expand, save_to_pe_header, auto_strip_last_section)
: rebuild_tls_base<pe_types_class_64>(pe, info, tls_section, offset_from_section_start, write_tls_callbacks, write_tls_data, expand, save_to_pe_header, auto_strip_last_section);
}
//Get TLS info
//If image does not have TLS, throws an exception
template<typename PEClassType>
const tls_info get_tls_info_base(const pe_base& pe)
{
tls_info ret;
//If there's no TLS directory, throw an exception
if(!pe.has_tls())
throw pe_exception("Image does not have TLS directory", pe_exception::directory_does_not_exist);
//Get TLS directory data
typename PEClassType::TLSStruct tls_directory_data = pe.section_data_from_rva<typename PEClassType::TLSStruct>(pe.get_directory_rva(image_directory_entry_tls), section_data_virtual, true);
//Check data addresses
if(tls_directory_data.EndAddressOfRawData == tls_directory_data.StartAddressOfRawData)
{
try
{
pe.va_to_rva(static_cast<typename PEClassType::BaseSize>(tls_directory_data.EndAddressOfRawData));
}
catch(const pe_exception&)
{
//Fix addressess on incorrect conversion
tls_directory_data.EndAddressOfRawData = tls_directory_data.StartAddressOfRawData = 0;
}
}
if(tls_directory_data.StartAddressOfRawData &&
pe.section_data_length_from_va(static_cast<typename PEClassType::BaseSize>(tls_directory_data.StartAddressOfRawData),
static_cast<typename PEClassType::BaseSize>(tls_directory_data.StartAddressOfRawData), section_data_virtual, true)
< (tls_directory_data.EndAddressOfRawData - tls_directory_data.StartAddressOfRawData))
throw pe_exception("Incorrect TLS directory", pe_exception::incorrect_tls_directory);
//Fill TLS info
//VAs are not checked
ret.set_raw_data_start_rva(tls_directory_data.StartAddressOfRawData ? pe.va_to_rva(static_cast<typename PEClassType::BaseSize>(tls_directory_data.StartAddressOfRawData)) : 0);
ret.set_raw_data_end_rva(tls_directory_data.EndAddressOfRawData ? pe.va_to_rva(static_cast<typename PEClassType::BaseSize>(tls_directory_data.EndAddressOfRawData)) : 0);
ret.set_index_rva(tls_directory_data.AddressOfIndex ? pe.va_to_rva(static_cast<typename PEClassType::BaseSize>(tls_directory_data.AddressOfIndex)) : 0);
ret.set_callbacks_rva(tls_directory_data.AddressOfCallBacks ? pe.va_to_rva(static_cast<typename PEClassType::BaseSize>(tls_directory_data.AddressOfCallBacks)) : 0);
ret.set_size_of_zero_fill(tls_directory_data.SizeOfZeroFill);
ret.set_characteristics(tls_directory_data.Characteristics);
if(tls_directory_data.StartAddressOfRawData && tls_directory_data.StartAddressOfRawData != tls_directory_data.EndAddressOfRawData)
{
//Read and save TLS RAW data
ret.set_raw_data(std::string(
pe.section_data_from_va(static_cast<typename PEClassType::BaseSize>(tls_directory_data.StartAddressOfRawData), section_data_virtual, true),
static_cast<uint32_t>(tls_directory_data.EndAddressOfRawData - tls_directory_data.StartAddressOfRawData)));
}
//If file has TLS callbacks
if(ret.get_callbacks_rva())
{
//Read callbacks VAs
uint32_t current_tls_callback = 0;
while(true)
{
//Read TLS callback VA
typename PEClassType::BaseSize va = pe.section_data_from_va<typename PEClassType::BaseSize>(static_cast<typename PEClassType::BaseSize>(tls_directory_data.AddressOfCallBacks + current_tls_callback), section_data_virtual, true);
if(va == 0)
break;
//Save it
ret.add_tls_callback(pe.va_to_rva(va, false));
//Move to next callback VA
current_tls_callback += sizeof(va);
}
}
return ret;
}
//Rebuilder of TLS structures
//If write_tls_callbacks = true, TLS callbacks VAs will be written to their place
//If write_tls_data = true, TLS data will be written to its place
//If you have chosen to rewrite raw data, only (EndAddressOfRawData - StartAddressOfRawData) bytes will be written, not the full length of string
//representing raw data content
//auto_strip_last_section - if true and TLS are placed in the last section, it will be automatically stripped
//Note/TODO: TLS Callbacks array is not DWORD-aligned (seems to work on WinXP - Win7)
template<typename PEClassType>
const image_directory rebuild_tls_base(pe_base& pe, const tls_info& info, section& tls_section, uint32_t offset_from_section_start, bool write_tls_callbacks, bool write_tls_data, tls_data_expand_type expand, bool save_to_pe_header, bool auto_strip_last_section)
{
//Check that tls_section is attached to this PE image
if(!pe.section_attached(tls_section))
throw pe_exception("TLS section must be attached to PE file", pe_exception::section_is_not_attached);
uint32_t tls_data_pos = pe_utils::align_up(offset_from_section_start, sizeof(typename PEClassType::BaseSize));
uint32_t needed_size = sizeof(typename PEClassType::TLSStruct); //Calculate needed size for TLS table
//Check if tls_section is last one. If it's not, check if there's enough place for TLS data
if(&tls_section != &*(pe.get_image_sections().end() - 1) &&
(tls_section.empty() || pe_utils::align_up(tls_section.get_size_of_raw_data(), pe.get_file_alignment()) < needed_size + tls_data_pos))
throw pe_exception("Insufficient space for TLS directory", pe_exception::insufficient_space);
//Check raw data positions
if(info.get_raw_data_end_rva() < info.get_raw_data_start_rva() || info.get_index_rva() == 0)
throw pe_exception("Incorrect TLS directory", pe_exception::incorrect_tls_directory);
std::string& raw_data = tls_section.get_raw_data();
//This will be done only if tls_section is the last section of image or for section with unaligned raw length of data
if(raw_data.length() < needed_size + tls_data_pos)
raw_data.resize(needed_size + tls_data_pos); //Expand section raw data
//Create and fill TLS structure
typename PEClassType::TLSStruct tls_struct = {0};
typename PEClassType::BaseSize va;
if(info.get_raw_data_start_rva())
{
pe.rva_to_va(info.get_raw_data_start_rva(), va);
tls_struct.StartAddressOfRawData = va;
tls_struct.SizeOfZeroFill = info.get_size_of_zero_fill();
}
if(info.get_raw_data_end_rva())
{
pe.rva_to_va(info.get_raw_data_end_rva(), va);
tls_struct.EndAddressOfRawData = va;
}
pe.rva_to_va(info.get_index_rva(), va);
tls_struct.AddressOfIndex = va;
if(info.get_callbacks_rva())
{
pe.rva_to_va(info.get_callbacks_rva(), va);
tls_struct.AddressOfCallBacks = va;
}
tls_struct.Characteristics = info.get_characteristics();
//Save TLS structure
memcpy(&raw_data[tls_data_pos], &tls_struct, sizeof(tls_struct));
//If we are asked to rewrite TLS raw data
if(write_tls_data && info.get_raw_data_start_rva() && info.get_raw_data_start_rva() != info.get_raw_data_end_rva())
{
try
{
//Check if we're going to write TLS raw data to an existing section (not to PE headers)
section& raw_data_section = pe.section_from_rva(info.get_raw_data_start_rva());
pe.expand_section(raw_data_section, info.get_raw_data_start_rva(), info.get_raw_data_end_rva() - info.get_raw_data_start_rva(), expand == tls_data_expand_raw ? pe_base::expand_section_raw : pe_base::expand_section_virtual);
}
catch(const pe_exception&)
{
//If no section is presented by StartAddressOfRawData, just go to next step
}
unsigned long write_raw_data_size = info.get_raw_data_end_rva() - info.get_raw_data_start_rva();
unsigned long available_raw_length = 0;
//Check if there's enough RAW space to write raw TLS data...
if((available_raw_length = pe.section_data_length_from_rva(info.get_raw_data_start_rva(), info.get_raw_data_start_rva(), section_data_raw, true))
< info.get_raw_data_end_rva() - info.get_raw_data_start_rva())
{
//Check if there's enough virtual space for it...
if(pe.section_data_length_from_rva(info.get_raw_data_start_rva(), info.get_raw_data_start_rva(), section_data_virtual, true)
< info.get_raw_data_end_rva() - info.get_raw_data_start_rva())
throw pe_exception("Insufficient space for TLS raw data", pe_exception::insufficient_space);
else
write_raw_data_size = available_raw_length; //We'll write just a part of full raw data
}
//Write raw TLS data, if any
if(write_raw_data_size != 0)
memcpy(pe.section_data_from_rva(info.get_raw_data_start_rva(), true), info.get_raw_data().data(), write_raw_data_size);
}
//If we are asked to rewrite TLS callbacks addresses
if(write_tls_callbacks && info.get_callbacks_rva())
{
unsigned long needed_callback_size = static_cast<unsigned long>((info.get_tls_callbacks().size() + 1 /* last null element */) * sizeof(typename PEClassType::BaseSize));
try
{
//Check if we're going to write TLS callbacks VAs to an existing section (not to PE headers)
section& raw_data_section = pe.section_from_rva(info.get_callbacks_rva());
pe.expand_section(raw_data_section, info.get_callbacks_rva(), needed_callback_size, pe_base::expand_section_raw);
}
catch(const pe_exception&)
{
//If no section is presented by RVA of callbacks, just go to next step
}
//Check if there's enough space to write callbacks TLS data...
if(pe.section_data_length_from_rva(info.get_callbacks_rva(), info.get_callbacks_rva(), section_data_raw, true)
< needed_callback_size - sizeof(typename PEClassType::BaseSize) /* last zero element can be virtual only */)
throw pe_exception("Insufficient space for TLS callbacks data", pe_exception::insufficient_space);
if(pe.section_data_length_from_rva(info.get_callbacks_rva(), info.get_callbacks_rva(), section_data_virtual, true)
< needed_callback_size /* check here full virtual data length available */)
throw pe_exception("Insufficient space for TLS callbacks data", pe_exception::insufficient_space);
std::vector<typename PEClassType::BaseSize> callbacks_virtual_addresses;
callbacks_virtual_addresses.reserve(info.get_tls_callbacks().size() + 1 /* last null element */);
//Convert TLS RVAs to VAs
for(tls_info::tls_callback_list::const_iterator it = info.get_tls_callbacks().begin(); it != info.get_tls_callbacks().end(); ++it)
{
typename PEClassType::BaseSize cb_va = 0;
pe.rva_to_va(*it, cb_va);
callbacks_virtual_addresses.push_back(cb_va);
}
//Ending null element
callbacks_virtual_addresses.push_back(0);
//Write callbacks TLS data
memcpy(pe.section_data_from_rva(info.get_callbacks_rva(), true), &callbacks_virtual_addresses[0], needed_callback_size);
}
//Adjust section raw and virtual sizes
pe.recalculate_section_sizes(tls_section, auto_strip_last_section);
image_directory ret(pe.rva_from_section_offset(tls_section, tls_data_pos), needed_size);
//If auto-rewrite of PE headers is required
if(save_to_pe_header)
{
pe.set_directory_rva(image_directory_entry_tls, ret.get_rva());
pe.set_directory_size(image_directory_entry_tls, ret.get_size());
}
return ret;
}
}
|