VC File Filter System Driver Development Filemon Learning notes

Source: Internet
Author: User

Windows file filtering system Driven development, can be used for hard disk restore, anti-virus, file security protection, file encryption and many other fields. and mastering the core layer of the theory and practice for becoming a good developer is indispensable.

Windows file filtering system Driven development of two classic examples, Filemon and sfilter, beginners after a certain theoretical accumulation, the analysis of the two example code, will be a step into the driving development Hall, I believe that a certain theoretical accumulation and through the analysis of the two routines, will be able to start the file filtering system driven development of the actual work.

For the sfilter example, the Chu Madman's tutorial has been more popular, and Filemon example may be due to the frame structure relatively clear, easy to analyze understanding, no one posted a tutorial, I in the analysis of the process of filemon accumulation of some of the notes, gradually posted a hope for beginners to help, And through communication with everyone to improve each other.

Filemon Learning notes the first article:

The approximate schema for Filemon is that in this driver, two types of device objects are created.

A class of device objects are used to communicate with the Filemon corresponding EXE program to receive user input information, such as hook or monitor which partition, whether to hook up, whether to monitor, monitor what operation and so on. This device object creates only one, in the driver's entry function DriverEntry. This type of device object is generally called a control device object and has a name to facilitate the application layer and its communication operations.

The second type of device object is used to hook up to the partition to be monitored, such as c:,d: or e:,f: To intercept the read, write, and so on of the partition performed by the primer application layer. This type of device object is generally not named for security purposes and can be created one or more depending on how many partitions are to be monitored.

The drive entry function is roughly as follows:

NTSTATUS
DriverEntry (
In Pdriver_object DriverObject,
In Punicode_string Registrypath
)
{
NTSTATUS NTSTATUS;
Pdevice_object Guidevice;
WCHAR devicenamebuffer[] = L "\\Device\\Filemon";
Unicode_string devicenameunicodestring;
WCHAR devicelinkbuffer[] = L "\\DosDevices\\Filemon";
Unicode_string devicelinkunicodestring;
ULONG i;
Dbgprint (("Filemon.SYS:entering driverentry\n"));
Filemondriver = DriverObject;
//
Setup the device name
//
Rtlinitunicodestring (&devicenameunicodestring,
Devicenamebuffer);
//
Create the device used for GUI communications
This device object is used to interact with user information
NtStatus = IoCreateDevice (DriverObject,
sizeof (Hook_extension),
&devicenameunicodestring,
File_device_filemon,
0,
TRUE,
&guidevice);
//
If successful, make a symbolic link so allows for the device
Object ' s access from WIN32 programs
//
if (nt_success (NtStatus)) {
//
Mark this as our GUI device
//
((phook_extension) guidevice->deviceextension)->type = Guiinterface;
//
Create a symbolic link this GUI can specify to gain access
To this Driver/device
//
Rtlinitunicodestring (&devicelinkunicodestring,
Devicelinkbuffer);
NtStatus = Iocreatesymboliclink (&devicelinkunicodestring,
&devicenameunicodestring);
if (! Nt_success (NtStatus)) {
Dbgprint (("Filemon.SYS:IoCreateSymbolicLink failed\n"));
Iodeletedevice (Guidevice);
return ntStatus;
}
//
Create dispatch points for all routines this must be handled.
All entry points are registered since we might filter a
File system that processes all of them.
//
for (i = 0; I <= irp_mj_maximum_function; i++) {
Driverobject->majorfunction[i] = Filemondispatch;
}
#if DBG
//
Driver Unload is only set if we are debugging Filemon. This is
Because unloading a filter is not really safe-threads could
Be in my fastio routines (or about to enter them), for example,
And there is no way to tell. When debugging, we can risk the
Occasional unload crash as a trade-off for not have to
Reboot as often.
//
Driverobject->driverunload = Filemonunload;
#endif//DBG
//
Set up the Fast I/O dispatch table
//
Driverobject->fastiodispatch = &FastIOHook;
} else {
//
If something went wrong, cleanup the device object and don ' t load
//
Dbgprint ("filemon:failed to create We device!\n"));
return ntStatus;
}
//
Initialize The name hash table
//
for (i = 0; i < Numhash i++) hashtable[i] = NULL;
//
Find the Process name offset
//
Processnameoffset = Filemongetprocessnameoffset ();//To get the current process name
//
Initialize the synchronization objects
//
#if DBG
Keinitializespinlock (&countmutex);
#endif
Exinitializefastmutex (&logmutex);
Exinitializeresourcelite (&filterresource);
Exinitializeresourcelite (&hashresource);
//
Initialize a lookaside for file names
//
Exinitializenpagedlookasidelist (&fullpathlookaside, NULL, NULL,
0, Maxpathlen, ' Mlif ', 256);
//
Allocate the i output buffer
//
Currentlog = ExAllocatePool (NonPagedPool, sizeof (*currentlog));
if (! Currentlog) {
//
Oops-we can ' t do anything without at least one buffer
//
Iodeletesymboliclink (&devicelinkunicodestring);
Iodeletedevice (Guidevice);
return status_insufficient_resources;
}
//
Set the buffer pointer to the start of the buffer just allocated
//
Currentlog->len = 0;
Currentlog->next = NULL;
Numlog = 1;
return status_success;
}

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.