Problems with self-mutation code

I’m doing a self-mutation, but when I try to change the page permisions. it works , but it always get 1 on both sides when it should be 42 the overwrite. as I cannot do void arithmetic operations so how can I fix my code however it works on dev c++ while in visual not?


int change_page_permissions_of_address(void* address) {
	// Move the pointer to the page boundary
	unsigned char* addr = reinterpret_cast<unsigned char *>(address);

	int page_size = getpagesize();
	DWORD dwOldProtect;

	addr -= (uintptr_t)addr % page_size;

	if (VirtualProtect(addr, page_size, PAGE_EXECUTE_READWRITE, &dwOldProtect) == -1) {
		return -1;
	}

	return 0;
}


int main() {
	void *foo_addr = (void*)foo;
	if (change_page_permissions_of_address(foo_addr) == -1) {
		printf("Error while changing page permissions of foo(): %s\n");
		return 1;
	}

	// Call the unmodified foo()
	puts("Calling foo...");
	foo();

	// Change the immediate value in the addl instruction in foo() to 42
	unsigned char *instruction = (unsigned char*)foo_addr + 18;
	*instruction = 0x2A;
	
	// Call the modified foo()
	puts("Calling foo...");
	foo();
	getchar();
	return 0;

}


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.