39
Challenges and Solutions of Window Remote Shellcode @若渴 2017.11.19 <[email protected]> AjMaChInE

[若渴計畫] Challenges and Solutions of Window Remote Shellcode

  • Upload
    aj0612

  • View
    145

  • Download
    6

Embed Size (px)

Citation preview

Page 1: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Challenges and Solutions of Window Remote Shellcode @若渴

2017.11.19 <[email protected]>

AjMaChInE

Page 2: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Outline

• Overview of window remote shellcode

• Some challenges and solutions

– Antivirus

– EMET

– Firewall

– Intrusion-Detection System (IDS)/ Intrusion-Prevention System (IPS)

• Reference

Page 3: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Remote Shellcode [0][1]

pipe protocol process terminal process

command

sh

Page 4: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Windows Shellcode Skeleton in Assembly [12]

• Getting EIP

• Decoder

• Getting addresses of required functions

• Setup socket

• Spawning a shell

Page 5: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Getting EIP – Why [12][13]

• What is the problem with such a hardcoded address?

Compiler

move that code to another address space

????

Page 6: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Getting EIP [12]

Page 7: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Getting Addresses of Required Functions [2]

Finding kernel32.dll

LoadLibraryA

GetProcAddress

System calls

not reliable

Page 8: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

How to Finding kernel32.dll in ASLR? [2][3][4]

Process Environment Block(PEB)

fs:[0x30]

Structured Exception Handling(SEH)

fs:[0]

Thread Environment Block +0x4 TOPSTACK

fs:[0x18] not reliable

Page 9: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Finding kernel32.dll- PEB

“The process of determining the kernel32.dll base address involves making use of the Process Environment Block (PEB). The operating system allocates a structure for every running process that can always be found at fs:[0x30] from within the process. The PEB structure holds information about the process’ heaps, binary image information, and, most importantly, three linked lists regarding loaded modules that have been mapped into process space. The linked lists themselves differ in purposes from showing the order in which the modules were loaded to the order in which the modules were initialized. The initialization order linked list is of most interest as the order in which kernel32.dll is initialized is always constant as the second module to be initialized.” [2]

Page 10: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Finding kernel32.dll- SEH

“Windows NT based versions the top-most entry in the SEH list can always be found at fs:[0] from within the process. With this in mind, one can walk the list of installed exception handlers until they reach the last one. When the last one is reached the address of the function pointer can be used as a starting point for walking down in increments of 64KB, or 16 × 4096 byte pages. In Windows, DLL’s will only align on 64KB boundaries. At each 64KB boundary a check can be performed to see if the two characters at that point are ‘MZ’. These two characters mark the MSDOS header that is prepended to portable executables.” [2]

Page 11: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Address Resolution of Required Functions

• PEB parsing [5]

– This method uses the Process Environment Block(PEB) data structure to locate the base addresses of loaded DLLs and finding their function addresses with parsing the Export Address Table(EAT)

• Hash API search [5]

– For quickly finding required functions

EAT

IAT

DLL Required Functions

hash hash ?=

Page 12: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Functions You maybe Want to [6]

• WinExec • CreateProcessW • CreateProcessA • LoadLibraryExA • LoadLibraryExW • OpenFile • CreateThread • CreateRemoteThread • GetProcAddress • LoadModule • CreateFileA • CreateFileW • _lopen • _lcreat

• CopyFileA • CopyFileW • CopyFileExA • CopyFileExW • MoveFileA • MoveFileExW • LockFile • GetModuleHandleA • VirtualProtect • OpenProcess • GetModuleHandleW • MoveFileWithProgressA • MoveFileWithProgressW • DeleteFileA

Page 13: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Challenges of Shellcode for Antivirus [7][8]

• Static signature analysis – Signature analysis is based on a blacklist method – EX: YARA [9]

• Static heuristic analysis – In this case the AV will check the code for patterns which are known

to be found in malwares. There are a lot of possible rules, which depends on the vendor

• Dynamic analysis – These days most AV will rely on a dynamic approach. When an

executable is scanned, it is launched in a virtual environment for a short amount of time. Combining this with signature verification and heuristic analysis allows detecting unknown malwares even those relying on encryption. Indeed, the code is self-decrypted in AV sandbox; then, analysis of the “new code” can trigger some suspicious behavior.

Page 14: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Bypassing Challenges of Shellcode for Antivirus [7][8]

• Bypassing static signature analysis/ static heuristic analysis

– Decryption [10][11]

– Obfuscation [7]

– Non-standard languages for windows binaries [25]

• Bypassing dynamic analysis

Page 15: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Obfuscation

Page 16: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

The Veil-Framework [25]

• Obfuscated code

• Encrypted code

• Non-standard languages for windows binaries

– Python, Ruby, Perl, Go, etc.

Page 17: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Bypassing Dynamic Analysis [7][8] • Allocate and fill 100M memory

• Hundred million increments

• Attempt to open a system process

• Attempt to open a non-existing URL

• Action which depends on local username

• What the fuck is NUMA?

• What the fuck are FLS?

• Check process memory

• Time distortion

• What is my name?

• I am my own father

• First open a mutex

• Load fake library

• Is debugger present

• Number of Cores

• Trap flag manipulation

Page 18: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Bypassing Dynamic Analysis - Hundred Million Increments [8]

AV detection

emulator

Page 19: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Proper Ways To Execute Shellcodes [7][8]

• HeapCreate/HeapAlloc

• LoadLibrary/GetProcAddress

• GetModuleHandle/GetProcAddress

• Multi-Threading

Page 20: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Challenges of Shellcode for EMET

• Preventing EAT parsing techniques

Page 21: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Bypassing Challenges of Shellcode for EMET

• IAT parsing [23]

– Also holding the WIN API function addresses by the application

Page 22: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Challenges of Shellcode for Firewalls

• Inbound detection

• Outbound detection

• Usually, firewall allow connection to popular services like port 25(SMTP), 53(DNS), 80(HTTP), etc.

Page 23: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Bypassing Challenges of Shellcode for Firewalls

• Bypassing inbound detection

– Reverse remote shellcode

• Bypassing outbound detection (進去了要怎出來),EX [12] :

Page 24: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Bypassing Outbound Detection

• DLL/PE Injection to iexplore.exe, telnet, ftp, SSH and alike [13]

• One-way shellcode [2][12]

• Meterpreter HTTP, HTTPS and DNS stagers [21]

Page 25: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

DLL Injection Overview – Step 1/2 [17]

Page 26: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

DLL Injection Overview – Step 3/4 [17]

Page 27: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Execution Methods of DLL Injection [15]

• CreateRemoteThread()

• NtCreateThreadEx()

• QueueUserAPC()

• SetWindowsHookEx()

• RtlCreateUserThread()

• Code cave via SetThreadContext()

• Reflective DLL

Page 28: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

DLL/PE Injection to iexplore.exe [13]

• Querying the register key, rather than referring to “c:\...\iexplore.exe”

• CreateProcess() to open and keep browser windows hidden

• WaitForInputIdle() to give processes time for initialization

• WaitProcessMemory() to copy networking code • CreateRemoteThread() to run code • The injected procedure connects the web site and

sends HTTP request

Page 29: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Reflective DLL Injection [16][18]

Reflective DLL (= DLL-format PE file loader)

reflective DLL

is loaded by reflective DLL

Page 30: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

DLL/PE/Process Hollowing Injection[19][20]

Page 31: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

One-way Shellcode – Find Socket [12]

(using anonymous pipe)

Page 32: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

One-way Shellcode – Reuse Socket [12]

The problem of the “Find Socket” method:

• If the socket already been closed

(the SO_REUSEADDR socket option)

Page 33: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

One-way Shellcode – Rebind Socket [12]

The problem of the “Rebind Socket” method:

• using SO_EXCLUSIVEADDRUSE, thus reusing the address is not possible

Page 34: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

The Meterpreter: a stager, and and stage [21][22][24]

Page 35: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

An Up-to-Standards Secure Corporate Environment with the meterpreter/reverse_winhttp Payload [14][22]

Page 36: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Bypassing An Up-to-Standards Secure Corporate Environment with the meterpreter/reverse_winhttp

Payload [14][22]

Thread 1 Thread 2 local proxy with port 8080

reverse_winhttp LHOST=127.0.0.1 LPORT=8080

NTLM authentication + HTTP requests

trust local proxy and go through the corporate proxy

Page 37: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

Reference • [0] How To Make A Reverse TCP Backdoor In Python - Part 1

– https://0x00sec.org/t/how-to-make-a-reverse-tcp-backdoor-in-python-part-1/1038

• [1] How To Make A Reverse TCP Backdoor In Python - Part 2

– https://0x00sec.org/t/how-to-make-a-reverse-tcp-backdoor-in-python-part-2/1040

• [2] Understanding Windows Shellcode

– http://www.hick.org/code/skape/papers/win32-shellcode.pdf

• [3] Windows Reverse Shell Shellcode I.

– http://sh3llc0d3r.com/windows-reverse-shell-shellcode-i/

• [4] Windows Reverse Shell Shellcode II.

– http://sh3llc0d3r.com/windows-reverse-shell-shellcode-ii/

• [5] Art of Anti Detection 3 – Shellcode Alchemy

– https://pentest.blog/art-of-anti-detection-3-shellcode-alchemy/

• [6] NT shellcodes prevrntion Demystified

– http://www.phrack.org/issues/63/15.html#article

• [7] Art of Anti Detection – 1 Introduction to AV and Detection Techniques

– https://www.exploit-db.com/docs/40900.pdf

• [8] Bypass Antivirus Dynamic Analysis - Limitations of the AV Model and How to Exploit Them

– https://wikileaks.org/ciav7p1/cms/files/BypassAVDynamics.pdf

• [9] YARA

– http://virustotal.github.io/yara/

Page 38: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

• [10] Code Segment Encryption

– http://blog.sevagas.com/?Code-segment-encryption

• [11] Hide Meterpreter Shellcode in Executable

– http://blog.sevagas.com/Hide-meterpreter-shellcode-in-executable

• [12] History and Advances in Windows Shellcode

– http://phrack.org/issues/62/7.html

– https://www.blackhat.com/presentations/bh-asia-03/bh-asia-03-chong.pdf

• [13] Using Process Infection to Bypass Windows Software Firewalls

– http://phrack.org/issues/62/7.html

• [14] Evade Egress Restrictions with Staged Payloads

– https://blog.cobaltstrike.com/2013/11/15/evade-egress-restrictions-with-staged-payloads/

• [15] Inject All the Things

– http://blog.deniable.org/blog/2017/07/16/inject-all-the-things/

– https://github.com/fdiskyou/injectAllTheThings/

– Microsoft Visual Studio Express 2013 for Windows Desktop

• [16] Reflective DLL Injection

– https://www.dc414.org/wp-content/uploads/2011/01/242.pdf

– https://github.com/stephenfewer/ReflectiveDLLInjection

• [17] Windows DLL Injection Basics

– http://blog.opensecurityresearch.com/2013/01/windows-dll-injection-basics.html

Page 39: [若渴計畫] Challenges and Solutions of Window Remote Shellcode

• [18] DOUBLEPULSAR Usermode Analysis: Generic Reflective DLL Loader

– https://countercept.com/our-thinking/doublepulsar-usermode-analysis-generic-reflective-dll-loader/

• [19] Ten Process Injection Techniques: A Technical Survey Of Common And Trending Process Injection Techniques

– https://www.endgame.com/blog/technical-blog/ten-process-injection-techniques-technical-survey-common-and-trending-process

– https://github.com/secrary/InjectProc

• [20] Process Hollowing

– https://github.com/m0n0ph1/Process-Hollowing

• [21] Metasploit - The Exploit Learning Tree

– https://www.exploit-db.com/docs/27935.pdf

• [22] Meterpreter Stage AV/IDS Evasion with Powershell

– https://arno0x0x.wordpress.com/2016/04/13/meterpreter-av-ids-evasion-powershell/

– https://github.com/Arno0x/PowerShellScripts/blob/master/proxyMeterpreterHideout.ps1

• [23] Teaching Old Shellcode New Tricks

– https://recon.cx/2018/brussels/resources/slides/RECON-BRX-2017-Teaching_Old_Shellcode_New_Tricks.pdf

– https://github.com/secretsquirrel/fido

• [24] Deep Dive Into Stageless Meterpreter Payloads

– https://blog.rapid7.com/2015/03/25/stageless-meterpreter-payloads/

• [25] The Art of AV Evasion - or Lack Thereof

– https://www.slideshare.net/CTruncer/the-art-of-av-evasion-or-lack-thereof

– https://github.com/Veil-Framework/Veil