Post on 14-Jul-2015
Bypass AV in Python by y0nd13.
Quick introduction to Python
• By default exist in every major Linux Distribution
• Can be install or run as portable tools in Windows :
How interpreter language work.
Hello World in Python
Easy right!!
So what’s the big deal?
• Python support Foreign Function Instruction • It supports Ctypes. • http://docs.python.org/2/library/ctypes.html • It provides C compatible data types, and allows
calling functions in DLLs or shared libraries. It can be used to wrap these libraries in pure Python
• Smell profits!!! • Alternative ways besides using import system • Good for Post Exploitation • Bypass AV
Quick Introduction to Python FFI
A Simple MessageBoxA
• From MSDN
• Required 4 argument,
How to understand quickly
• HWND – A handle to the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window. (SO we set to Null, in Python Null is None)
• LPCTSR lpText - It’s a string for a Text
• LPCTSR lpCaption – It’s a string for the MessageBox Title
• UINT - Unsigned Integer .
_in_opt_ is a SAL Annotation saying you can put NULL as a value
SAL Annotation shortcut
Parameters are
required
Parameters are
optional
Input to called function _In_ _In_opt_
Input to called function, and output to
caller
_Inout_ _Inout_opt_
Output to caller _Out_ _Out_opt_
Output of pointer to caller _Outptr_ _Outptr_opt_
How easy to pop up a MessageBox in python?
• Simple
from ctypes import * ctypes.windll.user32.MessageBoxA(None,"Hello World","Title",None)
How to about WinExec?
• WinExec is a classical function since the age of Windows 16-bit . Only 2 Args are needed.
• From MSDN
• We know lpCmdLine is a string for the Exectuable path but what value should we place for uCmdShow?
uCmdShow from MSDN
• http://msdn.microsoft.com/en-us/library/windows/desktop/ms633548(v=vs.85).aspx
To Spawn a calc from ctypes import * ctypes.windll.kernel32.WinExec(“C:\Windows\system32\calc.exe”,1)
Get CurrentProcessID
How about Executing Shellcode?
• Many ways
– File Dropping Technique (BAD)
– Code Injection Technique(BAD)
– InMemory Technique (G000D)
• File Dropping Technique are bad , since antivirus/malware will immedietely catch it up and trigeger
• Code Injection , affects the integrity of a binary. HIPS might trigger alert.
• Why Shellcode? Becoz we can!!
InMemory Technique
• We are going to chain 4 API to execute our shellcode .
– >VirtualAlloc()
– >WriteProcessMemory()
– >CreateThread()
– >WaitForSingleObject()
VirtualAlloc()
• lpAddress = Null
• dwSize = length of shellcode can be use,
• flAllocation = MEM_COMMIT|MEM_RESERVED (0x3000)
• flProtect = PAGE_EXECUTE_READWRITE(0x40)
WriteProcessMemory()
• hProcess = -1 * we writing in the same process
• lpBaseAddress = A Pointer to address return from VirtualALloc()
• lpBuffer = A pointer to our buffer
• nSize = we can use shellcode size and times 2 to be safe
• lpNUmberofBytesWritten = Null it..
CreateThread()
• Everything is 0 except for (go figure it out yerself)
WaitForSingleObject()
• -1 , -1 !!!
P.O.C
• Inspired by SK Training.. Use \xcc !!!
Using OllyDBG
Attached with Olly
Executing native inside us heheheheh
2nd POC is our calc
(Optional) Freeze it to exe
• Using pyinstaller
Simple2
Exercise
• Create a Reverse Shell is a piece of cake!
Reference
• Understanding Win32Shellcode Skape:
• http://www.hick.org/code/skape/papers/win32-shellcode.pdf
• Advance Windows Shellcode, SK:
• http://www.phrack.org/issues.html?id=7&issue=62
• http://msdn.microsoft.com/en-US/