So I was learning to write a little bit windows shellcoding and this guy showed in his tut that we'll be writing a shellcode to kill any process. Everything was working fine, in his video,his code worked fine but when I did all, mine didn't work so I decided to use Olly and trace where the fuck in the problem. I ran the code in Olly and it worked FINE, I mean yes, it worked in the debugger but when I run this outside the debugger, it just doesn't work. I don't really see any problem, and maybe there isn't cause in the debugger, everything running fine but it isn't running out of the debugger.
Here's the code #include<Windows.h>
#include<stdio.h>
char shellcode[] = {"\x6A\x00\x6A\xFF\xE8\x01\x00\x00\x00\xC3\xB8\x01\x01\x00\x00\xE8\x00\x00\x00\x00\x89\xE2\x0F\x34"};
DWORD shellcode_size = 0x18;
DWORD shellcode_offset = 0x00;
/*
The shellcode is :
push 0
push -1
call TERM
ret
TERM:
mov eax, 101h
call sys
sys:
mov edx, esp
syscall
ret
*/
void kill_app(DWORD pid){
char code[0x18];
memcpy(code, shellcode, 0x18);
HMODULE h = GetModuleHandle("NTDLL.DLL");
FARPROC f = GetProcAddress(h, "ZwTerminateProcess");
memcpy((char *)(shellcode+0x0B), (char *)((char *)f+1), 4);
HANDLE hProc = 0;
hProc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_WRITE | PROCESS_CREATE_THREAD, FALSE, pid);
LPVOID hRemoteMem = VirtualAllocEx(hProc, NULL, 0x18, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
DWORD numberBytesWritten = 0;
WriteProcessMemory(hProc, hRemoteMem, shellcode, 0x18, &numberBytesWritten);
HANDLE HRemoteThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)hRemoteMem, 0, 0, NULL);
CloseHandle(hProc);
}
int main(int argc, CHAR* argv[])
{
if (argc != 2)
{
printf("Usage: %s <pid_of_program_to_close>\n\n", argv[0]);
return 1;
}
DWORD pid = atoi(argv[1]);
kill_app(pid);
return 0;
}
Explanation: I write the shellcode, then get the address of ZwTerminateProcess function from ntdll.dll, then I open the process into which I have to execute this, then I allocate the memory, write it into the process memory, create a thread, run the code and since the shellcode is of exiting the process, it should kill the process of the given pid.