1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/prctl.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ptrace.h>
#include <sys/prctl.h>
#include <sys/mman.h>
int* state;
int main(void)
{
state = mmap(NULL, sizeof(int), PROT_WRITE|PROT_READ, MAP_ANONYMOUS|MAP_SHARED, -1, 0);
*state = 0;
#ifndef PR_SET_PTRACER
#define PR_SET_PTRACER 0x59616d61
#endif
printf("----------- FIRST TRY ------------\n");
int status;
pid_t pid;
pid_t pidFork;
pid = getpid();
pidFork = fork();
printf("state %d\n", *state);
if (pidFork == -1)
{
printf("Error during fork");
exit(0);
}
// Parent attach son.
if (pidFork != 0)
{
printf("Run PID %d with new fork with PID %d\n", pid, pidFork);
printf("Set ptracer : %d for id :%d\n", prctl(PR_SET_PTRACER, pidFork, 0, 0, 0), pidFork);
status = ptrace(PTRACE_TRACEME, 0, 1, 0);
printf("Trace traceme %d for PID %d\n", status, getpid());
// Detect debug.
if (status == -1)
{
printf("Debugger detect LEAVING NOW !!!\n");
exit(1);
}
*state = 1;
}
printf("state for pid %d %d\n", getpid(), *state);
// Kill child.
if (pidFork == 0)
{
while(*state == 0){printf(""); sleep(1);}
status = ptrace(PTRACE_ATTACH, pid, 1, 0);
printf("\nTrace attach %d for PID %d\n", status, pid);
status = ptrace(PTRACE_DETACH, pid, 1, 0);
printf("Trace detach %d for PID %d\n", status, pid);
*state = 2;
}
// Assure we have kill the child.
while(*state != 2)
{}
printf("----------- SECOND TRY ------------\n");
*state = 0;
pidFork = fork();
if (pidFork == -1)
{
printf("Error during fork");
exit(0);
}
// Parent attach son.
if (pidFork != 0)
{
printf("Run PID %d with new fork with PID %d\n", pid, pidFork);
printf("Set ptracer : %d for id :%d\n", prctl(PR_SET_PTRACER, pidFork, 0, 0, 0), pidFork);
status = ptrace(PTRACE_TRACEME, 0, 1, 0);
printf("Trace traceme %d for PID %d\n", status, getpid());
// Detect debug.
if (status == -1)
{
printf("Debugger detect LEAVING NOW !!!\n");
exit(1);
}
*state = 1;
}
// Kill child.
if (pidFork == 0)
{
while(*state == 0){printf(""); sleep(1);}
status = ptrace(PTRACE_ATTACH, pid, 1, 0);
printf("\nTrace attach %d for PID %d\n", status, pid);
status = ptrace(PTRACE_DETACH, pid, 1, 0);
printf("Trace detach %d for PID %d\n", status, pid);
*state = 2;
}
// Assure we have kill the child.
while(*state != 2)
{}
// Kill parent.
if (pidFork != 0)
{
printf("Parent end of life.\n");
exit(0);
}
} |
Partager