NTSTATUS
NtOpenFile(
OUT PHANDLE FileHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
OUT PIO_STATUS_BLOCK IoStatusBlock,
IN ULONG ShareAccess,
IN ULONG OpenOptions
);
NTSTATUS
ZwOpenProcess(
OUT PHANDLE ProcessHandle,
IN ACCESS_MASK DesiredAccess,
IN POBJECT_ATTRIBUTES ObjectAttributes,
IN PCLIENT_ID ClientId
);
NTSTATUS
PsLookupProcessByProcessId(
IN HANDLE ProcessId,
OUT PEPROCESS *Process
);
HANDLE
PsGetProcessId(
IN PEPROCESS Process
);
NTSTATUS
RtlFormatCurrentUserKeyPath(
OUT PUNICODE_STRING CurrentUserKeyPath
);
VOID KeAttachProcess( PEPROCESS proc );
VOID KeDetachProcess();
頭文件2:
/***************************************************************************************
* AUTHOR :
* DATE : 2009-6-15
* MODULE : ReadMemory.H
*
* IOCTRL Sample Driver
*
* Description:
* Demonstrates communications between USER and KERNEL.
*
****************************************************************************************
* Copyright (C) 2009 .
****************************************************************************************/
#ifndef CXX_READMEMORY_H
#define CXX_READMEMORY_H
#ifdef __cplusplus
extern "C" {
#endif
#include <ntddk.h>
#include <devioctl.h>
#include "common.h"
//
// TODO: Add your include here
//
//////////////////////////////////////////////////////////////////////////
//
// TODO: Add your struct,enum(private) here
//
//////////////////////////////////////////////////////////////////////////
//***************************************************************************************
//* NAME: DriverEntry
//*
//* DESCRIPTION: Registers dispatch routines.
//*
//* PARAMETERS: pDriverObj IN
//* Address of the DRIVER_OBJECT created by NT for this driver.
//* pRegistryString IN
//* UNICODE_STRING which represents this drivers KEY in the Registry.
//*
//* IRQL: IRQL_PASSIVE_LEVEL.
//*
//* RETURNS: NTSTATUS
//***************************************************************************************
NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObj, IN PUNICODE_STRING pRegistryString);
//***************************************************************************************
//* NAME: DriverUnload
//*
//* DESCRIPTION: This routine is our dynamic unload entry point.
//*
//* PARAMETERS: pDriverObj IN Address of our DRIVER_OBJECT.
//*
//* IRQL: IRQL_PASSIVE_LEVEL.
//*
//* RETURNS: None
//***************************************************************************************
VOID DriverUnload(IN PDRIVER_OBJECT pDriverObj);
//***************************************************************************************
//* NAME: DispatchCreate, DispatchClose
//*
//* DESCRIPTION: This two methods are the dispatch entry point for IRP_MJ_CREATE and IRP_MJ_CLOSE
//* routines. This sample simply completes the requests with success.
//*
//* PARAMETERS: pDevObj IN Address of our DRIVER_OBJECT.
//* pIrp IN Address of the IRP.
//*
//* IRQL: IRQL_PASSIVE_LEVEL.
//*
//* RETURNS: STATUS_SUCCESS
//***************************************************************************************
NTSTATUS DispatchCreate(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
NTSTATUS DispatchClose(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
//***************************************************************************************
//* NAME: DispatchDeviceControl
//*
//* DESCRIPTION: This is the dispatch entry point for IRP_MJ_DEVICE_CONTROL.
//*
//* PARAMETERS: pDevObj IN Address of our DRIVER_OBJECT.
//* pIrp IN Address of the IRP.
//*
//* IRQL: IRQL_PASSIVE_LEVEL.
//*
//* RETURNS: NTSTATUS
//*
//* NOTES: IRP_MJ_DEVICE_CONTROL
//* Parameters:
//* Parameters.DeviceIoControl.OutputBufferLength Length of OutBuffer
//* in bytes (length of buffer from GUI)
//* Parameters.DeviceIoControl.InputBufferLength Length of InBuffer
//* in bytes (length of buffer from DRIVER)
//* Parameters.DeviceIoControl.ControlCode I/O control code
//***************************************************************************************
NTSTATUS DispatchDeviceControl(IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
// common dispatch
//***************************************************
//* #define IRP_MJ_CREATE 0x00
//* #define IRP_MJ_CREATE_NAMED_PIPE 0x01
//* #define IRP_MJ_CLOSE 0x02
//* #define IRP_MJ_READ 0x03
//* #define IRP_MJ_WRITE 0x04
//* #define IRP_MJ_QUERY_INFORMATION 0x05
//* #define IRP_MJ_SET_INFORMATION 0x06
//* #define IRP_MJ_QUERY_EA 0x07
//* #define IRP_MJ_SET_EA 0x08
//* #define IRP_MJ_FLUSH_BUFFERS 0x09
//* #define IRP_MJ_QUERY_VOLUME_INFORMATION 0x0a
//* #define IRP_MJ_SET_VOLUME_INFORMATION 0x0b
//* #define IRP_MJ_DIRECTORY_CONTROL 0x0c
//* #define IRP_MJ_FILE_SYSTEM_CONTROL 0x0d
//* #define IRP_MJ_DEVICE_CONTROL 0x0e
//* #define IRP_MJ_INTERNAL_DEVICE_CONTROL 0x0f
//* #define IRP_MJ_SHUTDOWN 0x10
//* #define IRP_MJ_LOCK_CONTROL 0x11
//* #define IRP_MJ_CLEANUP 0x12
//* #define IRP_MJ_CREATE_MAILSLOT 0x13
//* #define IRP_MJ_QUERY_SECURITY 0x14
//* #define IRP_MJ_SET_SECURITY 0x15
//* #define IRP_MJ_POWER 0x16
//* #define IRP_MJ_SYSTEM_CONTROL 0x17
//* #define IRP_MJ_DEVICE_CHANGE 0x18
//* #define IRP_MJ_QUERY_QUOTA 0x19
//* #define IRP_MJ_SET_QUOTA 0x1a
//* #define IRP_MJ_PNP 0x1b
//* #define IRP_MJ_PNP_POWER IRP_MJ_PNP // Obsolete....
//* #define IRP_MJ_MAXIMUM_FUNCTION 0x1b -->
//***************************************************************************************
NTSTATUS DispatchCommon (IN PDEVICE_OBJECT pDevObj, IN PIRP pIrp);
ULONG GetPlantformDependentInfo(ULONG dwFlag);
//////////////////////////////////////////////////////////////////////////
#ifdef ALLOC_PRAGMA
// Allow the DriverEntry routine to be discarded once initialization is completed
#pragma alloc_text(INIT, DriverEntry)
//
#pragma alloc_text(PAGE, DriverUnload)
#pragma alloc_text(PAGE, DispatchCreate)
#pragma alloc_text(PAGE, DispatchClose)
#pragma alloc_text(PAGE, DispatchDeviceControl)
#pragma alloc_text(PAGE, DispatchCommon)
#endif // ALLOC_PRAGMA
//////////////////////////////////////////////////////////////////////////
//
// TODO: Add your module declarations here
//
頭文件3:
/**************************************************************************************
* AUTHOR :
* DATE : 2009-6-15
* MODULE : common.h
*
* Command:
* IOCTRL Common Header
*
* Description:
* Common data for the IoCtrl driver and application
*
****************************************************************************************
* Copyright (C) 2009 .
****************************************************************************************/
#pragma once
//#######################################################################################
// D E F I N E S
//#######################################################################################
#if DBG
#define dprintf DbgPrint
#else
#define dprintf
#endif
//不支持符號(hào)鏈接用戶相關(guān)性
#define DEVICE_NAME L"\\Device\\devNsanguo" // Driver Name
#define SYMBOLIC_LINK_NAME L"\\DosDevices\\Nsanguo" // Symbolic Link Name
#define WIN32_LINK_NAME "\\\\.\\Nsanguo" // Win32 Link Name
//支持符號(hào)鏈接用戶相關(guān)性
#define SYMBOLIC_LINK_GLOBAL_NAME L"\\DosDevices\\Global\\Nsanguo" // Symbolic Link Name
#define DATA_TO_APP "Hello World from Driver"
//
// Device IO Control Codes
//
#define IOCTL_BASE 0x800
#define MY_CTL_CODE(i) \
CTL_CODE \
( \
FILE_DEVICE_UNKNOWN, \
IOCTL_BASE + i, \
METHOD_BUFFERED, \
FILE_ANY_ACCESS \
)
#define IOCTL_READ_MEMORY MY_CTL_CODE(0)
#define IOCTL_SET_PROCESS MY_CTL_CODE(1)
#define IOCTL_GET_PROCESS MY_CTL_CODE(2)
#define IOCTL_WRITE_MEMORY MY_CTL_CODE(3)
#define IOCTL_GET_CR4 MY_CTL_CODE(4)
#define EPROCESS_SIZE 0
#define PEB_OFFSET 1
#define FILE_NAME_OFFSET 2
#define PROCESS_LINK_OFFSET 3
#define PROCESS_ID_OFFSET 4
#define EXIT_TIME_OFFSET 5
//
// TODO: Add your IOCTL define here
//
//
// TODO: Add your struct,enum(public) define here
//
PVOID g_pInfo = NULL;
/* EOF */
驅(qū)動(dòng)文件:
/***************************************************************************************
* AUTHOR :
* DATE : 2009-6-15
* MODULE : ReadMemory.C
*
* Command:
* Source of IOCTRL Sample Driver
*
* Description:
* Demonstrates communications between USER and KERNEL.
*
****************************************************************************************
* Copyright (C) 2009 .
****************************************************************************************/
//#######################################################################################
//# I N C L U D E S
//#######################################################################################
#ifndef CXX_READMEMORY_H
# include "ReadMemory.h"
#endif
#include "struct.h"
//////////////////////////////////////////////////////////////////////////
//#######################################################################################
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//@@@@@@@@ D R I V E R E N T R Y P O I N T @@@@@@@@
//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
//#######################################################################################
NTSTATUS
DriverEntry(IN PDRIVER_OBJECT pDriverObj, IN PUNICODE_STRING pRegistryString)
{
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING ustrLinkName;
UNICODE_STRING ustrDevName;
PDEVICE_OBJECT pDevObj;
int i = 0;
dprintf("[ReadMemory] EasySys Sample Driver\r\n"
"[ReadMemory] Compiled %s %s\r\n[ReadMemory] In DriverEntry : %wZ\r\n",
__DATE__, __TIME__, pRegistryString);
// Register dispatch routines
/*
for(i = 0; i < IRP_MJ_MAXIMUM_FUNCTION; i++)
{
pDriverObj->MajorFunction[i] = DispatchCommon;
}
*/
pDriverObj->MajorFunction[IRP_MJ_CREATE] = DispatchCreate;
pDriverObj->MajorFunction[IRP_MJ_CLOSE] = DispatchClose;
// Dispatch routine for communications
pDriverObj->MajorFunction[IRP_MJ_DEVICE_CONTROL] = DispatchDeviceControl;
// Unload routine
pDriverObj->DriverUnload = DriverUnload;
// Initialize the device name.
RtlInitUnicodeString(&ustrDevName, DEVICE_NAME);
// Create the device object and device extension
status = IoCreateDevice(pDriverObj,
0,
&ustrDevName,
FILE_DEVICE_UNKNOWN,
0,
FALSE,
&pDevObj);
if(!NT_SUCCESS(status))
{
dprintf("[ReadMemory] Error, IoCreateDevice = 0x%x\r\n", status);
return status;
}
//// Get a pointer to our device extension
//deviceExtension = (PDEVICE_EXTENSION) deviceObject->DeviceExtension;
//// Save a pointer to the device object
//deviceExtension->DeviceObject = deviceObject;
if(IoIsWdmVersionAvailable(1,0x10))
{
//如果是支持符號(hào)鏈接用戶相關(guān)性的系統(tǒng)
RtlInitUnicodeString(&ustrLinkName, SYMBOLIC_LINK_GLOBAL_NAME);
}
else
{
//不支持
RtlInitUnicodeString(&ustrLinkName, SYMBOLIC_LINK_NAME);
}
// Create a symbolic link to allow USER applications to access it.
status = IoCreateSymbolicLink(&ustrLinkName, &ustrDevName);
//
// TODO: Add your module definitions here.
//
ULONG GetPlantformDependentInfo(ULONG dwFlag)
{
ULONG current_build;
ULONG ans = 0;
PsGetVersion(NULL, NULL, ¤t_build, NULL);
switch ( dwFlag )
{
case EPROCESS_SIZE:
if (current_build == 2195) ans = 0 ; // 2000,當(dāng)前不支持2000,下同
if (current_build == 2600) ans = 0x25C; // xp
if (current_build == 3790) ans = 0x270; // 2003
break;
case PEB_OFFSET:
if (current_build == 2195) ans = 0;
if (current_build == 2600) ans = 0x1b0;
if (current_build == 3790) ans = 0x1a0;
break;
case FILE_NAME_OFFSET:
if (current_build == 2195) ans = 0;
if (current_build == 2600) ans = 0x174;
if (current_build == 3790) ans = 0x164;
break;
case PROCESS_LINK_OFFSET:
if (current_build == 2195) ans = 0;
if (current_build == 2600) ans = 0x088;
if (current_build == 3790) ans = 0x098;
break;
case PROCESS_ID_OFFSET:
if (current_build == 2195) ans = 0;
if (current_build == 2600) ans = 0x084;
if (current_build == 3790) ans = 0x094;
break;
case EXIT_TIME_OFFSET:
if (current_build == 2195) ans = 0;
if (current_build == 2600) ans = 0x078;
if (current_build == 3790) ans = 0x088;
break;
}
return ans;
}