Changeset 2999 for neercs/trunk
- Timestamp:
- Oct 18, 2008, 11:54:52 PM (12 years ago)
- Location:
- neercs/trunk/src
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
neercs/trunk/src/grab.c
r2906 r2999 44 44 int mode[3]; 45 45 struct stat stat_buf; 46 struct termios tos;47 46 48 47 debug("pty is %s", ptyname); … … 56 55 57 56 child = mytrace_fork(parent); 58 mytrace_write(parent, 1, "\x1b[H\x1b[2J", 7); 59 mytrace_write(parent, 1, "\n[Process stolen by neercs]\n", 28); 60 mytrace_tcgets(parent, 1, &tos); 61 tos.c_lflag |= ICANON|ECHO; 62 mytrace_tcsets(parent, 1, &tos); 63 mytrace_close(parent, 1); 64 mytrace_exit(parent, 0); 57 mytrace_exec(parent, "/usr/bin/reset"); 58 mytrace_exit(parent, 0); /* In case the execve failed */ 65 59 mytrace_detach(parent); 60 mytrace_write(child, 1, "\x1b[H\x1b[2J", 7); 61 mytrace_write(child, 1, "\n[Process stolen by neercs]\n", 28); 66 62 pid = mytrace_getpid(child); 67 63 -
neercs/trunk/src/mytrace.c
r2906 r2999 18 18 #include <errno.h> 19 19 #include <fcntl.h> 20 #include <limits.h> 20 21 #include <stdio.h> 21 22 #include <stdlib.h> … … 23 24 24 25 #if defined USE_GRAB 25 # include <sys/ioctl.h>26 26 # include <sys/ptrace.h> 27 27 # include <sys/stat.h> … … 103 103 104 104 int mytrace_exit(struct mytrace *t, int status) 105 { 106 errno = ENOSYS; 107 return -1; 108 } 109 110 int mytrace_exec(struct mytrace *t, char const *command) 105 111 { 106 112 errno = ENOSYS; … … 165 171 #define MYCALL_FORK 7 166 172 #define MYCALL_EXIT 8 167 #define MYCALL_ IOCTL9173 #define MYCALL_EXECVE 9 168 174 169 175 #if defined __x86_64__ 170 176 /* from unistd_32.h on an amd64 system */ 171 int syscalls32[] = { 5, 6, 4, 63, 57, 66, 37, 2, 1, 54};177 int syscalls32[] = { 5, 6, 4, 63, 57, 66, 37, 2, 1, 11 }; 172 178 int syscalls64[] = 173 179 #else … … 175 181 #endif 176 182 { SYS_open, SYS_close, SYS_write, SYS_dup2, SYS_setpgid, SYS_setsid, 177 SYS_kill, SYS_fork, SYS_exit, SYS_ ioctl};183 SYS_kill, SYS_fork, SYS_exit, SYS_execve }; 178 184 179 185 char const *syscallnames[] = 180 186 { "open", "close", "write", "dup2", "setpgid", "setsid", "kill", "fork", 181 "exit", " ioctl" };187 "exit", "execve" }; 182 188 183 189 struct mytrace … … 401 407 } 402 408 403 int mytrace_ tcgets(struct mytrace *t, int fd, struct termios *tos)409 int mytrace_exec(struct mytrace *t, char const *command) 404 410 { 405 411 #if defined USE_GRAB 406 412 struct user_regs_struct regs; 407 struct termios mytos; 408 int ret; 413 char *env; 414 char envpath[PATH_MAX+1]; 415 ssize_t envsize = 32*1024; 416 int ret, fd, l; 417 ssize_t r; 409 418 410 419 if(ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0) … … 414 423 } 415 424 416 /* Backup the data that we will use */ 417 if(memcpy_from_target(t, (char *)&mytos, regs.RSP, sizeof(struct termios)) < 0) 418 return -1; 419 420 ret = remote_syscall(t, MYCALL_IOCTL, fd, TCGETS, regs.RSP); 421 422 memcpy_from_target(t, (char *)tos, regs.RSP, sizeof(struct termios)); 423 424 /* Restore the data */ 425 memcpy_into_target(t, regs.RSP, (char *)&mytos, sizeof(struct termios)); 426 427 if(ret < 0) 428 { 429 errno = ret; 430 return -1; 431 } 432 433 return ret; 434 #else 435 errno = ENOSYS; 436 return -1; 437 #endif 438 } 439 440 int mytrace_tcsets(struct mytrace *t, int fd, struct termios *tos) 441 { 442 #if defined USE_GRAB 443 struct user_regs_struct regs; 444 struct termios mytos; 445 int ret; 446 447 if(ptrace(PTRACE_GETREGS, t->pid, NULL, ®s) < 0) 448 { 449 fprintf(stderr, "PTRACE_GETREGS failed\n"); 450 return errno; 451 } 452 453 /* Backup the data that we will use */ 454 if(memcpy_from_target(t, (char *)&mytos, regs.RSP, sizeof(struct termios)) < 0) 455 return -1; 456 457 memcpy_into_target(t, regs.RSP, (char *)tos, sizeof(struct termios)); 458 459 ret = remote_syscall(t, MYCALL_IOCTL, fd, TCSETS, regs.RSP); 460 461 /* Restore the data */ 462 memcpy_into_target(t, regs.RSP, (char *)&mytos, sizeof(struct termios)); 425 env = malloc(envsize); 426 if (!env) 427 return -1; 428 429 snprintf(envpath, PATH_MAX, "/proc/%d/environ", t->pid); 430 431 fd = open(envpath, O_RDONLY); 432 r = read(fd, env, envsize); 433 close(fd); 434 if (r == -1) 435 return -1; 436 while (r == envsize) 437 { 438 free(env); 439 env = malloc(envsize); 440 if (!env) 441 return -1; 442 fd = open(envpath, O_RDONLY); 443 r = read(fd, env, envsize); 444 close(fd); 445 if (r == -1) 446 return -1; 447 } 448 l = strlen(command)+1; 449 memcpy_into_target(t, regs.RSP, command, l); 450 memcpy_into_target(t, regs.RSP+l, env, envsize); 451 free(env); 452 ret = remote_syscall(t, MYCALL_EXECVE, regs.RSP, 0, regs.RSP+l); 463 453 464 454 if(ret < 0) -
neercs/trunk/src/mytrace.h
r2906 r2999 12 12 * http://sam.zoy.org/wtfpl/COPYING for more details. 13 13 */ 14 15 #include <termios.h>16 14 17 15 struct mytrace; … … 30 28 int mytrace_kill(struct mytrace *t, long pid, int sig); 31 29 int mytrace_exit(struct mytrace *t, int status); 32 int mytrace_tcgets(struct mytrace *t, int fd, struct termios *tos); 33 int mytrace_tcsets(struct mytrace *t, int fd, struct termios *tos); 30 int mytrace_exec(struct mytrace *t, char const *command);
Note: See TracChangeset
for help on using the changeset viewer.