@xor: it depends on the process you are trying to ptrace. I have had no problems ptracing a process of user "ca0s" from a program running as "ca0s".
Try this:
#include <stdio.h>
#include <sys/ptrace.h>
#include <dirent.h>
#include <stdlib.h>
#include <sys/reg.h>
#include <sys/user.h>
#include "scalls.h"
void clean(void);
int findPidByComm(char *);
int pid=-1;
int main(int argc, char *argv[])
{
atexit(clean);
printf("Proceso: %s - Pid: %i\n", argv[1], findPidByComm(argv[1]));
pid=findPidByComm(argv[1]);
int status, c;
long ret, ret2;
long arg1, arg2, arg3;
char *str;
ptrace(PTRACE_ATTACH, pid, NULL, NULL);
char reemp[]=0x00004141;
while(1)
{
ptrace(PTRACE_SYSCALL, pid, NULL, NULL);
wait(&status);
ret=ptrace(PTRACE_PEEKUSER, pid, ORIG_RAX*8, NULL);
if(ret==1)
{
printf("Syscall: %i -> write\n", ret);
arg1=ptrace(PTRACE_PEEKUSER, pid, 8 * RDI, NULL);
arg2=ptrace(PTRACE_PEEKUSER, pid, 8 * RSI, NULL);
arg3=ptrace(PTRACE_PEEKUSER, pid, 8 * RDX, NULL);
printf("ARGS -> RBX: %X - RCX: %X - RDX: %X\n", arg1, arg2, arg3);
str=malloc(arg3);
memset(str, 0, arg3);
for(c=0; c<=arg3;)
{
ret2=ptrace(PTRACE_PEEKDATA, pid, arg2+c, NULL);
memcpy(str+c*sizeof(long), &ret2, sizeof(long));
c+=sizeof(long);
}
printf("Texto: %s\n", str);
;ptrace(PTRACE_POKEDATA, pid, arg2, reemp);
free(str);
}
else printf("Syscall: %i -> %s\n", ret, syscalls[ret]);
}
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return 0;
}
int findPidByComm(char *name)
{
DIR *procs=opendir("/proc");
if(!procs) return -1;
struct dirent *pDir=NULL, *process=NULL;
FILE *shit=NULL;
char path[256];
char comm[256];
while((pDir=readdir(procs))!=NULL)
{
if((strcmp(pDir->d_name, ".")==0) || (strcmp(pDir->d_name, "..")==0)) continue;
memset(path, 0, 256);
strcat(path, "/proc/");
strcat(path, pDir->d_name);
strcat(path, "/comm");
// printf("/proc/%s\n", pDir->d_name);
shit=fopen(path, "r");
if(shit)
{
fscanf(shit, "%s", comm);
fclose(shit);
if(strcmp(comm, name)==0)
{
closedir(procs);
return atoi(pDir->d_name);
}
// printf("\t%s\n", comm);
}
}
closedir(procs);
return -1;
}
void clean(void)
{
ptrace(PTRACE_DETACH, pid, NULL, NULL);
}
// scalls.h
char* syscalls[] = {
"read",
"write",
"open",
"close",
"stat",
"fstat",
"lstat",
"poll",
"lseek",
"mmap",
"mprotect",
"munmap",
"brk",
"rt_sigaction",
"rt_sigprocmask",
"rt_sigreturn",
"ioctl",
"pread",
"pwrite",
"readv",
"writev",
"access",
"pipe",
"select",
"sched_yield",
"mremap",
"msync",
"mincore",
"madvise",
"shmget",
"shmat",
"shmctl",
"dup",
"dup2",
"pause",
"nanosleep",
"getitimer",
"alarm",
"setitimer",
"getpid",
"sendfile",
"socket",
"connect",
"accept",
"sendto",
"recvfrom",
"sendmsg",
"recvmsg",
"shutdown",
"bind",
"listen",
"getsockname",
"getpeername",
"socketpair",
"setsockopt",
"getsockopt",
"clone",
"fork",
"vfork",
"execve",
"_exit",
"wait4",
"kill",
"uname",
"semget",
"semop",
"semctl",
"shmdt",
"msgget",
"msgsnd",
"msgrcv",
"msgctl",
"fcntl",
"flock",
"fsync",
"fdatasync",
"truncate",
"ftruncate",
"getdents",
"getcwd",
"chdir",
"fchdir",
"rename",
"mkdir",
"rmdir",
"creat",
"link",
"unlink",
"symlink",
"readlink",
"chmod",
"fchmod",
"chown",
"fchown",
"lchown",
"umask",
"gettimeofday",
"getrlimit",
"getrusage",
"sysinfo",
"times",
"ptrace",
"getuid",
"syslog",
"getgid",
"setuid",
"setgid",
"geteuid",
"getegid",
"setpgid",
"getppid",
"getpgrp",
"setsid",
"setreuid",
"setregid",
"getgroups",
"setgroups",
"setresuid",
"getresuid",
"setresgid",
"getresgid",
"getpgid",
"setfsuid",
"setfsgid",
"getsid",
"capget",
"capset",
"rt_sigpending",
"rt_sigtimedwait",
"rt_sigqueueinfo",
"rt_sigsuspend",
"sigaltstack",
"utime",
"mknod",
"uselib",
"personality",
"ustat",
"statfs",
"fstatfs",
"sysfs",
"getpriority",
"setpriority",
"sched_setparam",
"sched_getparam",
"sched_setscheduler",
"sched_getscheduler",
"sched_get_priority_max",
"sched_get_priority_min",
"sched_rr_get_interval",
"mlock",
"munlock",
"mlockall",
"munlockall",
"vhangup",
"modify_ldt",
"pivot_root",
"_sysctl",
"prctl",
"arch_prctl",
"adjtimex",
"setrlimit",
"chroot",
"sync",
"acct",
"settimeofday",
"mount",
"umount",
"swapon",
"swapoff",
"reboot",
"sethostname",
"setdomainname",
"iopl",
"ioperm",
"create_module",
"init_module",
"delete_module",
"get_kernel_syms",
"query_module",
"quotactl",
"nfsservctl",
"getpmsg",
"putpmsg",
"afs_syscall",
"tuxcall",
"security",
"gettid",
"readahead",
"setxattr",
"lsetxattr",
"fsetxattr",
"getxattr",
"lgetxattr",
"fgetxattr",
"listxattr",
"llistxattr",
"flistxattr",
"removexattr",
"lremovexattr",
"fremovexattr",
"tkill",
"time",
"futex",
"sched_setaffinity",
"sched_getaffinity",
"set_thread_area",
"io_setup",
"io_destroy",
"io_getevents",
"io_submit",
"io_cancel",
"get_thread_area",
"lookup_dcookie",
"epoll_create",
"epoll_ctl_old",
"epoll_wait_old",
"remap_file_pages",
"getdents64",
"set_tid_address",
"restart_syscall",
"semtimedop",
"fadvise64",
"timer_create",
"timer_settime",
"timer_gettime",
"timer_getoverrun",
"timer_delete",
"clock_settime",
"clock_gettime",
"clock_getres",
"clock_nanosleep",
"exit_group",
"epoll_wait",
"epoll_ctl",
"tgkill",
"utimes",
"vserver",
"mbind",
"set_mempolicy",
"get_mempolicy",
"mq_open",
"mq_unlink",
"mq_timedsend",
"mq_timedreceive",
"mq_notify",
"mq_getsetattr",
"kexec_load",
"waitid",
"add_key",
"request_key",
"keyctl",
"ioprio_set",
"ioprio_get",
"inotify_init",
"inotify_add_watch",
"inotify_rm_watch",
"migrate_pages",
"openat",
"mkdirat",
"mknodat",
"fchownat",
"futimesat",
"newfstatat",
"unlinkat",
"renameat",
"linkat",
"symlinkat",
"readlinkat",
"fchmodat",
"faccessat",
"pselect6",
"ppoll",
"unshare",
"set_robust_list",
"get_robust_list",
"splice",
"tee",
"sync_file_range",
"vmsplice",
"move_pages",
"utimensat",
"epoll_pwait",
"signalfd",
"timerfd_create",
"eventfd",
"fallocate",
"timerfd_settime",
"timerfd_gettime",
"accept4",
"signalfd4",
"eventfd2",
"epoll_create1",
"dup3",
"pipe2",
"inotify_init1",
"preadv",
"pwritev",
"rt_tgsigqueueinfo",
"perf_event_open",
"recvmmsg",
"fanotify_init",
"fanotify_mark",
"prlimit64"
};
In this:
#include <stdio.h>
#include <unistd.h>
int main()
{
char str[]="Original\n";
printf("%i\n", getpid());
while(1)
{
printf("%s", str);
sleep(2);
}
}
(64bits code, if you only have a 32bits pc, it needs to be modified...)