




























































































Studia grazie alle numerose risorse presenti su Docsity
Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium
Questa pagina non è visibile nell’anteprima
Non perderti parti importanti!





























































































What’s in the file?
○ Code
○ Data
○ Other resources
○ Metadata
The Portable Execution (PE) File Format
Used by Windows executable files, object code, and DLLs
▪ Very convenient - > A single file format for several different usage scenarios
Means that you treat EXEs and DLLs in a very similar way
○ A data structure that contains the information necessary to Windows for file loading
○ Almost every file executed on Windows is in PE format
○ Starts with MS-DOS header+stub - > just for compatibility
○ The PE headers contain metadata used to correctly load and execute the file
File content is structured in “sections”
▪ Each section contains a specific kind of data
▪ Section table describes the sections - > it's a table of contents
○ Usually at least two sections are present
The sections that are most commonly present in an executable are:
Executable Code Section , named .text (Microsoft) or CODE (Borland)
Data Sections , named .data , .rdata , or .bss (Microsoft) or DATA (Borland) - > usually if the code is packed, it si not in the
Executable Code Section, but it's in the Data Section
▪ Resources Section , named .rsrc
▪ Export Data Section , named .edata - > functionalities available for other codes
▪ Import Data Section , named .idata - > functionalities available from other codes
Debug Information Section , named .debug
□ Information used by programmers to understand how the code was compiled
□ They are deleted by the compilers, so when we do reverse engineering they are not available anymore
□ They are used for correct mapping between assembly and code, but typically they are not available for malware analysis
The names are actually irrelevant as they are ignored by the OS and are present only for the convenience of the programmer - > Means
you can find very different names (e.g. packers use their names)
DOS Header + Stub
○ The DOS header occupies the first 64 bytes of the file
It's there in case the program is run from DOS, so DOS can recognise it as a valid executable and run the DOS stub ○
○ The DOS stub usually just prints a string like "This program must be run under Microsoft Windows"
○ Example
offset where PE header is going to
start since DOS stub has not a fixed
dimension
Every file that starts
with MZ in Windows
is an executable file
File Execution in Windows
martedì 8 ottobre 2024 10:
○ Signature + File Header
○ Optional Headers
○ Section Table: One of these (40 bytes) for each section defined in the file
Sections
The size of all headers + section
table. You can also use this value
as the file offset of the first
section in the PE file
Overall size of the PE image
in memory. It's the sum of
all headers and sections
aligned to SectionAlignment.
says to which offset we can find the first section of the
file
There could be sections that are defined in file, but are
empty and they are declared to be larger in memory
since they are used to store data
Example: Services.exe vs Services.ex_ (malware)
Common dlls
Kernel32.dll: This is a very common DLL that contains core functionality, such as access and manipulation of memory, files and
hardware
▪ Advapi32.dll: This DLL provides access to advanced core Windows components such as the Service Manager and Registry
User32.dll: This DLL contains all the user-interface components, such as buttons, scrollbars, and components for controlling and
responding to user actions
▪ Gdi32.dll : This DLL contains functions for displying and manipulating graphics
Ntdll.dll:
□ This DLL is the intergface to the Windows kernel
□ Executables generally do not import this file directly, alothough it is always imported indirectly by Kernel32.dll
If an executable imports this file, it means that the author intended to use functionality not normally available to Windows
programs
□ Some tasks, such as hiding functionality or manipulating processes, will use this interface
Malware are programs that are executed on complex environment that have necessarily to interact with the operating
system to perform its activities, since the OS has a lot of barriers about what a program can actually do on a machine
WSock32.dll and Ws2_32.dll:
□ These are networking DLLs
□ A program that accesses either of these most likely connects to a network or performs network-related tasks
▪ Wininet.dll : This DLL contains higher-level networking functions that implement protocols such as FTP, HTTP and NTP
Examples:
Keylogger
□ Imports User32.dll and uses the function SetWindowsHookEx which is a popular way keyloggers receive keyboard inputs
□ It exports LowLevelKeyboardProc and LowLevelMouseProc to send the data elsewhere
□ It uses RegisterHotKey to define a special keystroke like Ctrl+Shift+P to harvest the collected data
A packed program
□ Very few functions
□ All you see is the unpacker
□ Virtual Size: RAM
□ Size of Raw Data: DISK
□ For .text section, normally equal, or nearly equal
Packed executables show Virtual Size much larger than Size of
Raw Data for .text section
means that the code is
not there, so the code
is compressed and it is
in another memory
location
▪ Imports and Exports
Exports
○ which functions in our program have to be used by another program, so need to be exported to be used by external programs
○ DLLs export functions
○ EXEs import functions
○ Both exports and imports are listed in the PE header
○ Exports should be rare in EXEs (but reality is sometimes different)
Time Date Stamp
○ Shows when this executable was compiled
○ Older programs are more likely to be known to antivirus software
But sometimes the date is wrong
All Delphi programs show June 19, 1992
▪ Date can also be faked
Resource Hacker
○ Lets you browse the .rsrc section
○ Strings, icons, and menus
Static analysis can reach a dead-end, due to
○ Obfuscation
○ Packing
○ Examiner has exhausted the available static analysis techniques
Approaches:
Diffing
▪ Take a snapshot of a clean system state and a snapshot of a compromised system state
Compare before and after
Used when we capture more or less all artifacts
▪ Pro: Artifacts can be observed easily
▪ Con: Can miss evidence that is created during malware activities and erased purposely by malware
▪ Tools : regshot, autoruns
System Monitoring
From a clean system state, record every individual change on system and network traffic that appear after execution of the
suspicious file
▪ gives information about what a single process does at execution (creation and elimination of the artifacts)
▪ Pro : Can collect all manifested changes
▪ Con: Often too much information and need to weed out irrelevant data
Tools: procmon, Wireshark
API Tracing
▪ Hook and record important API calls made by the suspicious process
is the dynamic counter part of looking on import table in static analysis - > means using a tool that injects in the code that we
want to monitor some hooks, so that when the program is executed and performs the API call, the call is traces and locked
Pros:
□ Provides visibility into activity beyond the typical file/process/registry/network shown by other tools
□ Gets you a little closer to the type of interpretation that is required when doing static analysis.
Cons:
□ Often too much of information and need to weed out irrelevant data
□ API-specific interpretation can take a lot of time, but still less than static analysis
Tools: WinApiOverride, Rohitab API Monitor
Debugging
▪ Set breakpoints inside the suspicious file to stop its execution at a given location and inspect its state
most powerful tool in the toolbox since we can see everything that the malware does and we can change the execution of
the malware at runtime - > used only in the part of the malware that we are not able to understand with simpler tools, since it
requires a lot of time to be performed
▪ Pro: Provides a superset of the functionality of an API monitor
Cons:
□ Typically must be be done in conjunction with some basic static analysis and assembly reading
□ Malware will often change its behavior or refuse to run when being debugged, which requires a work-around.
▪ Tools: IDA Pro Debugger, OllyDbg, Immunity Debugger, WinDbg
Limits you need to be aware of:
○ in general, single path (execution trace) is examined
analysis environment possibly not invisible ○
○ analysis environment possibly not comprehensive
○ scalability issues
○ Classification schemes are hardly coherent
Real Machines
Disadvantage s
▪ No Internet connection, so parts of the malware may not work
▪ Can be difficult to remove malware, so re-imaging the machine will be necessary
○ Advantage: Some malware detects virtual machines and won't run properly in one
Virtual Machines
○ The most common method
This protects the host machine from the malware
▪ VM-escape attacks are theoretically possible
▪ I don’t know about real malware implementing it
“naïf” VM usage is way more dangerous
○ You can easily “snapshot” the VM and revert back to a clean state at the end of each analysis job.
Oracle Virtual Box
Free
▪ Reads/Writes VMware disks
▪ Reasonable performance for the goals of this course
▪ You can take several snapshots and revert back to them if needed
Configuring Virtual Box
□ You can disable networking by disconnecting the virtual network adapter while the VM is running
Host-only networking allows network traffic to the host but not the Internet - > Disconnect the virtual cable, but not
deleting the network
More complex setup are possible
Use ad-hoc networking to fake external services - > create a vm
that simulates the network
host-only networking
Snapshots to:
▪ Protect a clean installation of the analysis environment
▪ Keep track of ongoing progresses during analysis
Hop instantly from job to job if you need to analyze several samples at the same time.
Risk of using a VM for Malware Analysis
▪ Malware may detect that it is in a VM and run differently
▪ Virtualization environments may have bugs: malware may crash or exploit it
▪ Malware may spread or affect the host (no real cases to report)
All the textbook samples are harmless
Launching DLLs
○ EXE files can be run directly, but DLLs can't
Use Rundll32.exe (included in Windows) - > rundll32.exe DLLname, Export arguments ○
○ The Export value is one of the exported functions you found in Dependency Walker, PEview, or PE Explorer.
○ Example: rip.dll has these exports: Install and Uninstall - > rundll32.exe rip.dll, Install
○ Some functions use ordinal values instead of names, like rundll32.exe xyzzy.dll, #
It's also possible to modify the PE header and convert a DLL into an EXE, or install it as a service: rundll32.exe ipr32x.dll,
InstallService ServiceName net start ServiceName
Process Monitor Tool
○ Monitors registry, file system, network, process, and thread activity
○ All recorded events are kept, but you can filter the display to make it easier to find items of interest
○ Don't run it too long or it will fill up all RAM and crash the machine
Filtering With Exclude
▪ One technique: hide normal activity before launching malware
▪ Right-click each Process Name and click Exclude
Most useful filters: Process Name, Operation, and Detail
○ Info on loaded DLLs is available following View - > Lower Pane View - > DLLs
Properties
○ Shows DEP and ASLR status
○ Verify button checks the disk file's Windows signature
○ But not the RAM image, so it won't detect process replacement
Detecting Malicious Documents
○ Open the document (e.g. PDF) on a system with a vulnerable application (e.g. an old version of Adobe Reader)
○ Watch Process Explorer to see if it launches a process
○ The Image tab of that process's Properties sheet will show where the malware is
Strings: Compare Image to Memory strings, if they are very
different, it can indicate process replacement
File System
Startup locations
□ For the logged-in user: %USERPROFILE%\Start Menu\Programs\Startup
□ For all users: %ALLUSERSPROFILE%\Start Menu\Programs\Startup
DLL search order hijacking
▪ Trojanizing system files
○ Master Boot Record (MBR)
Basic Input/Output System (BIOS) ○
Microsoft Windows Service
▪ Long-running executables without user interaction (like a *nix daemon)
Can be automatically started when the computer boots
▪ CreateService() Windows API is called to register a service
▪ Registered services can be found under the registry key HKLM\System\CurrentControlSet\Services
Svchost
○ C:\Windows\System32\svchost.exe is a generic host process for services that run from DLLs
○ Multiple instances are often running - > One instance contains a group of services
○ Groups are listed in the registry key HKLM\Software\Microsoft\Windows NT\CurrentVersion\Svchost
It is common to have malware name itself svchost.exe but run from somewhere other than C:\Windows\System32, e.g. C:
\Windows
○ Or alternatively they will just add a new DLL for the real svchost to run as a service
Using the tools
○ Procmon: Filter on the malware executable name and clear all events just before running it
○ Process Explorer
○ Regshot
Virtual Network with
ApateDNS (e.g. to redirect DNS Resolutions)
▪ InetSim
○ Monitoring with Ncat (included with Nmap)
○ Wireshark
Can save files from streams here too
□ Fools a Browser □ Fools Nmap
Loops
○ For
While
○ While