Windows APIs are so powerful that windows programmers will merely use third-party libraries.
Types and Hungarian Notation
Handles are items that have been opened or created in the OS. like window, process, module, menu, file. Like pointers in that they refer an object location in the memory.
Unlike pointers tho, handles cannot be used in arithmetic operations, and they do not always represent the object's address.
The only thing you can do with a handle is store it and use it in a later function call to refer to the same object.
CreateFile(dwCreationDisposition)- the parameter controls whether the CreateFile function create a new file or open a existing one.
ReadFile and WriteFile.
CreateFileMapping and MapViewOfFile
The program using these functions can easily load a file into memory and read and write anywhere in the file. it is handy when parsing a file format, because you can easily jump to different memory addresses.
File mappings are commonly used to replicate the functionality of the Windows loader. After obtaining a map of the file, the malware can parse the PE header and make all necessary changes to the file in memory, thereby causing the PE file to be executed as if it had been loaded by the OS loader.
Shared Files.
File Accessible via Namespaces.
Alternate Data Streams
The Windows registry is used to store OS and program configuration information, such as settings and options. Like the file system, it is a good source of host-based indicators and can reveal useful information about the malware’s functionality.
Key terms:
Malware usually will utilize these functions to edit the reg keys.
almost identical on Windows and UNIX system.
Berkeley compatible sockets’ network functionality in Windows is implemented in the Winsock libraries, primarily in ws2_32.dll. Of these, the socket, connect, bind, listen, accept, send, and recv functions are the most common, and these are described in Table 7-2.
there is a higher-level API called the WinINet API. The WinINet API functions are stored in Wininet.dll. If a program imports functions from this DLL, it’s using higher-level networking APIs.
The WinINet API implements protocols, such as HTTP and FTP, at the application layer. You can gain an understanding of what malware is doing based on the connections that it opens.
Processes can share memory addresses, and they often do. For example, if one process stores something at memory address 0x00400000, another can store something at that address, and the processes will not conflict. The addresses are the same, but the physical memory that stores the data is not the same.
Malware will often create a new process by storing one program inside another in the resource section. In Chapter 1, we discuss how the resource section of the PE file can store any file. Malware will sometimes store another executable in the resource section.
when one thread is running. it has complte control of the CPU or CPU core.the others cannot affect the state of the CPU(registers).
Before it switches to another thread, it will save all registers values in a structure called Thread Context. The OS then loads the thread context of a new thread into the CPU and executes the new thread.
CreateThread function (processthreadsapi.h) - Win32 apps
Note: ThreadFunction1:
Note: ThreadFunction2
In addition to threads, Microsoft systems use fibers. Fibers are like threads, but are managed by a thread, rather than by the OS. Fibers share a single thread context.
Mutex, Mutants.
In kernel conditions. Mutexes are global objects that coordinate multiple processes and threads. Mutexes are mainly used to control access to shared resources. For example, 2 Threads must access a memory structure, but only one can safely access it at a time. a mutex can be used to control access. Java.Lock.
CreateMutex. One process can get a handle to another process's mutex by using the OpenMutex. Malware will commonly create a mutex and attempt to open an existing mutex with the same name to ensure that only one version of malware is running at a time.
windows services. allows task to run without their own processes or threads by using services that runs as background applications.
—Windows Service Manager.
Each service on a local machine is stored in the registry. Subkey under HKLM\SYSTEM\CurrentControlSet\Services.
COM that makes it possible for different software componenets to call each other's code without knowledge of specifics about each othe
COM objects are accessed via GUIDS(globally unique identifiers) known as class identifiers(CLSIDs) and interface identifiers(IIDs).
CoCreateInstance is used to get access to COM functionality. EX. function Navigate which allows a program to launch IE and access a web address. The Navigate is part of IWebBroser2 Interface. But does not specify which program will provide that functionality. The function is coming from the interface IWebBrowser2 by COM class implemented in the program.
The interface IWebBroswer2 is identified with a GUID called IID. and classes are identified with a GUID called CLSID.
WALKTHRU:
malware.exe (CoCreateInstance) → accepts CLSID and IID of the object malware is requesting → OS searching for the class info and loads the program that will perform the function.(IE.exe) → IF the program isn't already running. (CoCreateInstance) class returns a pointer that points to a structure that contains function pointers. → supply that pointer address of the function to COM server.
nearly all code runs in user mode. Except the OS and hardware drivers. which runs in kernel mode.
Call windows API to manipulate kernel structures. SYSENTER, SYSCALL, or INT 0x2E. indicates that a call is being made into the kernel.
There are a series of Native API calls that can be used to get information about the system, processes, threads, handles, and other items. These include NtQuerySystemInformation, NtQueryInformationProcess, NtQueryInformationThread, NtQueryInformationFile, and NtQueryInformationKey. These calls provide much more detailed information than any available Win32 calls, and some of these functions allow you to set fine-grained attributes for files, processes, threads, and so on.
We covered several functions that start with the prefix Nt. In some instances, such as in the export tables of ntdll.dll, the same function can have either the Nt prefix or the Zw prefix. For example, there is an NtReadFile function and a ZwReadFile function. In the user space, these functions behave in exactly the same way, and usually call the exact same code. There are sometimes minor differences when called from kernel mode, but those differences can be safely ignored by the malware analyst.