changeofpace/Self-Remapping-Code

Code is not working out of the box

tripleslash opened this issue · 4 comments

I've just compiled the sample under VS 2017 x64 and I get the following error:

=================================================================
Section       Base Address       Size    Rva
-----------------------------------------------------------------
.text            7FF75AA91000    DA91    1000
.rdata           7FF75AA9F000    FD40    F000
.data            7FF75AAAF000    6D8     1F000
=================================================================
Error: .rdata or .data are not aligned to system allocation granularity.

What would I need to change to make it work under VS 2017 Compiler?

I've manually added enough padding to the text section and now it works. But is there a way to dynamically fill these sections to make it compiler independent?

Hey, thanks for the heads up. I guess the Visual Studio 2017 compiler was able to optimize the filler code enough to break the alignments.

I rewrote the project to generalize the technique in 886c6a6.

You can force the compiler to map each pe section at an address aligned to the system allocation granularity using the following linker option:

#pragma comment(linker, "/ALIGN:0x10000")

You can probably modify the IMAGE_OPTIONAL_HEADER.SectionAlignment field in the pe header of a binary for the same effect.

You can also use the /MERGE linker option to combine multiple sections.

Amazing work, thank you. I believe with the changes you've made you could turn optimizations in the project settings back on, no?

Yeah, the remapping should work with any of the code optimization options. If you (or anyone else) experience issues using this technique with code optimizations enabled then create a git issue, and I will check it out.

I have the setting disabled now so that it is easier to analyze the binary in IDA. I need to remake the vcxproj file for Visual Studio 2017 so that I have clean debug and release build configuration settings.