Protection CoreCode
# Include "ntddk. H"
# Define nt_device_name l "// device // protectprocess"
# Define dos_device_name l "// dosdevices // protectprocess"
# Define ioctl_protect_control ctl_code (file_device_unknown, 0x800, method_buffered, file_any_access)
Ntstatus dispatchdevicecontrol (in pdevice_object deviceobject, in pirp );
Void onUnload (in pdriver_object driverobject );
# Structure of the Pragma pack (1) // ssdt table
Typedef struct servicedescriptorentry {
Unsigned int * servicetablebase;
Unsigned int * servicecountertablebase; // used only in checked build
Unsigned int numberofservices;
Unsigned char * paramtablebase;
} Servicedescriptortableentry_t, * pservicedescriptortableentry_t;
# Pragma pack ()
_ Declspec (dllimport) servicedescriptortableentry_t keservicedescriptortable; // The variable name cannot be changed because it is imported from outside
// This is a macro used to query the address of a function.
# Define systemservice (_ function) keservicedescriptortable. servicetablebase [* (Pulong) (puchar) _ FUNCTION + 1)]
Ntsysapi ntstatus ntapi zwopenprocess (Out phandle processhandle, in access_mask desiredaccess, in pobject_attributes objectattributes, in pclient_id clientid optional );
Typedef ntstatus (* zwopenprocess) (Out phandle processhandle, in access_mask desiredaccess, in pobject_attributes objectattributes, in pclient_id clientid optional );
Zwopenprocess oldzwopenprocess;
Long pid =-1;
Ntstatus newzwopenprocess (Out phandle processhandle, in access_mask desiredaccess, in pobject_attributes objectattributes, in pclient_id clientid optional)
{
// New function to be replaced
Ntstatus nstatus = STATUS_SUCCESS;
If (long) clientid-> uniqueprocess = PID)
{
Dbuplint ("protection process PID: % LD/N", pid );
Return STATUS_ACCESS_DENIED;
}
// The original functions left for us
Nstatus = oldzwopenprocess (processhandle, desiredaccess, objectattributes, clientid );
Return STATUS_SUCCESS;
}
Void onUnload (in pdriver_object driverobject)
{
// Called upon uninstallation
Unicode_string devicelinkstring;
Pdevice_object deviceobjecttemp1 = NULL;
Pdevice_object deviceobjecttemp2 = NULL;
Dbuplint ("DriverProgramUninstall.../N ");
Rtlinitunicodestring (& devicelinkstring, dos_device_name );
Iodeletesymboliclink (& devicelinkstring );
If (driverobject)
{
Deviceobjecttemp1 = driverobject-> deviceobject;
While (deviceobjecttemp1)
{
Deviceobjecttemp2 = deviceobjecttemp1;
Deviceobjecttemp1 = deviceobjecttemp1-> nextdevice;
Iodeletedevice (deviceobjecttemp2 );
}
}
Dbuplint ("the device has been detached/N ");
Dbuplint ("ssdt table/N ");
(Zwopenprocess) (systemservice (zwopenprocess) = oldzwopenprocess;
Dbuplint ("the driver has been uninstalled./N ");
}
Ntstatus dispatchdevicecontrol (in pdevice_object deviceobject, in pirp)
{
// Response function of irp_mj_device_control
Ntstatus nstatus = STATUS_SUCCESS;
Ulong iocontrolcode = 0;
Pio_stack_location irpstack = NULL;
Long * inbuf = NULL;
Char * outbuf = NULL;
Ulong insize = 0;
Ulong outsize = 0;
Pchar buffer = NULL;
Pmdl MDL = NULL;
IRP-> iostatus. Status = STATUS_SUCCESS;
IRP-> iostatus. Information = 0;
Irpstack = iogetcurrentirpstacklocation (IRP );
Switch (irpstack-> majorfunction)
{
Case irp_mj_create:
Dbuplint ("irp_mj_create called/N ");
Break;
Case irp_mj_close:
Dbuplint ("irp_mj_close called/N ");
Break;
Case irp_mj_device_control:
Dbuplint ("irp_mj_device_control called/N ");
Iocontrolcode = irpstack-> parameters. deviceiocontrol. iocontrolcode;
Switch (iocontrolcode)
{
Case ioctl_protect_control:
Insize = irpstack-> parameters. deviceiocontrol. inputbufferlength;
Outsize = irpstack-> parameters. deviceiocontrol. outputbufferlength;
Inbuf = (long *) IRP-> associatedirp. systembuffer;
PID = * inbuf;
Dbuplint ("=================================/N ");
Dbuplint ("ioctl_protect_control is called and communication is successful! /N ");
Dbuplint ("input buffer size: % d/N", insize );
Dbuplint ("output buffer size: % d/N", outsize );
Dbuplint ("input buffer content: % LD/N", * inbuf );
Dbuplint ("current protection process ID: % LD/N", pid );
Dbuplint ("=================================/N ");
Strcpy (IRP-> userbuffer, "OK! /N ");
Break;
Default:
Break;
}
Break;
Default:
Dbuplint ("unknown request package call/N ");
Break;
}
Nstatus = IRP-> iostatus. status;
Iocompleterequest (IRP, io_no_increment );
Return nstatus;
}
Ntstatus DriverEntry (in pdriver_object driverobject, in punicode_string theregistrypath)
{
// Driver entry function
Ntstatus = STATUS_SUCCESS;
Unicode_string ntdevicename;
Unicode_string devicelinkstring;
Pdevice_object deviceobject = NULL;
Dbuplint ("Driver loading.../N ");
Rtlinitunicodestring (& ntdevicename, nt_device_name );
Ntstatus = iocreatedevice (
Driverobject,
0,
& Ntdevicename,
File_device_unknown,
0,
False,
& Deviceobject );
If (! Nt_success (ntstatus ))
{
Dbuplint ("unable to create driver device ");
Return ntstatus;
}
Rtlinitunicodestring (& devicelinkstring, dos_device_name );
Ntstatus = iocreatesymboliclink (& devicelinkstring, & ntdevicename );
If (! Nt_success (ntstatus ))
{
Return ntstatus;
}
Driverobject-> majorfunction [irp_mj_create] = dispatchdevicecontrol;
Driverobject-> majorfunction [irp_mj_close] = dispatchdevicecontrol;
Driverobject-> majorfunction [irp_mj_device_control] = dispatchdevicecontrol;
Driverobject-> driverunload = onUnload;
Dbuplint ("the driver has been started/N ");
Dbuplint ("modifying ssdt table.../N ");
// Modify the zwopenprocess function address
Oldzwopenprocess = (zwopenprocess) (systemservice (zwopenprocess ));
(Zwopenprocess) (systemservice (zwopenprocess) = newzwopenprocess;
Dbuplint ("the driver has been loaded./N ");
Return STATUS_SUCCESS;
}
# Include <stdio. h>
# Include <stdlib. h>
# Include <windows. h>
# Include <process. h>
# Define buf_size 4096
Int main (INT argc, char * argv [])
{
Char path [buf_size];
Char base [buf_size];
Char sername [buf_size];
Char disname [buf_size];
Memset (path, 0, buf_size );
Memset (base, 0, buf_size );
Memset (sername, 0, buf_size );
Memset (disname, 0, buf_size );
SC _handle RH = NULL;
SC _handle SH = NULL;
If (argc = 1)
{
Printf ("Use: Install/start/uninstall/N ");
Exit (0 );
}
: Getmodulefilename (0, base, buf_size );
Int P = strlen (base );
While (base [p]! = '//') {P --;}
Strncpy (path, base, p + 1 );
Memset (base, 0, buf_size );
Sprintf (base, "% sinstall. ini", PATH );
Memset (path, 0, buf_size );
: Getprivateprofilestring ("Config", "path", "", path, buf_size, base );
: Getprivateprofilestring ("Config", "servicename", "", sername, buf_size, base );
: Getprivateprofilestring ("Config", "displayname", "", disname, buf_size, base );
Printf ("[*] service name: % s/n", sername );
Printf ("[*] display name: % s/n", disname );
Printf ("[*] driver path: % s/n", PATH );
SH = openscmanager (null, null, SC _manager_all_access );
If (! SH ){
Printf ("[-] Error openscmanger./N ");
Exit (0 );
}
If (argc = 2 &&! Strcmp (argv [1], "Install "))
{
If (! Strcmp (path ,""))
{
Printf ("[-] Error read install. INI/N ");
Exit (0 );
}
RH = createservice (Sh, sername, disname,
Service_all_access,
Service_kernel_driver,
//{
// Service_system_start,
Service_demand_start,
//}
Service_error_normal,
Path,
Null, null, null );
If (! RH ){
Printf ("[-] Error createservice./N ");
Exit (0 );
}
Printf ("[-] install service complete.../N ");
} Else if (argc = 2 &&! Strcmp (argv [1], "Start "))
{
RH = openservice (Sh, sername, service_all_access );
If (! RH ){
Printf ("error openservice./N ");
Exit (0 );
}
Startservice (RH, null, null );
Printf ("[-] Start service complete.../N ");
} Else if (argc = 2 &&! Strcmp (argv [1], "Uninstall "))
{
RH = openservice (Sh, sername, service_all_access );
If (! RH ){
Printf ("error openservice./N ");
Exit (0 );
}
Service_status SS;
Controlservice (RH, service_control_stop, & SS );
Printf ("[-] Stop Service complete.../N ");
Deleteservice (RH );
Printf ("[-] Delete service complete.../N ");
}
Closeservicehandle (RH );
Closeservicehandle (SH );
Return 1;
}