Anti-Unpacking techniques

Slides:



Advertisements
Similar presentations
Pokas x86 Emulator for Generic Unpacking By Amr Thabet
Advertisements

Chapter 13: I/O Systems I/O Hardware Application I/O Interface
Process Description and Control
Operating-System Structures
1 Copyright © 2013 Elsevier Inc. All rights reserved. Chapter 3 CPUs.
6 Copyright © 2005, Oracle. All rights reserved. Building Applications with Oracle JDeveloper 10g.
XP New Perspectives on Microsoft Office Word 2003 Tutorial 2 1 Microsoft Office Word 2003 Tutorial 2 – Editing and Formatting a Document.
So far Binary numbers Logic gates Digital circuits process data using gates – Half and full adder Data storage – Electronic memory – Magnetic memory –
1 Processes and Threads Creation and Termination States Usage Implementations.
Chapter 5 Input/Output 5.1 Principles of I/O hardware
Chapter 6 File Systems 6.1 Files 6.2 Directories
Configuration management
Mehdi Naghavi Spring 1386 Operating Systems Mehdi Naghavi Spring 1386.
Tanenbaum, Modern Operating Systems 3 e, (c) 2008 Prentice-Hall, Inc
Debugging operating systems with time-traveling virtual machines Sam King George Dunlap Peter Chen CoVirt Project, University of Michigan.
Boris Lau, Vanja Svajcer Sophoslabs, Journal in Computer Virology, 2008.
I/O Systems.
Project 5: Virtual Memory
Vanderbilt Business Objects Users Group 1 Reporting Techniques & Formatting Beginning & Advanced.
Page Replacement Algorithms
Module 10: Virtual Memory
Chapter 3 Memory Management
Chapter 10: Virtual Memory
Trap Diagnostic Facility Todays Software Diagnostic Tool with innovative features for the z/OS software developer Arney Computer Systems.
Chapter 6 File Systems 6.1 Files 6.2 Directories
Chapter 5 Microsoft Excel 2007 Window
Error-handling using exceptions
COMP1214 Systems & Platforms: Operating Systems Concepts Dr. Yvonne Howard – Rikki Prince – 1.
3.1 Silberschatz, Galvin and Gagne ©2009 Operating System Concepts – 8 th Edition Process An operating system executes a variety of programs: Batch system.
Processes Management.
Pointers and Arrays Chapter 12
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Installing Windows XP Professional Using Attended Installation Slide 1 of 30Session 8 Ver. 1.0 CompTIA A+ Certification: A Comprehensive Approach for all.
Attacks on Virtual Machine Emulators Peter Ferrie, Senior Principal Researcher 12 April, 2007.
Secure In-VM Monitoring Using Hardware Virtualization Monirul Sharif, Wenke Lee, Weidong Cui, and Andrea Lanzi Presented by Tyler Bletsch.
Computer Organization and Architecture
Operating System Security : David Phillips A Study of Windows Rootkits.
RIVERSIDE RESEARCH INSTITUTE Helikaon Linux Debugger: A Stealthy Custom Debugger For Linux Jason Raber, Team Lead - Reverse Engineer.
Computer Organization and Architecture
Computer Organization and Architecture
Disclaimer The Content, Demonstration, Source Code and Programs presented here is "AS IS" without any warranty or conditions.
OllyDbg Debuger.
1 CS503: Operating Systems Part 1: OS Interface Dongyan Xu Department of Computer Science Purdue University.
Trying to like a boss… REVERSE ENGINEERING. WHAT EVEN IS… REVERSE ENGINEERING?? Reverse engineering is the process of disassembling and analyzing a particular.
Practical Malware Analysis Ch 8: Debugging Rev
Disclaimer The Content, Demonstration, Source Code and Programs presented here is "AS IS" without any warranty or conditions.
CSC 501 Lecture 2: Processes. Process Process is a running program a program in execution an “instantiation” of a program Program is a bunch of instructions.
FINAL MPX DELIVERABLE Due when you schedule your interview and presentation.
Part 3: Advanced Dynamic Analysis Chapter 8: Debugging.
Windows PE files Infections and Heuristic Detection Nicolas BRULEZ / Digital River PACSEC '04.
Lecture 3 Process Concepts. What is a Process? A process is the dynamic execution context of an executing program. Several processes may run concurrently,
Processes and Virtual Memory
Lecture 5 Rootkits Hoglund/Butler (Chapters 1-3).
Lecture 11 Example Rootkit. Intel internship Intel CTG (Corporate Technology Group) –Advanced research & development –System integrity services using.
Lecture 8 Rootkits Hoglund/Butler (Chapter 7-8). Avoiding detection Two ways rootkits can avoid detection –Modify execution path of operating system to.
Copyright © Curt Hill More on Operating Systems Continuation of Introduction.
Lecture 10 Anti-debugger techniques. Anti-debuggers Making reverse-engineering and disassembly painful –Polymorphism –Encryption –Interrupt disabling.
Chapter 6 Limited Direct Execution Chien-Chung Shen CIS/UD
Chapter 2: Operating-System Structures
Live Phishing Attack Authentication Activity from a Foreign Address.
Mechanism: Limited Direct Execution
Anti-Unpacking techniques
Anti-Unpacking techniques
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
CSE 451: Operating Systems Autumn 2003 Lecture 10 Paging & TLBs
Malware and the Windows API
Presentation transcript:

Anti-Unpacking techniques Peter Ferrie Senior Anti-virus Researcher 1 May, 2008 1

Classes of unpackers Memory dumper Debugger Emulator Code-buffer W-X interceptor Peter Ferrie, Microsoft Corporation 2

Anti-dumping SizeOfImage Field in PEB When altered, prevents debugger from attaching Causes incorrect number of pages to be dumped Erasing the header Removes section information Not obvious how many pages should be dumped Used by Yoda’s Crypter Nanomites Introduced in Armadillo Replace branches with int 3 and encoded pointers Peter Ferrie, Microsoft Corporation 3

Anti-dumping (cont.) Stolen bytes Introduced in ASProtect Copy bytes elsewhere and execute from there Optionally destroy original bytes Guard pages Allows on-demand decryption or decompression Used by Armadillo (decryption) Used by Shrinker (decompression) Imports Table is often removed, encrypted or compressed Sometimes erased after first use Functions can be redirected to another location Peter Ferrie, Microsoft Corporation 4

Anti-dumping (cont.) Virtual machines Uses p-code instead of native code P-code can be polymorphic (Themida) P-code can contain anti-debugging (HyperUnpackMe2) Interpreter can be obfuscated (Virtual CPU) Peter Ferrie, Microsoft Corporation 5

Anti-debugging NtGlobalFlag Field in PEB Debugger typically causes certain flags to be set Very common method Very commonly incorrect (cmp instead of test) Very unreliable (several ways to set/clear flags) Used by ExeCryptor Heap flags Pointer is field in PEB Two sets of flags Also very unreliable (flags controllable like NtGlobalFlag) Peter Ferrie, Microsoft Corporation 6

Anti-debugging (cont.) Heap Tail markers appear if certain flag is set Debugger typically causes flag to be set Marker is constant and easy to find Used by Themida IsDebuggerPresent() Introduced in Windows 95 Reads field in PEB One of the most common methods Returns true if a debugger is present Peter Ferrie, Microsoft Corporation 7

Anti-debugging (cont.) CheckRemoteDebuggerPresent() Introduced in Windows XP SP1 Wrapper around NtQueryInformationProcess (ProcessDebugPort) Returns true if a debugger is present NtQueryInformationProcess() Can be called with ProcessDebugPort class Returns EPROCESS->DebugPort value Non-zero if a debugger is present Used by MSLRH Peter Ferrie, Microsoft Corporation 8

Anti-debugging (cont.) Debug objects Another use for NtQueryInformationProcess() Introduced in Windows XP Can be called with ProcessDebugObjectHandle class Returns non-zero if a debugger is present Used by HyperUnpackMe2 More debug objects Yet another use for NtQueryInformationProcess() Also introduced in Windows XP Can be called with ProcessDebugFlags class Returns inverse of EPROCESS->NoDebugInherit bit Returns zero if a debugger is present Peter Ferrie, Microsoft Corporation 9

Anti-debugging (cont.) Kernel debugger Final use for NtQueryInformationProcess() Can be called with SystemKernelDebuggerInformation class Not supported by any version of Windows(?) Returns non-zero in AH register if a debugger is present Used by SafeDisc NtQueryObject() Supported since Windows NT Extended in Windows XP to include debug object Request all objects Search for "DebugObject" Check associated handle count Non-zero if a debugger is present Peter Ferrie, Microsoft Corporation 10

Anti-debugging (cont.) Thread hiding NtSetInformationThread() method Introduced in Windows 2000 Can be called with HideThreadFromDebugger class Debugger receives no further events for that thread Used by HyperUnpackMe2 OpenProcess() Csrss.exe process can be opened If debug privilege is obtained first Even though it's a system process Process can be opened with full access rights Allows thread injection and system-wide DoS Peter Ferrie, Microsoft Corporation 11

Anti-debugging (cont.) CloseHandle() Different behaviour if debugger is present Kernel raises EXCEPTION_INVALID_HANDLE For invalid handle if debugger is present Exception can be intercepted by debugged process OutputDebugString() Kernel clears thread error code If debugger is present ReadFile() Indirect breakpoint removal Read data from file over location of breakpoint Peter Ferrie, Microsoft Corporation 12

Anti-debugging (cont.) WriteProcessMemory() Alternative breakpoint removal Write data from memory over location of breakpoint Used by NsAnti UnhandledExceptionFilter() Called as last resort if no other handlers succeed Not called if a debugger is present Exception is passed to debugger instead BlockInput() Blocks mouse and keyboard events Used by Yoda's Protector Peter Ferrie, Microsoft Corporation 13

Anti-debugging (cont.) SuspendThread() Can be used on remote processes Such as debuggers Used by Yoda's Protector Guard pages Certain debuggers might not support them properly Access should cause EXCEPTION_GUARD_PAGE OllyDbg uses guard pages for memory breakpoints Consumes breakpoint and allows execution Used by PC Guard Peter Ferrie, Microsoft Corporation 14

Anti-debugging (cont.) Alternative desktop Sessions support multiple desktops A new desktop can be created and made active No obvious way to switch back Used by HyperUnpackMe2 Prefetch queue Queue is not flushed when rep instruction is used Allows for some old self-overwriting tricks Two known variations of the trick One is used by Invius The other is used by Obsidium Peter Ferrie, Microsoft Corporation 15

Anti-debugging (cont.) Hardware breakpoints Exception handler receives context structure Structure contains all CPU debug registers Non-zero values suggest presence of debugger Handler can also remove breakpoints and resume Instruction counting Handler can insert own hardware breakpoints Then count number of exceptions that occur Debugger might skip some breakpoints Execution timing Single-stepping through code take a long time Delay can be detected Several time sources available Peter Ferrie, Microsoft Corporation 16

Anti-debugging (cont.) Exceptions Obfuscated method to move EIP around Infinite variety of possible implementations Header entrypoint Some debuggers don't use VirtualProtectEx() So header remains write-protected Cannot set initial breakpoint Code runs freely Parent process Should be shell process if launched by clicking Usually Explorer.exe, unless debugger is involved Used by Yoda's Protector Peter Ferrie, Microsoft Corporation 17

Anti-debugging (cont.) Self-execution Simple method to escape from debugger Synchronisation object prevents infinite executions Used by MSLRH Process name Process names can be enumerated Names can be compared to "watch list" List commonly includes anti-malware software Peter Ferrie, Microsoft Corporation 18

Anti-debugging (cont.) Threads Multiple threads are hard to trace Some anti-malware emulators don't support them One thread is often devoted to debugger detection Can also be used to detect code changes Used by PE-Crypt32 Self-debugging Effective anti-debugging method Only one debugger can attach to a process Defeated by zeroing EPROCESS->DebugPort Or call DebugActiveProcessStop() on Windows XP Used by Armadillo Peter Ferrie, Microsoft Corporation 19

Anti-debugging (cont.) Disassembly Can be used to steal instructions Safer method to detect breakpoints Can also be used to detect detours Problematic if routine is hot-patched Since that will be a detour TLS callback Relatively little-known method to execute code Code executes before main entrypoint Also executes before inserted breakpoint Debuggers typically don't handle it Used by ExeCryptor Peter Ferrie, Microsoft Corporation 20

Anti-debugging (cont.) Device names A way to communicate with drivers Also a way to detect presence of drivers List of names often target SoftICE, RegMon and FileMon SoftICE-specific Driver names can be enumerated Version information can be queried Product and copyright strings can be compared to "watch list" Harder to change than renaming the driver List of strings are usually SoftICE-related Peter Ferrie, Microsoft Corporation 21

Anti-debugging (cont.) SoftICE-specific (cont.) Interrupt 1 is normally not invokable from ring 3 SoftICE hooks interrupt 1 and allows ring 3 access So wrong exception when SoftICE is running Used by SafeDisc OllyDbg-specific Cannot handle unusual NumberOfRvaAndSizes value Some unchecked fields allow memory allocation DoS Initial ESI register value is -1 on Windows XP Looks like a detection method It's just a coincidence Peter Ferrie, Microsoft Corporation 22

Anti-debugging (cont.) OllyDbg-specific (cont.) Passes user-defined data directly to _vsprintf() Leads to DoS condition Debugger window can be found by calling FindWindow("OLLYDBG") HideDebugger-specific Plug-in for OllyDbg Detectable by far jump at OpenProcess()+6 ImmunityDebugger-specific Based on OllyDbg Shares many of the same vulnerabilities WinDbg-specific Debugger window can be found by calling FindWindow("WinDbgFrameClass") Peter Ferrie, Microsoft Corporation 23

Anti-emulating Interrupt 3 Size of instruction is not checked Using "cd 03" instead of "cc" instruction -> EIP points to "03" Prefixed "cc" instruction works, too Except perhaps in some emulators Used by TryGames Time-locks Effective anti-emulation technique Causes all iterations to be consumed before anything happens Some loops can be skipped arbitrarily Some loops cannot (results might be used for decryption keys) Used by Tibs Peter Ferrie, Microsoft Corporation 24

Anti-emulating (cont.) Invalid API parameters Some emulators do not support invalid parameters in APIs An expected error code can be used for decryption keys No error code means no decryption Used by Tibs GetProcAddress There are too many possible APIs for emulators to handle So some emulators always return a value for GetProcAddress() Sometimes GetProcAddress() should return 0 A non-zero value reveals the emulator Used by NsAnti Peter Ferrie, Microsoft Corporation 25

Anti-emulating (cont.) "Modern" CPU instructions Some emulators do not handle modern CPU instructions Some malware use FPU and MMX as do-nothing instructions Emulators might give up early as a result Used by NsAnti Undocumented instructions Some emulators do not handle undocumented CPU instructions Some instructions more undocumented than others Example: multi-byte NOP Example: fully allocated PREFETCH Peter Ferrie, Microsoft Corporation 26

Anti-emulating (cont.) Selector verification Some emulators pretend to be Windows NT-based While using Windows 9x-based selector values A CS selector value larger than 0xff reveals this Used by MSLRH Memory layout Certain in-memory structures are at predictable memory locations Example: RTL_USER_PROCESS_PARAMETERS at 0x20000 If they are missing, the program might not run Used by TryGames Peter Ferrie, Microsoft Corporation 27

Anti-emulating (cont.) File-format tricks Non-aligned SizeOfImage Windows will silently round up the value Overlapping structures Tools such as IDA have a problem with this Non-standard NumberOfRvaAndSizes SoftICE and OllyDbg have a problem with this Non-aligned SizeOfRawData Non-aligned PointerToRawData Windows will silently round down the value No section table Allowed when SectionAlignment is less than 4kb Header becomes writable and executable Peter Ferrie, Microsoft Corporation 28

Anti-intercepting Write->Exec Some interceptors watch for write-then-exec Executing dummy just-written instruction can fool them Used by ASPack, but probably for multi-processor support Write^Exec Some interceptors prevent write and exec on the same page Change can be detected indirectly Kernel functions return error when writing to read-only pages VirtualQuery() and VirtualProtect() return old page attributes Peter Ferrie, Microsoft Corporation 29

Thank you More techniques in the paper http://pferrie.tripod.com Questions? Peter Ferrie, Microsoft Corporation 30