Source: Moeomu’s blog
String manipulation
The UNICODE_STRING structure is used in the kernel as the basic string structure. It should be noted that the lenth member of this structure is used to determine the string length, not
'\0'
.
String initialization
- Function:
RtlInitUnicodeString
- Parameters.
PUNICODE_STRING
:DestinationString
PCWSTR
:SourceString
- Return value: None
- IRQL:
<=DISPATCH_LEVEL
- Explanation: Initialize a WCHAR string ending with 0, the first parameter is the input parameter and also the output parameter
|
|
ps: it does not allocate space for buffer, but points directly to Source first address, so make sure Source is always valid, otherwise it is invalid access
String Copy
- Function:
RtlUnicodeStringCopyString
- Parameters
PUNICODE_STRING
:DestinationString
NTSTRSAFE_PCWSTR
:pszSrc
- Return value:
NTSTAUTS
- Successful execution returns
STATUS_SUCCESS
- Successful execution returns
- IRQL:
=PASSIVE_LEVEL
- Explanation: Copy a copy of src to dest
|
|
PS: In order to use the RtlUnicodeStringCopyString function, you should add the header file
Ntstrsafe.h
; you can’t copy to the String with fixed length buf, otherwise you will blue screen report memory read/write error
Chain table
Definition of a linked table
The following is the definition of a linked table in wdk
|
|
Using linked tables
|
|
- Generally, for ease of operation, a header node of a chain table is defined, containing nothing but a LIST_ENTRY structure.
Header node initialization
|
|
Node insertion
|
|
Link table traversal
|
|
- The role of
CONTAINING_RECORD
is to convert the address ofm_ListEntry
to the first address of the structureTestListEntry
. CONTAINING_RECORD
usage:CONTAINING_RECORD(PCHAR Address, TYPE Type, PCHAR Field)
Node Removal
- Remove the first node:
PLIST_ENTRY RemoveHeadList(PLIST_ENTRY ListHead)
- Remove the tail node:
PLIST_ENTRY RemoveTailList(PLIST_ENTRY ListHead)
- If successful, both of the above functions will return the address of the head of the chain, or NULL if they cannot be removed
- To remove a specific node.
BOOLEAN RemoveEntryList(PLIST_ENTRY Entry)
- If the chain becomes empty after removal, then TRUE will be returned, if it is not empty, then FALSE will be returned
Determine the state of the linked list
BOOLEAN IsListEmpty(const LIST_ENTRY *ListHead)
- It returns TRUE to indicate an empty linked table, otherwise it means the chain is non-empty
Spin locks
Using spin locks
A spinlock is a high IRQL lock provided by the kernel to access a resource in a synchronous and exclusive manner
Caution.
- The spinlock variable cannot be stored on the current function stack, otherwise it is the same as not initializing it every time you enter it
Initializing/using spin locks
|
|
Spin locks are used in bidirectional linked tables
|
|
Queue spinlock
Queue spinlock can have better performance on multi-CPU platforms, and also follows the first-wait-first-acquire spinlock principle.
- It is initialized in the same way as a normal spinlock, but the initialized spinlocks must not be mixed
|
|
Memory allocation
General memory allocation
|
|
Lookaside Memory Allocation
Benefits: High frequency of memory requests and releases from the system, using Lookaside allocation will greatly improve performance
- Note: In some places it is called “LookAside”.
|
|
Objects and handles
Objects created in the kernel, destroyed in the kernel, and managed and maintained by the kernel are called kernel objects
|
|
PS: there is a conflict when importing header files:
ntddk.h
andntifs.h
, the solution is to putntifs.h
in front ofntddk.h
and import it, so there is no conflict
Registry
The registry is actually the configuration storage structure of Windows, storing most of the system configuration information, most of the files are stored in the SYSTEM32\CONFIG directory under the system disk, these files are stored in the kernel space in a memory-mapped way, and then organized in the way of “HIVE”. The registry API actually manipulates the HIVE memory data, which is eventually written back to the corresponding file in the config directory
Open and close
- To be continued
|
|