-
Notifications
You must be signed in to change notification settings - Fork 0
/
ltrace.c
270 lines (220 loc) · 5.32 KB
/
ltrace.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
LTRACE32.C -- cruddy win32 trace program.
Copyright (C) 2002 johnbrazel@gmail.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* Potential bugs:
* - all the places where ReadProcessMemory() tries to read a string of
* MAX_PATH characters: if string lies at end of readable memory segment,
* ReadProcessMemory() will fail if string < MAX_PATH len characters.
* - If libraries longjmp() or use dynamic exception handling, the return
* value catching will get screwed up.
* - If > 40960 library calls, the brkpnt_blk overflows.
*/
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "icommon.h"
#include "parse.h"
#include "ptrace.h"
#include "ps.h"
#include "hookpattern.h"
#include "print.h"
#include "write.h"
int debug = 0;
/* For a list of privileges, see winnt.h
* Privilege names symbolic defines begin with prefix 'SE_'
*/
BOOL
SetPrivilege(HANDLE hToken,LPCTSTR lpszPrivilege,BOOL bEnablePrivilege)
{
TOKEN_PRIVILEGES tp;
LUID luid;
if ( !LookupPrivilegeValue(NULL, lpszPrivilege, &luid))
{
WinPerror("LookupPrivilegeValue error");
return FALSE;
}
tp.PrivilegeCount = 1;
tp.Privileges[0].Luid = luid;
if (bEnablePrivilege)
{
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
}
else
{
tp.Privileges[0].Attributes = 0;
}
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(TOKEN_PRIVILEGES),
(PTOKEN_PRIVILEGES) NULL, (PDWORD) NULL);
if (GetLastError() != ERROR_SUCCESS)
{
WinPerror("AdjustTokenPrivileges failed");
return FALSE;
}
return TRUE;
}
void
do_traceables(void *tracer, char *arg)
{
char *s, *d;
d = arg;
while(d)
{
char *dll, *lib, *fn;
s = strchr(d, ',');
if (s) *s++ = '\0';
dll = d;
d = strchr(dll, ':');
if (d == NULL)
goto die;
*d++ = '\0';
if (*dll == '\0')
dll = NULL;
lib = d;
d = strchr(lib, ':');
if (d == NULL || (*d == '\0'))
goto die;
*d++ = '\0';
fn = d;
if (*fn == '\0')
fn = NULL;
if (SetHookPattern(tracer, dll, lib, fn))
{
error("Conflicting pattern '%s:%s:%s'\n", dll ? dll : "",
lib, fn ? fn : "");
exit(1);
}
d = s;
continue;
die:
error("-D must be followed by a '[import.dll]:exporting.dll:[fn_name]' argument\n");
exit(1);
}
}
int
main(int argc,char **argv)
{
HANDLE ptoken;
DWORD pid = 0;
char *cmdline;
int i, j, cmdlinelen = 0;
void *tracer;
int flags;
FILE *tracefile;
tracefile = stdout;
flags = 0;
for(i=1;i<argc;i++)
if (*argv[i] == '-')
{
switch(argv[i][1]) {
case 'a':
DoProcesses();
exit(0);
case 'p':
if (++i >= argc)
{
error("-p requires an argument\n");
exit(1);
}
pid = atoi(argv[i]);
break;
case 'd':
flags |= FLG_DESCEND;;
break;
case 'D':
if (++i >= argc)
{
error("-D requires an argument\n");
exit(1);
}
/* skip for now */
break;
case 'f':
if (parse_config(argv[++i]))
exit(0);
break;
case 'i':
flags |= FLG_INTR_PTR;
break;
case 'l':
flags |= FLG_LONG_NAME;
break;
case 'n':
flags |= FLG_NTDLL;
break;
case 'v':
debug = 1;
break;
case 'o':
if (++i >= argc)
{
error("-o requires an outout filename\n");
exit(0);
}
if ((tracefile = fopen(argv[i],"w")) == NULL)
{
perror(argv[i]);
exit(1);
}
break;
}
}
else
break;
if ((tracer = NewTrace(tracefile, flags, 0)) == NULL)
{
WinPerror("NewTrace()");
exit(1);
}
/* Do trace pattern arguments now that we have a trace handle. We can't create
* the trace handle before parsing the arguments, as we need to pass the file
* descriptor for the trace file to NewTrace().
*/
for(j=1;j<argc;j++)
if (*argv[j] != '-')
break;
else if (argv[j][1] == 'D')
do_traceables(tracer, argv[j]);
if (!OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
&ptoken))
{
WinPerror("OpenProcessToken() failed");
exit(1);
}
if (!SetPrivilege(ptoken,"SeDebugPrivilege",TRUE))
{
WinPerror("Failed to grant myself SeDebugPrivilege");
exit(1);
}
if (!pid)
{
for(j=i; j<argc; j++)
{
cmdlinelen += strlen(argv[j]) + 1;
}
cmdline = (char*)calloc(1,cmdlinelen + 1);
for(j=i; j<argc; j++)
{
strcat(cmdline,argv[j]);
strcat(cmdline," ");
}
SpawnTraceProcess(tracer, cmdline);
}
else
{
TraceProcess(tracer, pid);
}
return 0;
}
/* $Id: ltrace.c,v 1.9 2002/11/23 08:14:29 john Exp $ -- EOF */