Invisibility on NT boxes

=========================[ Invisibility on NT boxes ]===========================

                      How to become unseen on Windows NT
                      ----------------------------------

                        Author: Holy_Father <holy_father@phreaker.net>
                       Version: 1.2 english
                          Date: 05.08.2003
                           Web: http://www.hxdef.org, http://hxdef.net.ru,
                                http://hxdef.czweb.org, http://rootkit.host.sk
                          Link: http://www.rootkit.com


=====[ 1. Contents ]============================================================

 1. Contents
 2. Introduction
 3. Files
        3.1 NtQueryDirectoryFile
        3.2 NtVdmControl
 4. Processes
 5. Registry
        5.1 NtEnumerateKey
        5.2 NtEnumerateValueKey
 6. System services and drivers
 7. Hooking and spreading
        7.1 Rights
        7.2 Global hook
        7.3 New processes
        7.4 DLL
 8. Memory
 9. Handle
        9.1 Naming handle and getting type
10. Ports
        10.1 Netstat, OpPorts on WinXP, FPort on WinXP
        10.2 OpPorts on Win2k and NT4, FPort on Win2k
11. Ending



=====[ 2. Introduction ]========================================================

        This document is about technics of hiding objects, files, services, 
processes etc. on OS Windows NT. These methods are based on hooking Windows API 
functions which are described in my document "Hooking Windows API".
        Everything here was get from my own research during writing rootkit 
code, so there is a chance it can be written more effectively or it can be 
written much more easily. This also involve my implementation.
        Hiding arbitrary object in this document mean to change some system 
functions which name this object in the way they would skip its naming. In 
the case this object is only return value of that function we would return 
value as the object does not exist.
        Basic method (excluding cases of telling different) is that we would 
call original function with original arguments and then we would change its 
output.
        In this version of this text are described methods of hiding files, 
processes, keys and values in registry, system services and drivers, allocated 
memory and handles.



=====[ 3. Files ]===============================================================

        There are serveral possibilities of hiding files in the way OS would 
not see it. We would aim only changing API and leave out technics like those
which play on features of filesystem. It also is much easier because we dont 
need to know how particular filesystem works.


=====[ 3.1 NtQueryDirectoryFile ]===============================================

        Looking for a file on wNT in some directory is based on searching in 
all its files and files in all its subdirectories. For file enumeration is used 
function NtQueryDirectoryFile.

        NTSTATUS NtQueryDirectoryFile(
                IN HANDLE FileHandle,
                IN HANDLE Event OPTIONAL,
                IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
                IN PVOID ApcContext OPTIONAL,
                OUT PIO_STATUS_BLOCK IoStatusBlock,
                OUT PVOID FileInformation,
                IN ULONG FileInformationLength,
                IN FILE_INFORMATION_CLASS FileInformationClass,
                IN BOOLEAN ReturnSingleEntry,
                IN PUNICODE_STRING FileName OPTIONAL,
                IN BOOLEAN RestartScan
        );


        Important parameters for us are FileHandle, FileInformation 
and FileInformationClass. FileHandle is a handle of directory object which 
can be get from NtOpenFile. FileInformation is a pointer on allocated memory, 
where this function write wanted data to. FileInformationClass determines type 
of record written in FileInformation.
        FileInformationClass is varied enumerative type, but we need only 
four values which are used for enumerating directory content:
 
        #define FileDirectoryInformation 1
        #define FileFullDirectoryInformation 2
        #define FileBothDirectoryInformation 3
        #define FileNamesInformation 12


structure of recoed written in FileInformation for FileDirectoryInformation:

        typedef struct _FILE_DIRECTORY_INFORMATION { 
                ULONG NextEntryOffset;
                ULONG Unknown;
                LARGE_INTEGER CreationTime;
                LARGE_INTEGER LastAccessTime;
                LARGE_INTEGER LastWriteTime;
                LARGE_INTEGER ChangeTime;
                LARGE_INTEGER EndOfFile;
                LARGE_INTEGER AllocationSize; 
                ULONG FileAttributes;
                ULONG FileNameLength;
                WCHAR FileName[1];
        } FILE_DIRECTORY_INFORMATION, *PFILE_DIRECTORY_INFORMATION;


for FileFullDirectoryInformation:

        typedef struct _FILE_FULL_DIRECTORY_INFORMATION {
                ULONG NextEntryOffset;
                ULONG Unknown;
                LARGE_INTEGER CreationTime;
                LARGE_INTEGER LastAccessTime;
                LARGE_INTEGER LastWriteTime;
                LARGE_INTEGER ChangeTime;
                LARGE_INTEGER EndOfFile;
                LARGE_INTEGER AllocationSize;
                ULONG FileAttributes;
                ULONG FileNameLength;
                ULONG EaInformationLength;
                WCHAR FileName[1];
        } FILE_FULL_DIRECTORY_INFORMATION, *PFILE_FULL_DIRECTORY_INFORMATION;


for FileBothDirectoryInformation:

        typedef struct _FILE_BOTH_DIRECTORY_INFORMATION { 
                ULONG NextEntryOffset;
                ULONG Unknown;
                LARGE_INTEGER CreationTime;
                LARGE_INTEGER LastAccessTime;
                LARGE_INTEGER LastWriteTime;
                LARGE_INTEGER ChangeTime;
                LARGE_INTEGER EndOfFile;
                LARGE_INTEGER AllocationSize;
                ULONG FileAttributes;
                ULONG FileNameLength;
                ULONG EaInformationLength;
                UCHAR AlternateNameLength;
                WCHAR AlternateName[12];
                WCHAR FileName[1];
        } FILE_BOTH_DIRECTORY_INFORMATION, *PFILE_BOTH_DIRECTORY_INFORMATION; 


and for FileNamesInformation:

        typedef struct _FILE_NAMES_INFORMATION {
                ULONG NextEntryOffset;
                ULONG Unknown;
                ULONG FileNameLength;
                WCHAR FileName[1];
        } FILE_NAMES_INFORMATION, *PFILE_NAMES_INFORMATION;


        This function writes a list of these structures in FileInformation. 
Only three vairiables are important for us in any of these structure types.
        NextEntryOffset is the length of particular list item. First item 
can be found on address FileInformation + 0. So the second item is on address 
FileInformation + NextEntryOffset of first one. Last item has NextEntryOffset 
set on zero.
        FileName is a full name of the file.
        FileNameLength is a length of file name.

        If we want to hide a file, we need to tell apart these four types 
and for each returned record we need to compare its name with the one which 
we want to hide. If we want to hide first record, we have to move following 
structures by the size of the first. This will cause the first record would 
be rewritten. If we want to hide another record, we can easily change the value 
of NextEntryOffset of previous record. New value of NextEntryOffset would be 
zero if we want to hide the last record, otherwise the value would be the sum 
of NextEntryOffset of the record we want to hide and of previous record.
Then we should change the value of Unknown of previous record which is prolly 
an index for next search. The value of Unknown of previous record should have 
a value of Unknown of the record we want hide.
        If no record which should be seen was found, we will return error 
STATUS_NO_SUCH_FILE.

        #define STATUS_NO_SUCH_FILE 0xC000000F


=====[ 3.2 NtVdmControl ]=======================================================

        From unknown reason DOS emulation NTVDM can get a list of files also 
with function NtVdmContol.

        NTSTATUS NtVdmControl(        
                IN ULONG ControlCode,
                IN PVOID ControlData
        );

        ControlCode specifies the subfunction which is applied on data in 
ControlData buffer. If ControlCode equals to VdmDirectoryFile this function 
does the same as NtQueryDirectoryFile with FileInformationClass set on 
FileBothDirectoryInformation.

        #define VdmDirectoryFile 6

        Then ControlData is used like FileInformation. The only difference here 
is that we do not know the length of this buffer. So we have to count it 
manually. We have to add NextEntryOffset of all records and FileNameLength 
of the last record and 0x5E as a length of the last record excluding the name 
of the file. Hiding methods are the same as in NtQueryDirectoryFile then.



=====[ 4. Processes ]===========================================================

        Various system info is available using NtQuerySystemInformation.

        NTSTATUS NtQuerySystemInformation(
                IN SYSTEM_INFORMATION_CLASS SystemInformationClass,
                IN OUT PVOID SystemInformation,
                IN ULONG SystemInformationLength,
                OUT PULONG ReturnLength OPTIONAL
        );

        
        SystemInformationClass specifies the type of information which we want 
to get, SystemInformation is a pointer to the function output buffer, 
SystemInformationLength is the length of this buffer and ReturnLength is 
number of written bytes.
        For the enumeration of running processes we use SystemInformationClass 
set on SystemProcessesAndThreadsInformation.

        #define SystemInformationClass 5


        Returned structure in SystemInformation buffer is:

        typedef struct _SYSTEM_PROCESSES { 
                ULONG NextEntryDelta;
                ULONG ThreadCount;
                ULONG Reserved1[6];
                LARGE_INTEGER CreateTime;
                LARGE_INTEGER UserTime;
                LARGE_INTEGER KernelTime;
                UNICODE_STRING ProcessName; 
                KPRIORITY BasePriority;
                ULONG ProcessId;
                ULONG InheritedFromProcessId;
                ULONG HandleCount;
                ULONG Reserved2[2];
                VM_COUNTERS VmCounters;
                IO_COUNTERS IoCounters;  // Windows 2000 only
                SYSTEM_THREADS Threads[1];
        } SYSTEM_PROCESSES, *PSYSTEM_PROCESSES;


        Hiding processes is similiar as in the case of hiding files.
We have to change NextEntryDelta of previous record of that we want to hide. 
Usually we will not want to hide the first record here because it is Idle 
process.



=====[ 5. Registry ]============================================================

        Windows registry is quite big tree structure containing two important 
types of records for us which we could want to hide. First type is registry 
keys, second is values. Owing to registry structure hiding registry keys is 
not as trivial as hiding file or process.


=====[ 5.1 NtEnumerateKey ]=====================================================

        Owing to its structure we are not able to ask for a list of all keys 
in the specific part of registry. We can get only information about one key 
specified by its index in some part of registry. This provides NtEnumerateKey.

        NTSTATUS NtEnumerateKey(
                IN HANDLE KeyHandle,
                IN ULONG Index,
                IN KEY_INFORMATION_CLASS KeyInformationClass, 
                OUT PVOID KeyInformation,
                IN ULONG KeyInformationLength,
                OUT PULONG ResultLength
        );


        KeyHandle is a handle to a key in which we want to get information 
about a subkey specified by Index. Type of returned information is specified 
by KeyInformationClass. Data are written to KeyInformation buffer which length 
is KeyInformationLength. Number of written bytes is returned in ResultLength.
        The most important think we need to perceive is that if we hide a key, 
indexes of all following keys woould be shifted. And because we are able to get 
information about a key with higher index with asking for key with lower index 
we always have to count how many records before were hidden and then return 
the right one.
        Let's have a look on the example. Assume we have some keys called A, B, 
C, D, E and F in any part of registry. Indexing starts from zero which mean 
index 4 match E key. Now if we want to hide B key and the hooked application 
call NtEnumerateKey with Index 4 we should return information about F key 
because there is an index shift. The problem is that we don't know that there 
is a shift. And if we didn't care about shifting and return E instead of F when 
asking for key with index 4 we would return nothing when asking for key with 
index 1 or we would return C. Both cases are errors. This is why we have to 
care about shifting.
        Now if we counted the shift by recalling the function for each index 
from 0 to Index we would sometimes wait for ages (on 1GHz processor it could 
take up to 10 seconds with standard registry which is too much). So we have to 
think out more sophisticated method.
        We know that keys are (except of references) sorted alphabetically.
If we neglect references (which we don't want to hide) we can count the shift 
by following method. We will sort alphabetically our list of key names which we 
want to hide (RtlCompareUnicodeString can be used), then when application calls 
NtEnumerateKey we will not recall it with unchanged arguments but we will find 
out the name of the record specified by Index. 

        NTSTATUS RtlCompareUnicodeString(       
                IN PUNICODE_STRING String1, 
                IN PUNICODE_STRING String2, 
                IN BOOLEAN  CaseInSensitive  
        );

        String1 and String2 are strings which will be compared, CaseInSensitive 
is True if we want to compare with neglecting character case. 
        Function result describes relation between String1 and String2:

                result > 0:     String1 > String2
                result = 0:     String1 = String2
                result < 0:     String1 < String2


Now we have to find a border. We will compare alphabetically the name of 
the key specified by Index with the names in our list. The border would be 
the last lesser name from our list. We know that the shift is at most 
the number of the border in our list. But not all items from our list have to 
be a valid key in the part of registry we are in. So we have to ask for all 
items from our list up to border if they are in this part of the registry. 
This can be done using NtOpenKey.

        NTSTATUS NtOpenKey(
                OUT PHANDLE KeyHandle,
                IN ACCESS_MASK DesiredAccess,
                IN POBJECT_ATTRIBUTES ObjectAttributes
        );

        KeyHandle is a handle of superordinate key. We will use the value from 
NtEnumerateKey for it. DesiredAccess are access rights. KEY_ENUMERATE_SUB_KEYS 
is the right value for it. ObjectAttributes describes subkey which we want to 
open (including its name).

        #define KEY_ENUMERATE_SUB_KEYS 8

        If the result of NtOpenKey is 0 opening was successful which mean this 
key from our list exists. Opened key should be closed via NtClose.

        NTSTATUS NtClose(
                IN HANDLE Handle
        );

        
        For each call of NtEnumareteKey we will count the shift as a number of 
keys from our list which exist in the given part of registry. Then we will add 
this shift to Index argument and finally call the original NtEnumerateKey.
        For getting name of the key specified by Index we will use the value 
KeyBasicInformation as a KeyInformationClass.

        #define KeyBasicInformation 0

        NtEnumerateKey returns this structure in KeyInformation:

        typedef struct _KEY_BASIC_INFORMATION {
                LARGE_INTEGER LastWriteTime;
                ULONG TitleIndex;
                ULONG NameLength;
                WCHAR Name[1];            
        } KEY_BASIC_INFORMATION, *PKEY_BASIC_INFORMATION;

        Only thing we need here is Name and its length NameLength.
        If there is no entry for shifted Index we will return error 
STATUS_EA_LIST_INCONSISTENT.

        #define STATUS_EA_LIST_INCONSISTENT 0x80000014


=====[ 5.2 NtEnumerateValueKey ]================================================

        Registry values are not alphabetically sorted. Luckily the number 
of values in one key is quite small, so we can use recall method to get 
the shift. API for getting info about one value is called NtEnumerateValueKey.

        NTSTATUS NtEnumerateValueKey(
                IN HANDLE KeyHandle,
                IN ULONG Index,
                IN KEY_VALUE_INFORMATION_CLASS KeyValueInformationClass,
                OUT PVOID KeyValueInformation,
                IN ULONG KeyValueInformationLength,
                OUT PULONG ResultLength
        );

        KeyHandle is again a handle of superordinate key. Index is an index 
to the list of values in given key. KeyValueInformationClass describes a type 
of information which will be stored into KeyValueInformation buffer which 
is long KeyValueInformationLength bytes. Number of written bytes is returned 
in ResultLength.
        Again we have to count the shift but according to the number of values 
in one key we can recall this function for all indexes from 0 to Index. 
The name of the value can be get when KeyValueInformationClass is set to 
KeyValueBasicInformation.
        
        #define KeyValueBasicInformation 0


        Then we will get following structure in KeyValueInformation buffer: 

        typedef struct _KEY_VALUE_BASIC_INFORMATION {
                ULONG TitleIndex;
                ULONG Type;
                ULONG NameLength;
                WCHAR Name[1];
        } KEY_VALUE_BASIC_INFORMATION, *PKEY_VALUE_BASIC_INFORMATION;

        Again we are interested only in Name and NameLength.

        
        If there is no entry for shifted Index we will return error 
STATUS_NO_MORE_ENTRIES.

        #define STATUS_NO_MORE_ENTRIES 0x8000001A



=====[ 6. System services and drivers ]=========================================

        System services and drivers are enumerated by four independent API
functions. Their connections is different in each Windows version. That's why 
we have to hook all four functions. 

        BOOL EnumServicesStatusA(
                SC_HANDLE hSCManager,
                DWORD dwServiceType,
                DWORD dwServiceState,
                LPENUM_SERVICE_STATUS lpServices,
                DWORD cbBufSize,
                LPDWORD pcbBytesNeeded,
                LPDWORD lpServicesReturned,
                LPDWORD lpResumeHandle
        );

        BOOL EnumServiceGroupW(
                SC_HANDLE hSCManager,
                DWORD dwServiceType,
                DWORD dwServiceState,
                LPBYTE lpServices,
                DWORD cbBufSize,
                LPDWORD pcbBytesNeeded,
                LPDWORD lpServicesReturned,
                LPDWORD lpResumeHandle,
                DWORD dwUnknown
        );

        BOOL EnumServicesStatusExA(
                SC_HANDLE hSCManager,
                SC_ENUM_TYPE InfoLevel,
                DWORD dwServiceType,
                DWORD dwServiceState,
                LPBYTE lpServices,
                DWORD cbBufSize,
                LPDWORD pcbBytesNeeded,
                LPDWORD lpServicesReturned,
                LPDWORD lpResumeHandle,
                LPCTSTR pszGroupName
        );

        BOOL EnumServicesStatusExW(
                SC_HANDLE hSCManager,
                SC_ENUM_TYPE InfoLevel,
                DWORD dwServiceType,
                DWORD dwServiceState,
                LPBYTE lpServices,
                DWORD cbBufSize,
                LPDWORD pcbBytesNeeded,
                LPDWORD lpServicesReturned,
                LPDWORD lpResumeHandle,
                LPCTSTR pszGroupName
        );


        The most important here is lpServices which points on the buffer where 
the list of services would be stored. And also lpServicesReturned pointing on 
the number of records in result is important. Structure of data in the output 
buffer depends on the type of function. For functions EnumServicesStatusA 
and EnumServicesGroupW is returned structure 

        typedef struct _ENUM_SERVICE_STATUS {
                LPTSTR lpServiceName;
                LPTSTR lpDisplayName;
                SERVICE_STATUS ServiceStatus;
        } ENUM_SERVICE_STATUS, *LPENUM_SERVICE_STATUS;

        typedef struct _SERVICE_STATUS {
                DWORD dwServiceType;
                DWORD dwCurrentState;
                DWORD dwControlsAccepted;
                DWORD dwWin32ExitCode;
                DWORD dwServiceSpecificExitCode;
                DWORD dwCheckPoint;
                DWORD dwWaitHint;
        } SERVICE_STATUS, *LPSERVICE_STATUS;

for EnumServicesStatusExA a EnumServicesStatusExW it it

        typedef struct _ENUM_SERVICE_STATUS_PROCESS {
                LPTSTR lpServiceName;
                LPTSTR lpDisplayName;
                SERVICE_STATUS_PROCESS ServiceStatusProcess;
        } ENUM_SERVICE_STATUS_PROCESS, *LPENUM_SERVICE_STATUS_PROCESS;

        typedef struct _SERVICE_STATUS_PROCESS {
                DWORD dwServiceType;
                DWORD dwCurrentState;
                DWORD dwControlsAccepted;
                DWORD dwWin32ExitCode;
                DWORD dwServiceSpecificExitCode;
                DWORD dwCheckPoint;
                DWORD dwWaitHint;
                DWORD dwProcessId;
                DWORD dwServiceFlags;
        } SERVICE_STATUS_PROCESS, *LPSERVICE_STATUS_PROCESS;


        We are interested only in lpServiceName which is the name of system 
service. Records have static size, so if we want to hide one we will move all 
following records by its size. Here we have to differentiate between the size 
of SERVICE_STATUS and SERVICE_STATUS_PROCESS.



=====[ 7. Hooking and spreading ]===============================================

        To get the desiderative efect we have to hook all running processes 
and also all processes which would be created later. New processes should be 
hooked before running their first instruction of their own code otherwise 
they would be able to see our hidden objects in the time before they would be 
hooked.
        

=====[ 7.1 Rights ]=============================================================

        At first it is good to know that we need at least administrators rights
to get access to all running processes. The best possibility is to run our 
process as system service which run on user SYSTEM. To install the service we 
also need special rights. 
        Also getting SeDebugPrivilege is very useful. This can be done using 
API OpenProcessToken, LookupPrivilegeValue and AdjustTokenPrivileges.

        BOOL OpenProcessToken(
                HANDLE ProcessHandle,
                DWORD DesiredAccess,
                PHANDLE TokenHandle
        );

        BOOL LookupPrivilegeValue(
                LPCTSTR lpSystemName,
                LPCTSTR lpName,
                PLUID lpLuid
        );

        BOOL AdjustTokenPrivileges(
                HANDLE TokenHandle,
                BOOL DisableAllPrivileges,
                PTOKEN_PRIVILEGES NewState,
                DWORD BufferLength,
                PTOKEN_PRIVILEGES PreviousState,
                PDWORD ReturnLength
        );


        Neglecting errors the code can look like this:

        #define SE_PRIVILEGE_ENABLED    0x0002
        #define TOKEN_QUERY             0x0008
        #define TOKEN_ADJUST_PRIVILEGES 0x0020

        HANDLE hToken;
        LUID DebugNameValue;
        TOKEN_PRIVILEGES Privileges;
        DWORD dwRet;

        OpenProcessToken(GetCurrentProcess(),
                         TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,hToken);
        LookupPrivilegeValue(NULL,"SeDebugPrivilege",&DebugNameValue);
        Privileges.PrivilegeCount=1;
        Privileges.Privileges[0].Luid=DebugNameValue;
        Privileges.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
        AdjustTokenPrivileges(hToken,FALSE,&Privileges,sizeof(Privileges),
                              NULL,&dwRet);
        CloseHandle(hToken);


=====[ 7.2 Global hook ]========================================================

        Enumeration of processes is done by already metioned API function 
NtQuerySystemInformation. There are few native processes in the system, so we 
will use the method of rewriting first instructions of the function to hook 
them. For each running process we will do the same. We will allocate a part 
of memory in target process where we will write our new code for functions 
we want to hook. Then we will change the first five bytes of these functions 
with jmp instruction. This jump will redirect the execution to our code. 
So the jmp instruction will be executed immediately when the hooked function is 
called. We have to save first instructions of each function which is rewritten. 
We need them to call original code of the hooked function. Saving instructions 
is described in chapter 3.2.3 in the document "Hooking Windows API".
        At first we have to open target process via NtOpenProcess and get 
the handle. This will fail if we don't have enough rights. 

        NTSTATUS NtOpenProcess(
                OUT PHANDLE ProcessHandle,
                IN ACCESS_MASK DesiredAccess,
                IN POBJECT_ATTRIBUTES ObjectAttributes,
                IN PCLIENT_ID ClientId OPTIONAL
        );

        ProcessHandle is a pointer on a handle where the result will be stored. 
DesiredAccess should be set on PROCESS_ALL_ACCESS. We will set PID of target 
process to UniqueProcess part of ClientId structure, UniqueThread should be 0.
Open handle can be always closed via NtClose.

        #define PROCESS_ALL_ACCESS 0x001F0FFF

        Now we are going to allocate the part of memory for our code. This can 
be done using NtAllocateVirtualMemory.

        NTSTATUS NtAllocateVirtualMemory(
                IN HANDLE ProcessHandle,
                IN OUT PVOID BaseAddress,
                IN ULONG ZeroBits,
                IN OUT PULONG AllocationSize,
                IN ULONG AllocationType,
                IN ULONG Protect
        );

        ProcessHandle is the one from NtOpenProcess. BaseAddress is a pointer 
on a pointer on the beginning where we want to allocate. Here will be stored 
the address of the allocated memory. Input value can be NULL. AllocationSize 
is a pointer on number of bytes we want to allocate. And again it is also used 
as output value for the real number of allocated bytes. It is good to set 
AllocationType to MEM_TOP_DOWN in addition to MEM_COMMIT because the memory 
would be allocated on the highest possible address near DLLs.

        #define MEM_COMMIT      0x00001000
        #define MEM_TOP_DOWN    0x00100000      


        Then we can write our code there using NtWriteVirtualMemory.

        NTSTATUS NtWriteVirtualMemory(
                IN HANDLE ProcessHandle,
                IN PVOID BaseAddress,
                IN PVOID Buffer,
                IN ULONG BufferLength,
                OUT PULONG ReturnLength OPTIONAL
        );

        BaseAddress will be that address returned by NtAllocateVirtualMemory. 
Buffer points on bytes we want to write, BufferLength is number of bytes we 
want to write.

        Now we have to hook single functions. Only library which is loaded to 
all processes is ntdll.dll. So we have to check if function we want to hook is 
imported to the process if it is not from ntdll.dll. But the memory where would 
this function (from another DLL) be could be allocated, so rewriting bytes on 
its address could easily cause error in target process. This is why we have to 
check whether library (where function we want to hook is) is loaded to target 
process.
        We need to get PEB (Process Environment Block) of target process via 
NtQueryInformationProcess.

        NTSTATUS NtQueryInformationProcess(
                IN HANDLE ProcessHandle,
                IN PROCESSINFOCLASS ProcessInformationClass,
                OUT PVOID ProcessInformation,
                IN ULONG ProcessInformationLength,
                OUT PULONG ReturnLength OPTIONAL
        );

        We will set ProcessInfromationClass to ProcessBasicInformation. Then 
the PROCESS_BASIC_INFORMATION structure would be returned to ProcessInformation 
buffer which size is given by ProcessInformationLength.

        #define ProcessBasicInformation 0

        typedef struct _PROCESS_BASIC_INFORMATION {
                NTSTATUS ExitStatus;
                PPEB PebBaseAddress;
                KAFFINITY AffinityMask;
                KPRIORITY BasePriority;
                ULONG UniqueProcessId;
                ULONG InheritedFromUniqueProcessId;
        } PROCESS_BASIC_INFORMATION, *PPROCESS_BASIC_INFORMATION;

        PebBaseAddress is what we were looking for. On PebBaseAddress+0x0C is 
address PPEB_LDR_DATA. This would be get calling NtReadVirtualMemory.

        NTSTATUS NtReadVirtualMemory(
                IN HANDLE ProcessHandle,
                IN PVOID BaseAddress,
                OUT PVOID Buffer,
                IN ULONG BufferLength,
                OUT PULONG ReturnLength OPTIONAL
        );

        Parameters are similar like in NtWriteVirtualMemory. 
        On PPEB_LDR_DATA+0x1C is address InInitializationOrderModuleList. It is 
the list of libraries loaded to the process. We are interested only in a part 
of this structure.

        typedef struct _IN_INITIALIZATION_ORDER_MODULE_LIST {
                PVOID Next,
                PVOID Prev,
                DWORD ImageBase,
                DWORD ImageEntry,
                DWORD ImageSize,
                ...
        );

        Next is a pointer on next record, Prev on previous, last record points 
on first. ImageBase is an address of module in the memory, ImageEntry is 
the EntryPoint of the module, ImageSize is its size.
        
        For all libraries in which we want to hook we will get their ImageBase 
(e.g. using GetModuleHandle or LoadLibrary). This ImageBase we will compare 
with ImageBase of each entry in InInitializationOrderModuleList.
        Now we are ready for hooking. Because we are hooking running processes 
there is a possibility that the code we would be executed in the moment we will 
be rewriting it. This can cause error, so at first we will stop all threads 
in target process. The list of its threads can get via NtQuerySystemInformation 
with SystemProcessesAndThreadsInformation class. Result of this function is 
described in chapter 4. But we have to add the description of SYSTEM_THREADS 
structure where the information about thread is.

        typedef struct _SYSTEM_THREADS {
                LARGE_INTEGER KernelTime;
                LARGE_INTEGER UserTime;
                LARGE_INTEGER CreateTime;
                ULONG WaitTime;
                PVOID StartAddress;
                CLIENT_ID ClientId;
                KPRIORITY Priority;
                KPRIORITY BasePriority;
                ULONG ContextSwitchCount;
                THREAD_STATE State;
                KWAIT_REASON WaitReason;
        } SYSTEM_THREADS, *PSYSTEM_THREADS; 

        For each thread we have to get its handle using NtOpenThread. We will 
use ClientId for it.

        NTSTATUS NtOpenThread(
                OUT PHANDLE ThreadHandle,
                IN ACCESS_MASK DesiredAccess,
                IN POBJECT_ATTRIBUTES ObjectAttributes,
                IN PCLIENT_ID ClientId
        );

        The handle we want will be stored to ThreadHandle. We will set 
DesiredAccess to THREAD_SUSPEND_RESUME.

        #define THREAD_SUSPEND_RESUME 2

        ThreadHandle will be used for calling NtSuspendThread.

        NTSTATUS NtSuspendThread(
                IN HANDLE ThreadHandle,
                OUT PULONG PreviousSuspendCount OPTIONAL
        );


        Suspended process is ready for rewriting. We will proceed as it is 
described in chapter 3.2.2 in "Hooking Windows API". Only difference will be 
in using functions for other processes.

        After a hook we will revive all process threads calling NtResumeThread.

        NTSTATUS NtResumeThread(
                IN HANDLE ThreadHandle,
                OUT PULONG PreviousSuspendCount OPTIONAL
        );


=====[ 7.3 New processes ]======================================================

        Infection of all running processes does not affect processes which 
would be run later. We could get the process list and after a while get a new 
one and compare them and then infect those processes which are in second list 
but not in first. But this method is very unreliable.
        Much better is to hook function which is always called when new process 
starts. Because of hooking all running processes on the system we can't miss 
any new with this method. We can hook NtCreateThread but it is not the easiest 
way. We will hook NtResumeThread which is also called everytime after the new 
process is created. It is called after NtCreateThread.
        The only problem with NtResumeThread is that it is called not only when 
new process starts. But we can easily get over this. NtQueryInformationThread 
will give us an information about which process owns the specific thread. 
The last thing we have to do is to check whether this process is already hooked 
or not. This can be done by reading first byte of any function we are hooking.

        NTSTATUS NtQueryInformationThread(
                IN HANDLE ThreadHandle,
                IN THREADINFOCLASS ThreadInformationClass,
                OUT PVOID ThreadInformation,
                IN ULONG ThreadInformationLength, 
                OUT PULONG ReturnLength OPTIONAL
        );

        ThreadInformationClass is information class and it should be set in our 
case to ThreadBasicInformation. ThreadInformation is the buffer for result 
which size is ThreadInformationLength bytes.

        #define ThreadBasicInformation 0

        For class ThreadBasicInformation is this structure returned: 

        typedef struct _THREAD_BASIC_INFORMATION {
                NTSTATUS ExitStatus;
                PNT_TIB TebBaseAddress;
                CLIENT_ID ClientId;
                KAFFINITY AffinityMask;
                KPRIORITY Priority;
                KPRIORITY BasePriority;
        } THREAD_BASIC_INFORMATION, *PTHREAD_BASIC_INFORMATION;

        In ClientId is the PID of which owns the thread.

        Now we have to infect the new process. The problem is that the new 
process has only ntdll.dll in its memory. All others modules are loaded 
immediately after calling NtResumeThread. There are several ways how to handle 
this problem. E.g. we can hook API called LdrInitializeThunk which is called 
during process init.

        NTSTATUS LdrInitializeThunk(
                DWORD Unknown1,
                DWORD Unknown2,
                DWORD Unknown3
        );

        At first we will run original code and then we will hook all functions 
we want in this new process. But it will be better to unhook LdrInitializeThunk 
because it is called many times later and we don't want to rehook all functions 
again. Everything here is done before execution of the first instruction of 
hooked application. That's why there is no chance it would call any of hooked 
functions before we hook it.
        The hooking in itself is the same as when hooking running process 
but here we don't care about running threads.


=====[ 7.4 DLL ]================================================================

        In each process in the system is the copy of ntdll.dll. That mean we 
can hook any function from this module in the process init. But how about 
functions from other modules like kernel32.dll or advapi32.dll? And there are 
also several processes which has only ntdll.dll. All other modules can be 
loaded dynamically in the middle of the code after the process hook. That's why 
we have to hook LdrLoadDll which loades new modules.
        
        NTSTATUS LdrLoadDll( 
                PWSTR szcwPath,
                PDWORD pdwLdrErr,      
                PUNICODE_STRING pUniModuleName,
                PHINSTANCE pResultInstance
        );

        The most important for us here is pUniModuleName which is the name of 
the module. pResultInstance will be filled with its address if the call is 
successful.
        We will call original LdrLoadDll and then hook all functions in loaded 
module.



=====[ 8. Memory ]==============================================================

        When we are hooking a function we modify its first bytes. Via calling 
NtReadVirtualMemory anyone can detect that a function is hooked. So we have to 
hook NtReadVirtualMemory to prevent detecting.

        NTSTATUS NtReadVirtualMemory(
                IN HANDLE ProcessHandle,
                IN PVOID BaseAddress,
                OUT PVOID Buffer,
                IN ULONG BufferLength,
                OUT PULONG ReturnLength OPTIONAL
        );

        We have changed bytes on the begining of all functions we hooked and 
we have also allocated memory for our new code. We should check whether caller 
reads some of these bytes. If we have our bytes in the range from BaseAddress 
to BaseAddress + BufferLength we have to change some bytes in Buffer.
        If one ask for bytes from our allocated memory we should return empty 
Buffer and an error STATUS_PARTIAL_COPY. This value says not all requested 
bytes were copied to the Buffer. It is also used when asking for unallocated 
memory. ReturnLength should be set to 0 in this case.

        #define STATUS_PARTIAL_COPY 0x8000000D

        If one ask for first bytes of hooked function we have to call original 
code and than we should copy original bytes (we have saved them for original 
calls) to Buffer. 
        Now the process is not able to detect he is hooked via reading its 
memory. Also if you debug hooked process debugger will have a problem. It will 
show original bytes but it will execute our code.

        To make hiding perfect we can also hook NtQueryVirtualMemory. This 
function is used to get information about virtual memory. We can hook it to 
prevent detecting our allocated memory.

        NTSTATUS NtQueryVirtualMemory(
                IN HANDLE ProcessHandle,
                IN PVOID BaseAddress,
                IN MEMORY_INFORMATION_CLASS MemoryInformationClass,
                OUT PVOID MemoryInformation,
                IN ULONG MemoryInformationLength,
                OUT PULONG ReturnLength OPTIONAL
        );

        MemoryInformationClass specifies the class of data which are returned. 
First two types are interesting for us.

        #define MemoryBasicInformation 0
        #define MemoryWorkingSetList 1

        For class MemoryBasicInformation is returned this structure:

        typedef struct _MEMORY_BASIC_INFORMATION {
                PVOID BaseAddress;
                PVOID AllocationBase;
                ULONG AllocationProtect;
                ULONG RegionSize;
                ULONG State;
                ULONG Protect;
                ULONG Type;
        } MEMORY_BASIC_INFORMATION, *PMEMORY_BASIC_INFORMATION;

        Each memory section has its size RegionSize and its type Type. Free 
memory has type MEM_FREE.

        #define MEM_FREE 0x10000

        If a section before ours has type MEM_FREE we should add the size of 
ours section to its RegionSize. If the following section is also MEM_FREE we 
should add following section size again that RegionSize.
        If a section before ours has another type we will return MEM_FREE 
for our section. Its size is counted again according to following section.

        For class MemoryWorkingSetList is returned structure:

        typedef struct _MEMORY_WORKING_SET_LIST { 
                ULONG NumberOfPages;
                ULONG WorkingSetList[1];
        } MEMORY_WORKING_SET_LIST, *PMEMORY_WORKING_SET_LIST;

        NumberOfPages is the number of items in WorkingSetList. This number 
should be decreased. We should find ours section in WorkingSetList and move 
following records over ours. WorkingSetList is an array of DWORDs where higher 
20 bits specifies higher 20 bits of section address and lower 12 bits specifies 
flags.



=====[ 9. Handle ]==============================================================

        Calling NtQuerySystemInformation with SystemHandleInformation class
gives us array of all open handles in _SYSTEM_HANDLE_INFORMATION_EX strucure.

        #define SystemHandleInformation 0x10

        typedef struct _SYSTEM_HANDLE_INFORMATION {
                ULONG ProcessId;
                UCHAR ObjectTypeNumber;
                UCHAR Flags;
                USHORT Handle;
                PVOID Object;
                ACCESS_MASK GrantedAccess;
        } SYSTEM_HANDLE_INFORMATION, *PSYSTEM_HANDLE_INFORMATION;

        typedef struct _SYSTEM_HANDLE_INFORMATION_EX {
                ULONG NumberOfHandles;
                SYSTEM_HANDLE_INFORMATION Information[1];
        } SYSTEM_HANDLE_INFORMATION_EX, *PSYSTEM_HANDLE_INFORMATION_EX;

        ProcessId specifies the process which owns the handle. ObjectTypeNumber 
is handle type. NumberOfHandles is number of records in Information array. 
Hiding one item is trivial. We have to remove all following records by one 
and decrease NumberOfHandles. Removing all following is needed because handles 
in array are grouped by ProcessId. That mean all handles from one single 
process are together. And for one process the number Handle is growing.
        Now remember structure _SYSTEM_PROCESSES which is returned by this 
function with SystemProcessesAndThreadsInformation class. Here we can see that 
each process has an information about its number of handles in HandleCount. 
If we want to be perfect we should modify HandleCount owing to how many handles 
we hide when calling this function with SystemProcessesAndThreadsInformation
class. But this correction would be very time-consuming. There are many handles 
opening and closing in very short time during normal system running. So it can 
easily happend that number of handles is changed in between two calls of this 
function and we don't need to change HandleCount.


=====[ 9.1 Naming handle and getting type ]=====================================

        Handle hiding is trivial but find out which handle to hide is little 
bit harder. If we have e.g. hidden process we should hide all its handles and 
all handles which are connected with it. Hiding handles of this process is 
again trivial. We are only comparing ProcessId of handle and PID of our process 
and when they equals we hide it. But handles of other processes have to be 
named before we can compare something. The number of handles in the system is 
usually very big, so the best we can do is to compare handle type first before 
trying to name it. Naming types will save a lot of time for handles we are not 
interested in. 
        Naming handle and handle type can be done via calling NtQueryObject.

        NTSTATUS ZwQueryObject(
                IN HANDLE ObjectHandle,
                IN OBJECT_INFORMATION_CLASS ObjectInformationClass,
                OUT PVOID ObjectInformation,
                IN ULONG ObjectInformationLength,
                OUT PULONG ReturnLength OPTIONAL
        );

        ObjectHandle is a handle we want to get info about, 
ObjectInformationClass is the type of information which will be stored into 
ObjectInformation buffer which is ObjectInformationLength bytes long.
        We will use class ObjectNameInformation and ObjectAllTypesInformation.
ObjectNameInfromation class will fill the buffer with OBJECT_NAME_INFORMATION 
structure, ObjectAllTypesInformation class with OBJECT_ALL_TYPES_INFORMATION 
structure then.

        #define ObjectNameInformation 1
        #define ObjectAllTypesInformation 3

        typedef struct _OBJECT_NAME_INFORMATION {
                UNICODE_STRING Name;
        } OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION;

        Name determines the name of the handle.


        typedef struct _OBJECT_TYPE_INFORMATION {
                UNICODE_STRING Name;
                ULONG ObjectCount;
                ULONG HandleCount;
                ULONG Reserved1[4];
                ULONG PeakObjectCount;
                ULONG PeakHandleCount;
                ULONG Reserved2[4];
                ULONG InvalidAttributes;
                GENERIC_MAPPING GenericMapping;
                ULONG ValidAccess;
                UCHAR Unknown;
                BOOLEAN MaintainHandleDatabase;
                POOL_TYPE PoolType;
                ULONG PagedPoolUsage;
                ULONG NonPagedPoolUsage;
        } OBJECT_TYPE_INFORMATION, *POBJECT_TYPE_INFORMATION;

        typedef struct _OBJECT_ALL_TYPES_INFORMATION {
                ULONG NumberOfTypes;
                OBJECT_TYPE_INFORMATION TypeInformation;
        } OBJECT_ALL_TYPES_INFORMATION, *POBJECT_ALL_TYPES_INFORMATION;

        Name determines the name of type object which immediately follows 
each OBJECT_TYPE_INFORMATION structure. The next OBJECT_TYPE_INFORMATION 
structure follows this Name, starting on the first four-byte boundary.
        
        ObjectTypeNumber from SYSTEM_HANDLE_INFORMATION structure is an index 
to TypeInformation array.

        Harder is to get the name of handle from other process. There are two 
possibilities how to name it. First is to copy the handle via NtDuplicateObject 
to our process and then to name it. This method will fail for some specific 
types of handles. But it will fail only for few, so we can stay calm and use 
this.

        NtDuplicateObject(
                IN HANDLE SourceProcessHandle,
                IN HANDLE SourceHandle,
                IN HANDLE TargetProcessHandle,
                OUT PHANDLE TargetHandle OPTIONAL,
                IN ACCESS_MASK DesiredAccess,
                IN ULONG Attributes,
                IN ULONG Options
        );

        SourceProcessHandle is a handle of process which owns SourceHandle 
which is the handle we want to copy. TargetProcessHandle is handle of process 
where to copy. This will be handle to our process in our case. TargetHandle 
is the pointer on handle where to save a copy of original handle. DesiredAccess 
should be set to PROCESS_QUERY_INFORMATION, Attribures and Options to 0.

        Second naming method which works with any handle is to use system 
driver. Source code for this is available in OpHandle project on my site. 



=====[ 10. Ports ]==============================================================

        The easiest way to enumarate open ports is to use functions called 
AllocateAndGetTcpTableFromStack and AllocateAndGetUdpTableFromStack, and or
AllocateAndGetTcpExTableFromStack and AllocateAndGetUdpExTableFromStack from
iphlpapi.dll. The Ex functions are available since Windows XP.


        typedef struct _MIB_TCPROW {
                DWORD dwState;
                DWORD dwLocalAddr;
                DWORD dwLocalPort;
                DWORD dwRemoteAddr;
                DWORD dwRemotePort;
        } MIB_TCPROW, *PMIB_TCPROW;

        typedef struct _MIB_TCPTABLE {
                DWORD dwNumEntries;
                MIB_TCPROW table[ANY_SIZE];
        } MIB_TCPTABLE, *PMIB_TCPTABLE;

        typedef struct _MIB_UDPROW {
                DWORD dwLocalAddr;
                DWORD dwLocalPort;
        } MIB_UDPROW, *PMIB_UDPROW;

        typedef struct _MIB_UDPTABLE {
                DWORD dwNumEntries;
                MIB_UDPROW table[ANY_SIZE];
        } MIB_UDPTABLE, *PMIB_UDPTABLE;

        typedef struct _MIB_TCPROW_EX
        {
                DWORD dwState;
                DWORD dwLocalAddr;
                DWORD dwLocalPort;
                DWORD dwRemoteAddr;
                DWORD dwRemotePort;
                DWORD dwProcessId;
        } MIB_TCPROW_EX, *PMIB_TCPROW_EX;

        typedef struct _MIB_TCPTABLE_EX
        {
                DWORD dwNumEntries;
                MIB_TCPROW_EX table[ANY_SIZE];
        } MIB_TCPTABLE_EX, *PMIB_TCPTABLE_EX;

        typedef struct _MIB_UDPROW_EX
        {
                DWORD dwLocalAddr;
                DWORD dwLocalPort;
                DWORD dwProcessId;
        } MIB_UDPROW_EX, *PMIB_UDPROW_EX;

        typedef struct _MIB_UDPTABLE_EX
        {
                DWORD dwNumEntries;
                MIB_UDPROW_EX table[ANY_SIZE];
        } MIB_UDPTABLE_EX, *PMIB_UDPTABLE_EX;

        DWORD WINAPI AllocateAndGetTcpTableFromStack(
                OUT PMIB_TCPTABLE *pTcpTable,
                IN BOOL bOrder,
                IN HANDLE hAllocHeap,
                IN DWORD dwAllocFlags,
                IN DWORD dwProtocolVersion;
        );

        DWORD WINAPI AllocateAndGetUdpTableFromStack(
                OUT PMIB_UDPTABLE *pUdpTable,
                IN BOOL bOrder,
                IN HANDLE hAllocHeap,
                IN DWORD dwAllocFlags,
                IN DWORD dwProtocolVersion;
        );

        DWORD WINAPI AllocateAndGetTcpExTableFromStack(
                OUT PMIB_TCPTABLE_EX *pTcpTableEx,
                IN BOOL bOrder,
                IN HANDLE hAllocHeap,
                IN DWORD dwAllocFlags,
                IN DWORD dwProtocolVersion;
        );

        DWORD WINAPI AllocateAndGetUdpExTableFromStack(
                OUT PMIB_UDPTABLE_EX *pUdpTableEx,
                IN BOOL bOrder,
                IN HANDLE hAllocHeap,
                IN DWORD dwAllocFlags,
                IN DWORD dwProtocolVersion;
        );

        

        There is another way to do this stuff. When program creates a socket
and starts listening it surely has an open handle for it and for open port.
We can enumerate all open handles in the system and send them special buffer 
via NtDeviceIoControlFile to find out whether the handle is for open port 
or not. This will also give us information about the port. Because there are 
a lot of open handles we will test only handles which type is File and name 
is /Device/Tcp or /Device/Udp. Open ports have only this type and name.

        When we look to the code of iphlpapi.dll functions above we find out 
that these functions also calls NtDeviceIoControlFile and sends special buffer 
to get a list of all open ports in the system. That mean only functions we 
need to hook for hiding ports is NtDeviceIoControlFile.

        NTSTATUS NtDeviceIoControlFile(
                IN HANDLE FileHandle
                IN HANDLE Event OPTIONAL,
                IN PIO_APC_ROUTINE ApcRoutine OPTIONAL,
                IN PVOID ApcContext OPTIONAL,
                OUT PIO_STATUS_BLOCK IoStatusBlock,
                IN ULONG IoControlCode,
                IN PVOID InputBuffer OPTIONAL,
                IN ULONG InputBufferLength,
                OUT PVOID OutputBuffer OPTIONAL,
                IN ULONG OutputBufferLength
        );      

        Interesting agruments for us now are FileHandle which specify a handle 
of device to communicate with, IoStatusBlock which points to a variable that 
receives the final completion status and information about the requested 
operation, IoControlCode that is a number specifying type of the device, 
method, file access and a function. InputBuffer contains input data that are 
InputBufferLength bytes long and similarly OutputBuffer and OutputbufferLength.

       
=====[ 10.1 Netstat, OpPorts on WinXP, FPort on WinXP ]=========================

        Getting a list of all open ports is the first way which is used by e.g. 
OpPorts and FPort on Windows XP and also Netstat.
        Programs calls here NtDeviceIoControlFile twice with IoControlCode 
0x000120003. OutputBuffer is filled after a second call. Name of FileHandle is 
here alwats /Device/Tcp. InputBuffer differs for different types of call:

        1) To get an array of MIB_TCPROW InputBuffer looks as follows:

first call:
0x00 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x01 0x00 0x00 
0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 

second call:
0x00 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x01 0x00 0x00 
0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 


        2) To get an array of MIB_UDPROW:

first call:
0x01 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x01 0x00 0x00 
0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 

second call:
0x01 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x01 0x00 0x00 
0x01 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 


        3) To get an array of MIB_TCPROW_EX:

first call:
0x00 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x01 0x00 0x00 
0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 

second call:
0x00 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x01 0x00 0x00 
0x02 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 


        4) To get an array of MIB_UDPROW_EX:

first call:
0x01 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x01 0x00 0x00 
0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 

second call:
0x01 0x04 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x02 0x00 0x00 0x00 0x01 0x00 0x00 
0x02 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 
0x00 0x00 0x00 0x00 


        You can see the buffers are different in few bytes only. We can lucidly
recapitulate these:
        
        Calls we are interested in have InputBuffer[1] set to 0x04 and mainly 
InputBuffer[17] on 0x01. Only after these input data we get filled OutputBuffer
with desiderative tables. If we want to get info about TCP ports we set 
InputBuffer[0] on 0x00, or on 0x01 for information about UDP. If we want 
extended output tables (MIB_TCPROW_EX or MIB_UDPROW_EX) we use Inputbuffer[16]
in second call set to 0x02.

        If we find out the call with these parameters we can change the output 
buffer. To get number of rows in output buffer simply divide Information from 
IoStatusBlock by size of the row. Hiding of one row is easy then. Just rewrite 
it with following rows and delete last row. Don't forget to change 
OutputBufferLength and IoStatusBlock.


=====[ 10.2 OpPorts on Win2k and NT4, FPort on Win2k ]==========================

        We use NtDeviceIoControlFile with IoControlCode 0x00210012 to determine 
if the handle of File type and name /Device/Tcp or /Device/Udp is the handle of 
open port.

        So at first we compare IoControlCode and then a type and the name of 
the handle. If it is still interesting then we compare the length of input 
buffer which should be equal to the length of struct TDI_CONNECTION_IN. This 
length is 0x18. OutputBuffer is TDI_CONNECTION_OUT.

        typedef struct _TDI_CONNECTION_IN
        {
                ULONG UserDataLength,
                PVOID UserData,
                ULONG OptionsLength,
                PVOID Options,
                ULONG RemoteAddressLength,
                PVOID RemoteAddress
        } TDI_CONNECTION_IN, *PTDI_CONNECTION_IN;

        typedef struct _TDI_CONNECTION_OUT
        {
                ULONG State,
                ULONG Event,
                ULONG TransmittedTsdus,
                ULONG ReceivedTsdus,
                ULONG TransmissionErrors,
                ULONG ReceiveErrors,
                LARGE_INTEGER Throughput
                LARGE_INTEGER Delay,
                ULONG SendBufferSize,
                ULONG ReceiveBufferSize,
                ULONG Unreliable,
                ULONG Unknown1[5],
                USHORT Unknown2
        } TDI_CONNECTION_OUT, *PTDI_CONNECTION_OUT;


        Concrete implementation of how to determine the handle is open port 
is available in source code of OpPorts on my site. We are interested in hiding 
specific port now. We already compared InputBufferLength and IoControlCode. 
Now we have to compare RemoteAddressLength. This is always 3 or 4 for open 
port. The last we have to do is to compare ReceivedTsdus from OutputBuffer 
which contains the port in network form and our list of ports we want to hide. 
Differentiate between TCP and UDP is done according to the name of the handle. 
By deleting OutputBuffer, changing IoStatusBlock and returning the value 
of STATUS_INVALID_ADDRESS we will hide this port.


       
=====[ 11. Ending ]=============================================================

        Concrete implementation of described techniques will be available with 
the source code of Hander defender rootkit in version 1.0.0 on my site.
        It is possible I will add some more information about invisibility on 
Windows NT in the future. New versions of this document could also contain  
improvement of described methods or new comments. 
        Special thanks to Ratter who give me a lot of knowhow which was 
necessary to write this document and to code project Hacker defender. 
        Send all remarks into my mailbox or put them on the board on my site.

===================================[ End ]======================================
内容概要:本文提出了一种考虑不同充电需求的电动汽车有序充电调度方法,并提供了基于Matlab的完整代码实现。该方法通过构建精细化的数学模型,综合考量电动汽车用户的多样化充电需求,如充电起止时间、目标电量、充电偏好及用户满意度等因素,结合智能优化算法进行求解,实现对大规模电动汽车充电行为的协调控制。研究旨在通过有序调度策略有效平抑电网负荷波动,实现削峰填谷,降低配电网运行压力,提升电力系统运行的经济性与稳定性,尤其适用于未来高渗透率电动汽车接入场景下的充电管理与需求响应应用。; 适合人群:电气工程、自动化、能源系统及相关领域的科研人员、高校研究生,以及从事智能电网、电动汽车充电管理、能源优化调度等方向的技术人员,需具备一定的Matlab编程能力与优化理论基础。; 使用场景及目标:①应用于智能电网中规模化电动汽车集群的有序充电调度与能量管理;②支撑科研工作中关于需求响应、负荷调控、分布式资源优化调度等课题的模型构建与仿真验证;③为充电运营商或电力公司提供兼顾用户需求与电网安全的个性化、智能化充电服务解决方案。; 阅读建议:建议读者结合Matlab代码深入理解算法的具体实现流程,重点分析目标函数的设计思路、多类型约束条件的建模方式以及优化求解器的配置过程,可在此基础上拓展至多目标优化、实时滚动调度或考虑可再生能源不确定性的联合优化研究。
内容概要:本文研究了基于Benders分解的输配电网双层优化模型,旨在解决风电出力等不确定性因素对电网运行带来的挑战。模型采用TSO-DSO协调机制,其中输电网运营商(TSO)作为上层决策者负责全局优化与协调,配电网运营商(DSO)作为下层响应者进行本地优化。通过Benders分解算法将原问题分解为主问题与子问题,实现双层耦合系统的高效迭代求解,确保计算可行性与收敛性。研究涵盖了不确定性建模、双层博弈结构设计、协调变量传递机制及Benders割平面生成逻辑,并提供了完整的Matlab代码实现,具备良好的可复现性与工程应用价值。; 适合人群:具备电力系统优化、运筹学理论基础,熟悉Matlab编程语言,从事电力系统规划、调度、可再生能源集成及相关领域研究的研究生、科研人员及工程技术人员。; 使用场景及目标:① 掌握含不确定性因素的输配电网协同优化建模范式;② 深入理解Benders分解在多主体、多层次电力系统优化中的应用原理与实现路径;③ 开展高比例可再生能源接入背景下的电网调度仿真、鲁棒/分布鲁棒优化扩展研究及实际工程项目的技术验证; 阅读建议:建议结合Matlab代码逐模块剖析模型构建流程,重点关注主从问题间的变量耦合关系与Benders割的构造机制,进一步可引入多场景分析、分布鲁棒优化等高级不确定性处理方法进行模型拓展与深化研究。
源码链接: https://pan.quark.cn/s/a4b39357ea24 在深度学习领域,卷积神经网络(Convolutional Neural Network, CNN)是处理序列数据和图像数据的重要工具。 Keras 是一个高级神经网络API,它提供了便捷的方式来构建和训练CNN模型。 本文将深入探讨Keras中的`Conv1D`和`Conv2D`层的区别,帮助读者更好地理解和应用这两个关键组件。 `Conv1D`和`Conv2D`的主要区别在于它们处理的数据维度。 `Conv1D`主要用于一维数据,如时间序列分析、文本分类等,而`Conv2D`则用于二维数据,如图像处理。 1. 数据维度: - `Conv1D`:该层接受一维输入,形状通常是 `(batch_size, time_steps, features)`。 在这里,`time_steps`表示序列的长度,`features`是每个时间步的特征数量。 - `Conv2D`:该层处理二维输入,例如图像,其形状为 `(batch_size, height, width, channels)`。 `height`和`width`代表图像的高度和宽度,`channels`通常对应RGB图像的三个颜色通道或单通道灰度图像。 2. 卷积核(Kernel): - `Conv1D`的卷积核也是一维的,沿着输入的时间轴进行滑动,对每个时间步的特征进行卷积操作。 - `Conv2D`的卷积核是二维的,它同时在图像的高度和宽度方向上滑动,可以捕获空间上的局部特征。 3. 参数设置: - `kernel_size`:对于`Conv1D`,它是一个整数,表示卷积核在时间轴上的跨度。 对于`Conv2D`,它是一个包含两个整数...
代码下载链接: https://pan.quark.cn/s/a4b39357ea24 【华强北悦虎耳机弹窗动画功能nvr升级包】是一款专门为华强北地区生产的悦虎耳机所打造的软件升级解决方案,其核心功能在于为耳机增添或改进弹窗动画的相关特性。在苹果公司的产品中,当无线耳机与设备配对时,系统通常会展示一个设计精美的弹窗来展示耳机的当前状态,而这个升级包正是为了使非官方授权的悦虎耳机也能具备类似的功能而设计的。在接下来的内容中,我们将详细分析升级包的操作方法、技术原理以及与耳机相关的技术要点。 我们需要明确什么是升级过程。在电子产品的使用领域内,"升级"通常意味着通过软件更新或替换设备的操作系统和固件,以此来改善设备的功能表现、运行效率或视觉呈现。在这个具体场景中,"升级包"指的是一个包含新版本固件和相关配置信息的集合,它用于更新悦虎耳机的内部软件,使其能够支持弹窗动画功能。 悦虎耳机,作为华强北市场上的一种产品系列,其设计往往借鉴苹果AirPods的特点和性能。尽管在物理构造上可能达到了较高的相似程度,但在软件层面,非原装设备往往无法提供与正品相同的操作体验,特别是弹窗动画等细节。借助这个升级包,用户可以尝试将这些高级功能移植到他们的悦虎耳机上,从而优化使用感受。 洛达芯片是悦虎耳机及众多华强北AirPods仿制品普遍采用的一种蓝牙音频技术方案。洛达芯片因其可靠的蓝牙连接表现和出色的音质而受到认可,同时也为开发者提供了定制固件的可能性。升级包中的固件很可能就是针对洛达芯片进行特别调优的,目的是为了实现弹窗动画效果。 刷机流程通常包含以下几个环节: 1. 下载并展开升级包:务必确保从正规渠道获取升级包,以防止安装带有不良软件的版本。 2. 连接设备:通过数据线将耳机...
源码直接下载地址: https://pan.quark.cn/s/a4b39357ea24 JMeter的录制方法及过滤策略、线程组构成要素是什么? JMeter能够借助第三方录制工具(如BadBoy)或其自带的录制功能来完成录制工作,JMeter的录制机制:是借助HTTP代理服务器来捕获用户在操作网站时产生的链接信息。JMeter允许在配置HTTP代理服务器时,排除掉非必要的CSS、GIF等资源,以此减轻不必要的负担。 线程组涵盖:线程组的名称标识、附加注释说明、线程组内的用户数量、线程组完成请求的时间分配、循环执行次数、时间调度机制 【JMeter性能测试详解】 JMeter是一款功能强大的性能测试软件,常用于模拟大规模用户同时访问Web应用,用以衡量系统的性能表现和稳定性。接下来将具体说明JMeter的操作方法、线程组的设置以及性能测试的重要环节。 **JMeter录制与过滤** JMeter可以通过BadBoy等外部工具或其自带的HTTP代理服务器来记录用户的行为。其录制原理是JMeter作为HTTP代理,拦截用户浏览器发出的所有网络请求。在配置代理服务器时,能够过滤掉不必要的CSS、GIF等静态资源,以减少无效的负载。 **线程组配置** 线程组是JMeter测试计划的核心部分,包含以下几个关键参数: 1. **线程组名**:用于区分测试计划中的不同测试区域。 2. **注释**:用于记录测试目标或注意事项。 3. **线程数**:用于模拟并发用户的数量。 4. **循环次数**:每个线程需要执行的循环次数,可以设置为无限循环。 5. **Ramp-up period**:规定所有线程启动的时间跨度,旨在平滑增加负载。 6. **定时器**:例如思考时间或...
内容概要:本文研究了一种计及自适应预测修正的微电网模型预测控制(MPC)优化调度方法,并提供了完整的Matlab代码实现。该方法针对微电网中可再生能源(如风电)出力存在的强不确定性问题,引入自适应预测修正机制,有效提升短期预测精度与调度决策的可靠性。基于MPC的滚动优化框架,结合实时量测数据对预测偏差进行动态反馈校正,实现了源-荷-储多要素在多时间尺度下的协调优化调度,显著增强了系统的经济性、鲁棒性与运行稳定性。研究内容涵盖微电网系统建模、自适应修正策略设计、MPC优化模型构建及仿真验证全流程,具有明确的理论深度与工程应用价值。; 适合人群:具备电力系统、自动化、新能源等相关专业背景,熟悉Matlab/Simulink仿真环境,从事微电网能量管理、智能优化控制、可再生能源集成等方向研究的科研人员、高校研究生及工程技术开发者。; 使用场景及目标:①应用于高比例可再生能源接入的微电网能量管理系统设计;②解决风光发电预测误差引发的调度失配与运行风险问题;③实现微电网在不确定环境下的经济高效、安全可靠的优化运行;④为MPC控制策略在能源系统中的落地提供可复现的技术范例。; 阅读建议:学习者应结合所提供的Matlab代码,深入理解MPC滚动优化机制与自适应预测修正模块的实现逻辑,建议通过调整预测误差参数、对比有无修正机制的调度效果差异,全面掌握该方法的优势边界与适用条件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值