Created
May 5, 2017 07:30
-
-
Save RedToor/0d3e068f5ba1ab8fe9c67464bd63b1c6 to your computer and use it in GitHub Desktop.
JMP (SWAP Functions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Trampolin de FuncionA a FuncionB | |
1) Establecer permisos de escritura en la memoria. | |
2) Obtener direccion de la FuncionB | |
3) Ecribir en memoria OPCODE(0xE9) = JMP + direccion de FuncionB | |
*/ | |
#include <windows.h> | |
#include <cstdio> | |
const unsigned char OP_JMP = 0xE9; | |
const SIZE_T size_patch = 5; | |
typedef void (*Proceso)(); | |
void FuncionA() | |
{ | |
printf("A\n"); | |
} | |
void FuncionB() | |
{ | |
printf("B\n"); | |
} | |
int main() | |
{ | |
PBYTE A = reinterpret_cast<PBYTE>(FuncionA); | |
PBYTE B = reinterpret_cast<PBYTE>(FuncionB); | |
#ifdef DEBUG | |
printf("\nFuncionA (Contenido Real) ->"); | |
reinterpret_cast<Proceso>(A)(); | |
printf("Direccion de FuncionA() %08X [",A); | |
for (size_t i = 0; i < sizeof(A); ++i) | |
{ | |
printf("%02X " , static_cast<unsigned int>(A[i])); | |
} | |
printf("]\n"); | |
printf("Direccion de FuncionB() %08X\n",B); | |
#endif | |
DWORD oldProtection; | |
BOOL res = VirtualProtect(A,size_patch,PAGE_EXECUTE_READWRITE,&oldProtection); | |
if (!res) return 1; | |
#ifdef DEBUG | |
printf("Region Sin Proteccion %08X to %08X\n",A,(size_patch + A)); | |
#endif | |
DWORD distanciaFuncionB = B - A - size_patch; | |
*A = OP_JMP; | |
*reinterpret_cast<PDWORD>(A + 1) = distanciaFuncionB; | |
#ifdef DEBUG | |
printf("Direccion de FuncionA() %08X [",A); | |
for (size_t i = 0; i < sizeof(A); ++i) | |
{ | |
printf("%02X " , static_cast<unsigned int>(A[i])); | |
} | |
printf("]\nFuncionA (Contenido de B) ->"); | |
#endif | |
reinterpret_cast<Proceso>(A)(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment