-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextcd.c
151 lines (116 loc) · 3.12 KB
/
extcd.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* ExtCD - A standalone 'cd' command
* for GNU/Linux (i386)
*
* Copyright (C) 2007 Robert Swiecki <[email protected]>
* http://www.swiecki.net
*
* Licensed under:
* GNU LESSER GENERAL PUBLIC LICENSE version 2
* or alternatively under
* GNU LESSER GENERAL PUBLIC LICENSE version 3
*/
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <inttypes.h>
#include <sys/reg.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <asm/unistd.h>
#include <sys/mman.h>
#include <string.h>
static void fatal(const char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(1);
}
static void pfatal(const char *msg)
{
perror(msg);
exit(1);
}
#define _MAP_START 65536
#define _MAP_SIZE 4096
static void copypath(int ppid, const char *dir)
{
long *ptr;
int cnt;
if ((strlen(dir) + 1) > _MAP_SIZE)
fatal("directory name too long");
ptr = (long*) mmap(0, _MAP_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_FIXED | MAP_PRIVATE, 0, 0);
if (ptr == MAP_FAILED)
pfatal("mmap");
strncpy((char*)ptr, dir, _MAP_SIZE);
for (cnt = 0; cnt < (_MAP_SIZE / sizeof(long)); cnt++)
if (ptrace(PTRACE_POKEDATA, ppid, _MAP_START + (cnt * sizeof(long)), ptr[cnt]) == -1)
pfatal("ptrace");
munmap((void*)ptr, _MAP_SIZE);
}
static int changepdir(pid_t ppid, int count, const char *dir)
{
uint32_t eax;
eax = ptrace(PTRACE_PEEKUSER, ppid, 4 * ORIG_EAX, 0, 0);
if (eax == __NR_waitpid && count == 0) {
if (ptrace(PTRACE_POKEUSER, ppid, 4 * ORIG_EAX, __NR_mmap2) == -1)
pfatal("ptrace");
if (ptrace(PTRACE_POKEUSER, ppid, 4 * EBX, _MAP_START) == -1)
pfatal("ptrace");
if (ptrace(PTRACE_POKEUSER, ppid, 4 * ECX, _MAP_SIZE) == -1)
pfatal("ptrace");
if (ptrace(PTRACE_POKEUSER, ppid, 4 * EDX, PROT_READ | PROT_WRITE) == -1)
pfatal("ptrace");
if (ptrace(PTRACE_POKEUSER, ppid, 4 * ESI, MAP_ANONYMOUS | MAP_FIXED | MAP_PRIVATE ) == -1)
pfatal("ptrace");
return 1;
}
if (eax == __NR_mmap2 && count == 1) {
if (ptrace(PTRACE_POKEUSER, ppid, 4 * EAX, 0) == -1)
pfatal("ptrace");
copypath(ppid, dir);
return 1;
}
if (eax == __NR_waitpid && count == 2) {
if (ptrace(PTRACE_POKEUSER, ppid, 4 * ORIG_EAX, __NR_chdir) == -1)
pfatal("ptrace");
if (ptrace(PTRACE_POKEUSER, ppid, 4 * EBX, _MAP_START) == -1)
pfatal("ptrace");
return 1;
}
if (eax == __NR_chdir && count == 3) {
if (ptrace(PTRACE_POKEUSER, ppid, 4 * EAX, 0) == -1)
pfatal("ptrace");
return 1;
}
return 0;
}
int main(int argc, char **argv)
{
int status, count = 0;
pid_t ppid;
if (argc != 2)
fatal("Usage: cd [dir]");
ppid = getppid();
if (ptrace(PTRACE_ATTACH, ppid, 0, 0) == -1)
pfatal("ptrace");
for (;;) {
if (wait(&status) != ppid)
continue;
if (WIFEXITED(status))
fatal("Process finished\n");
if (WIFSIGNALED(status))
fatal("Process finished with signal");
if (!WIFSTOPPED(status))
continue;
if (WSTOPSIG(status) == SIGTRAP)
count += changepdir(ppid, count, argv[1]);
if (count == 4) {
ptrace(PTRACE_DETACH, ppid, 0, 0);
return 0;
}
if (ptrace(PTRACE_SYSCALL, ppid, 0, 0) == -1)
pfatal("ptrace");
}
return 0;
}