Re-write segment of a compiled C program

D.Rek

I've a C program structured in this way:

#include <Windows.h>
#include <stdio.h>
#include <stdint.h>

#pragma section(".code",execute, read, write)
#pragma comment(linker,"/SECTION:.code,ERW")
#pragma code_seg(".code")

//Code to decrypt

#pragma section(".stub", execute, read, write)
#pragma code_seg(".stub")

void decryptor(){
    //Retrieve virtual address of the pointer to the .code section
    //Retrieve the virtual size of the pointer to the .code section
    for(int i = 0; i<size; i++){
         //HERE THE PROGRAM STOPS
         ptrCode[0] = //Reverse function of the encryptor
    }
}

int main(){
    decryptor();
    mainFunctionDecrypted();
    return 0;
}

Basically i've an encryptor which first encrypt the .code segment in the exe of this program after compilation. Then when i execute the modified exe i want to be able to first decrypt it and then execute the decrypted part. However it seems like i cannot write to the .code segment loaded in memory (I think because it's a part memory dedicated to code to be executed).

Is there any way to write to executable memory?

Is there any workaroud you would do?

D.Rek

I need to modify the code in the following way. Moreover there are important compiler option to set in visual studio, for example to disable the Data Execution Prevention.

Compiler option used: /permissive- /GS /TC /GL /analyze- /W3 /Gy /Zc:wchar_t /Gm- /O2 /sdl /Zc:inline /fp:precise /Zp1 /D "_MBCS" /errorReport:prompt /WX- /Zc:forScope /GR- /Gd /Oy- /Oi /MD /FC /nologo /diagnostics:classic

Linker option used: /MANIFEST /LTCG:incremental /NXCOMPAT:NO /DYNAMICBASE:NO "kernel32.lib" "user32.lib" "gdi32.lib" "winspool.lib" "comdlg32.lib" "advapi32.lib" "shell32.lib" "ole32.lib" "oleaut32.lib" "uuid.lib" "odbc32.lib" "odbccp32.lib" /FIXED /MACHINE:X86 /OPT:REF /SAFESEH /INCREMENTAL:NO /SUBSYSTEM:CONSOLE /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /MAP /OPT:ICF /ERRORREPORT:PROMPT /NOLOGO /TLBID:1

#pragma section(".code", execute, read)
#pragma section(".codedata", read, write)
#pragma comment(linker,"/SECTION:.code,ERW")
#pragma comment(linker,"/SECTION:.codedata,ERW")
#pragma comment(linker, "/MERGE:.codedata=.code")

//All the following will go in code
#pragma code_seg(".code")
#pragma data_seg(".codedata")
#pragma const_seg(".codedata")

//CODE TO DECRYPT

// .stub SECTION
#pragma section(".stub", execute, read)
#pragma section(".stubdata", read, write)
#pragma comment(linker,"/SECTION:.stub,ERW")
#pragma comment(linker,"/SECTION:.stubdata,ERW")
#pragma comment(linker, "/MERGE:.stubdata=.stub")

//All the following will go in .stub segment
#pragma code_seg(".stub")
#pragma data_seg(".stubdata")
#pragma const_seg(".stubdata")

/*This function needs to be changed to whatever correspond to the decryption function of the encryotion function used by the encryptor*/
void decryptCodeSection(){

     //Retrieve virtual address of the pointer to the .code section
     //Retrieve the virtual size of the pointer to the .code section
     for(int i = 0; i<size; i++){
          //HERE THE PROGRAM STOPS
          ptrCode[0] = //Reverse function of the encryptor
      }

void main(int argc, char* argv[]){
    decryptor();
    mainFunctionDecrypted();
}

Doing this way i was able to first decrypt the segment and then execute the function.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

if 0 is compiled in c program

From Dev

C++ program can not be compiled

From Dev

C program new segment declaration

From Dev

Taylor Series Calculating Program in C compiled with GCC

From Dev

Finding dependencies for a compiled C++ program

From Dev

C program trying to modify a location in text segment

From Dev

Why is the program not running the main function of a flex and bison compiled C program

From Dev

Write to C:\Program Files from Java program

From Dev

Write to C:\Program Files from Java program

From Dev

Linking C compiled static library to C++ Program

From Dev

Is there a difference in source code for release and debug compiled program? [C/C++]

From Dev

rename() not working in a C program compiled using Dev C++

From Dev

how to write this program for C++

From Dev

Intel Compilers: linking a fortran compiled library to main program of C

From Dev

Why GCC compiled C program needs .eh_frame section?

From Dev

Can C macros be expanded in gdb when the program was compiled using clang?

From Dev

How can a single compiled C program run on multiple architectures?

From Dev

Cannot get stderr of the execution of a compiled C program with the system command in perl

From Dev

Play a music file in C++ program being compiled by MinGW

From Dev

Can C macros be expanded in gdb when the program was compiled using clang?

From Dev

exiting a shell script from a c program compiled from the same script

From Dev

Intel Compilers: linking a fortran compiled library to main program of C

From Dev

Compiled c++ output file displays random character at end of program?

From Dev

Running a C++ compiled program in the background and sending input whenever needed

From Dev

C program compiled in cygwin with make: "cannot execute binary file"

From Dev

Error running C program compiled with Geany IDE Error Code: 127

From Dev

How do I change the final location of a compiled c program in the terminal

From Dev

Run a compiled C program on a computer where the headers don't exist

From Dev

How to add the ncurses.h to a C++ program compiled on gcc?

Related Related

  1. 1

    if 0 is compiled in c program

  2. 2

    C++ program can not be compiled

  3. 3

    C program new segment declaration

  4. 4

    Taylor Series Calculating Program in C compiled with GCC

  5. 5

    Finding dependencies for a compiled C++ program

  6. 6

    C program trying to modify a location in text segment

  7. 7

    Why is the program not running the main function of a flex and bison compiled C program

  8. 8

    Write to C:\Program Files from Java program

  9. 9

    Write to C:\Program Files from Java program

  10. 10

    Linking C compiled static library to C++ Program

  11. 11

    Is there a difference in source code for release and debug compiled program? [C/C++]

  12. 12

    rename() not working in a C program compiled using Dev C++

  13. 13

    how to write this program for C++

  14. 14

    Intel Compilers: linking a fortran compiled library to main program of C

  15. 15

    Why GCC compiled C program needs .eh_frame section?

  16. 16

    Can C macros be expanded in gdb when the program was compiled using clang?

  17. 17

    How can a single compiled C program run on multiple architectures?

  18. 18

    Cannot get stderr of the execution of a compiled C program with the system command in perl

  19. 19

    Play a music file in C++ program being compiled by MinGW

  20. 20

    Can C macros be expanded in gdb when the program was compiled using clang?

  21. 21

    exiting a shell script from a c program compiled from the same script

  22. 22

    Intel Compilers: linking a fortran compiled library to main program of C

  23. 23

    Compiled c++ output file displays random character at end of program?

  24. 24

    Running a C++ compiled program in the background and sending input whenever needed

  25. 25

    C program compiled in cygwin with make: "cannot execute binary file"

  26. 26

    Error running C program compiled with Geany IDE Error Code: 127

  27. 27

    How do I change the final location of a compiled c program in the terminal

  28. 28

    Run a compiled C program on a computer where the headers don't exist

  29. 29

    How to add the ncurses.h to a C++ program compiled on gcc?

HotTag

Archive