在PE文件结构中的区块表中的IMAGE_SECTION_HEADER结构体中有一个Characteristics属性,这个属性规定了区块的属性,该属性可以设置下面这些字段:
Flag | Meaning |
---|---|
|
Reserved. |
|
Reserved. |
|
Reserved. |
|
Reserved. |
|
The section should not be padded to the next boundary. This flag is obsolete and is replaced by IMAGE_SCN_ALIGN_1BYTES. |
|
Reserved. |
|
The section contains executable code. |
|
The section contains initialized data. |
|
The section contains uninitialized data. |
|
Reserved. |
|
The section contains comments or other information. This is valid only for object files. |
|
Reserved. |
|
The section will not become part of the image. This is valid only for object files. |
|
The section contains COMDAT data. This is valid only for object files. |
|
Reserved. |
|
Reset speculative exceptions handling bits in the TLB entries for this section. |
|
The section contains data referenced through the global pointer. |
|
Reserved. |
|
Reserved. |
|
Reserved. |
|
Reserved. |
|
Align data on a 1-byte boundary. This is valid only for object files. |
|
Align data on a 2-byte boundary. This is valid only for object files. |
|
Align data on a 4-byte boundary. This is valid only for object files. |
|
Align data on a 8-byte boundary. This is valid only for object files. |
|
Align data on a 16-byte boundary. This is valid only for object files. |
|
Align data on a 32-byte boundary. This is valid only for object files. |
|
Align data on a 64-byte boundary. This is valid only for object files. |
|
Align data on a 128-byte boundary. This is valid only for object files. |
|
Align data on a 256-byte boundary. This is valid only for object files. |
|
Align data on a 512-byte boundary. This is valid only for object files. |
|
Align data on a 1024-byte boundary. This is valid only for object files. |
|
Align data on a 2048-byte boundary. This is valid only for object files. |
|
Align data on a 4096-byte boundary. This is valid only for object files. |
|
Align data on a 8192-byte boundary. This is valid only for object files. |
|
The section contains extended relocations. The count of relocations for the section exceeds the 16 bits that is reserved for it in the section header. If the NumberOfRelocations field in the section header is 0xffff, the actual relocation count is stored in theVirtualAddress field of the first relocation. It is an error if IMAGE_SCN_LNK_NRELOC_OVFL is set and there are fewer than 0xffff relocations in the section. |
|
The section can be discarded as needed. |
|
The section cannot be cached. |
|
The section cannot be paged. |
|
The section can be shared in memory. |
|
The section can be executed as code. |
|
The section can be read. |
|
The section can be written to. |
比如
IMAGE_SCN_MEM_READ 规定了可读权限
IMAGE_SCN_MEM_WRITE 规定了可写权限
然后我在打开PE文件的时候使用了CreateFile这个函数,这个函数定义如下:
HANDLE CreateFile( LPCTSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile );HANDLE CreateFile( LPCTSTR lpFileName, //指向文件名的指针 DWORD dwDesiredAccess, //访问模式(写/读) DWORD dwShareMode, //共享模式 LPSECURITY_ATTRIBUTES lpSecurityAttributes, //指向安全属性的指针 DWORD dwCreationDisposition, //如何创建 DWORD dwFlagsAndAttributes, //文件属性 HANDLE hTemplateFile //用于复制文件句柄 ); 然后我的问题就是:这里在函数CreateFile中的形参中规定了文件的权限,在PE结构中的Characteristics中又规定了区块的权限,那么这两个权限有什么区别呢?
作为一个新手,怎么也想不明白这个问题。后来问了学长,终于揭开了我的疑惑:
首先在Chracteristics属性中规定的区块权限是在磁盘上的PE文件映射到了内存以后,如果要对内存中的PE区块进行可读可写等区块操作,这个时候就要用到这个Chracteristics属性了。
而在磁盘中的PE文件其实不过是一堆二进制数据。大家都知道在linux里面的话一切都是文件。把这个概念引申过来,那么在磁盘上PE文件和记事本的.txt其实是没有本质上的区别的,因为在计算机硬盘里存的就是一堆二进制代码的0和1.所以我们在CreateFile的时候在形参里面首先规定这个文件的读写权限,就可以对这个文件进行相应的操作了。
其实这个问题想明白了是很简单的。-_-|||简单的说,就是Chracteristics属性规定了在内存中的读写权限,而CreateFile形参中的dwDesiredAccess规定了打开文件以后在磁盘中的读写权限。