關於進程與端口映射的文章已經有很多了
首先
NTSYSAPI
NTSTSTUS
NTAPI
ZwOpenSection(
Out PHANDLE sectionHandle
IN ACCESS_MASK DesiredAccess
IN POBJECT_ATTRIBUTES ObjectAttributes
}
(見ntddk
第一個參數得到函數執行成功後的句柄
第二個參數DesiredAccess為一個常數
#define SECTION_QUERY
#define SECTION_MAP_WRITE
#define SECTION_MAP_READ
#define SECTION_MAP_EXECUTE
#define SECTION_EXTEND_SIZE
#define SECTION_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED|SECTION_QUERY| SECTION_MAP_WRITE | SECTION_MAP_READ | SECTION_MAP_EXECUTE | SECTION_EXTEND_SIZE)
(見ntddk
第三個參數是一個結構
typedef struct _OBJECT_ATTRIBUTES {
ULONG Length
HANDLE RootDirectory
PUNICODE_STRING ObjectName
ULONG Attributes
PVOID SecurityDescriptor
PVOID SecurityQualityOfService
} OBJECT_ATTRIBUTES
typedef OBJECT_ATTRIBUTES *POBJECT_ATTRIBUTES
(見ntdef
對於這個結構的初始化用一個宏完成:
#define InitializeObjectAttributes( p
(見ntdef
那麼
WCHAR PhysmemName[] = L
void * pMapPhysicalMemory
HANDLE pHandle
bool OpenPhysicalMemory()
{
NTSTATUS status
UNICODE_STRING physmemString
OBJECT_ATTRIBUTES attributes
RtlInitUnicodeString( &physmemString
InitializeObjectAttributes( &attributes
OBJ_CASE_INSENSITIVE
status = ZwOpenSection(pHandle
if( !NT_SUCCESS( status ))
return false
pMapPhysicalMemory=MapViewOfFile(pHandle
//從內存地址
if( GetLastError()!=
return false
return true
}
為什麼要從
程序打開打開內核對象\Device\PhysicalMemory後
函數的原型如下:
NTSYSAPI
NTSTATUS
NTAPI
ZwOpenFile(
OUT PHANDLE FileHandle
IN ACCESS_MASK DesiredAccess
IN POBJECT_ATTRIBUTES ObjectAttributes
OUT PIO_STATUS_BLOCK IoStatusBlock
IN ULONG ShareAccess
IN ULONG OpenOptions
)
(見ntddk
第一個參數返回打開對象的句柄
第二個參數DesiredAccess為一個常數
#define FILE_READ_DATA(
#define FILE_LIST_DIRECTORY (
#define FILE_WRITE_DATA (
#define FILE_ADD_FILE (
#define FILE_APPEND_DATA (
#define FILE_ADD_SUBDIRECTORY (
#define FILE_CREATE_PIPE_INSTANCE (
#define FILE_READ_EA (
#define FILE_WRITE_EA (
#define FILE_EXECUTE (
#define FILE_TRAVERSE (
#define FILE_DELETE_CHILD(
#define FILE_READ_ATTRIBUTES(
#define FILE_WRITE_ATTRIBUTES (
#define FILE_ALL_ACCESS (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE |
#define FILE_GENERIC_READ(STANDARD_RIGHTS_READ |FILE_READ_DATA |FILE_READ_ATTRIBUTES |FILE_READ_EA |SYNCHRONIZE)
#define FILE_GENERIC_WRITE (STANDARD_RIGHTS_WRITE |FILE_WRITE_DATA |FILE_WRITE_ATTRIBUTES |FILE_WRITE_EA|FILE_APPEND_DATA|SYNCHRONIZE)
#define FILE_GENERIC_EXECUTE(STANDARD_RIGHTS_EXECUTE |FILE_READ_ATTRIBUTES |FILE_EXECUTE |SYNCHRONIZE)
(見ntdef
第三個參數是一個結構
第四個參數返回打開對象的屬性
typedef struct _IO_STATUS_BLOCK {
union {
NTSTATUS Status
PVOID Pointer
}
ULONG_PTR Information
} IO_STATUS_BLOCK
#if defined(_WIN
typedef struct _IO_STATUS_BLOCK
NTSTATUS Status
ULONG Information
} IO_STATUS_BLOCK
#endif
(見ntddk
第五個參數ShareAccess是一個常數
#define FILE_SHARE_READ
#define FILE_SHARE_WRITE
#define FILE_SHARE_DELETE
(見ntddk
第六個參數OpenOptions也是一個常數
#define FILE_DIRECTORY_FILE
#define FILE_WRITE_THROUGH
#define FILE_SEQUENTIAL_ONLY
#define FILE_NO_INTERMEDIATE_BUFFERING
#define FILE_SYNCHRONOUS_IO_ALERT
#define FILE_SYNCHRONOUS_IO_NONALERT
#define FILE_NON_DIRECTORY_FILE
#define FILE_CREATE_TREE_CONNECTION
#define FILE_COMPLETE_IF_OPLOCKED
#define FILE_NO_EA_KNOWLEDGE
#define FILE_OPEN_FOR_RECOVERY
#define FILE_RANDOM_ACCESS
#define FILE_DELETE_ON_CLOSE
#define FILE_OPEN_BY_FILE_ID
#define FILE_OPEN_FOR_BACKUP_INTENT
#define FILE_NO_COMPRESSION
#define FILE_RESERVE_OPFILTER
#define FILE_OPEN_REPARSE_POINT
#define FILE_OPEN_NO_RECALL
#define FILE_OPEN_FOR_FREE_SPACE_QUERY
#define FILE_COPY_STRUCTURED_STORAGE
#define FILE_STRUCTURED_STORAGE
#define FILE_VALID_OPTION_FLAGS
#define FILE_VALID_PIPE_OPTION_FLAGS
#define FILE_VALID_MAILSLOT_OPTION_FLAGS
#define FILE_VALID_SET_FLAGS
(見ntddk
那麼
WCHAR physmemNameTcp[]=L
WCHAR physmemNameUdp[]=L
HANDLE pTcpHandle
HANDLE pUdpHandle
From:http://tw.wingwit.com/Article/os/xtgl/201311/8981.html