Common technique in Bypassing Stuff in Python.

29
Bypass AV in Python by y0nd13.

Transcript of Common technique in Bypassing Stuff in Python.

Page 1: Common technique in Bypassing Stuff in Python.

Bypass AV in Python by y0nd13.

Page 2: Common technique in Bypassing Stuff in Python.

Quick introduction to Python

• By default exist in every major Linux Distribution

• Can be install or run as portable tools in Windows :

Page 3: Common technique in Bypassing Stuff in Python.

How interpreter language work.

Page 4: Common technique in Bypassing Stuff in Python.

Hello World in Python

Easy right!!

Page 5: Common technique in Bypassing Stuff in Python.

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

Page 6: Common technique in Bypassing Stuff in Python.

Quick Introduction to Python FFI

Page 7: Common technique in Bypassing Stuff in Python.

A Simple MessageBoxA

• From MSDN

• Required 4 argument,

Page 8: Common technique in Bypassing Stuff in Python.

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

Page 9: Common technique in Bypassing Stuff in Python.

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_

Page 10: Common technique in Bypassing Stuff in Python.

How easy to pop up a MessageBox in python?

• Simple

from ctypes import * ctypes.windll.user32.MessageBoxA(None,"Hello World","Title",None)

Page 11: Common technique in Bypassing Stuff in Python.

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?

Page 13: Common technique in Bypassing Stuff in Python.

To Spawn a calc from ctypes import * ctypes.windll.kernel32.WinExec(“C:\Windows\system32\calc.exe”,1)

Page 14: Common technique in Bypassing Stuff in Python.

Get CurrentProcessID

Page 15: Common technique in Bypassing Stuff in Python.

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!!

Page 16: Common technique in Bypassing Stuff in Python.

InMemory Technique

• We are going to chain 4 API to execute our shellcode .

– >VirtualAlloc()

– >WriteProcessMemory()

– >CreateThread()

– >WaitForSingleObject()

Page 17: Common technique in Bypassing Stuff in Python.

VirtualAlloc()

• lpAddress = Null

• dwSize = length of shellcode can be use,

• flAllocation = MEM_COMMIT|MEM_RESERVED (0x3000)

• flProtect = PAGE_EXECUTE_READWRITE(0x40)

Page 18: Common technique in Bypassing Stuff in Python.

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..

Page 19: Common technique in Bypassing Stuff in Python.

CreateThread()

• Everything is 0 except for (go figure it out yerself)

Page 20: Common technique in Bypassing Stuff in Python.

WaitForSingleObject()

• -1 , -1 !!!

Page 21: Common technique in Bypassing Stuff in Python.

P.O.C

• Inspired by SK Training.. Use \xcc !!!

Page 22: Common technique in Bypassing Stuff in Python.

Using OllyDBG

Attached with Olly

Page 23: Common technique in Bypassing Stuff in Python.

Executing native inside us heheheheh

Page 24: Common technique in Bypassing Stuff in Python.

2nd POC is our calc

Page 25: Common technique in Bypassing Stuff in Python.
Page 26: Common technique in Bypassing Stuff in Python.

(Optional) Freeze it to exe

• Using pyinstaller

Page 27: Common technique in Bypassing Stuff in Python.

Simple2

Page 28: Common technique in Bypassing Stuff in Python.

Exercise

• Create a Reverse Shell is a piece of cake!

Page 29: Common technique in Bypassing Stuff in Python.

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/