SlideShare une entreprise Scribd logo
1  sur  32
Will
@harmj0y
Veris Group
Adaptive Threat Division
$ whoami
 Security researcher and penetration tester/red teamer
for Veris Group’s Adaptive Threat Division
 Co-founder of the Veil-Framework #avlol
 www.veil-framework.com
 Shmoocon ‘14: AV Evasion with the Veil Framework
 co-wrote Veil-Evasion, wrote Veil-Catapult and Veil-
PowerView
 BSides ATX ‘14: Wielding a Cortana
 Defcon ‘14 (accepted): Post-Exploitation 2.0
tl;dr
 Why we use Pyinstaller
 DEP, Pyinstaller, and a weird Veil-Evasion bug
 How Pyinstaller works
 Pwnstaller v1.0
 Questions
Caveat
 This is a proof of concept based off of an idea
 Going to detail through the problem that prompted
thinking about this, and walk through the thought
process that led to the PoC solution
 Probably a better way to do this, but seemed like an
interesting concept and wanted to get the idea out there
Pyinstaller 101
 Pyinstaller is “a program that converts (packages) Python
programs into stand-alone executables”
 http://www.pyinstaller.org/
 Packages Python scripts into OSX, Linux, or Windows
self-extracting executables
 Lets developers distribute projects without relying on an
existing Python installation
Pyinstaller Repurposed
 Pentesters realized a few years ago that we could use it
to package malicious scripts
 advantageous, as legitimate projects use Pyinstaller
 www.pyinstaller.org/wiki/ProjectsUsingPyInstaller
 Dave Kennedy’s “PyInjector” was released in 2012 based
on Debasish Mandal’s original post:
 https://www.trustedsec.com/august-2012/new-tool-
pyinjector-released-python-shellcode-injection/
 http://www.debasish.in/2012_04_01_archive.html
Pyinstaller in Veil-
Evasion
 Veil-Evasion sets up Pyinstaller under Wine so Python
payloads can be compliled natively to Windows .exe’s
 Generation is transparent to the user
 Allows for the dynamic generation of Windows Python
payloads, all on Kali!
 We always want to preserve a single attack platform
Veil Payloads and DEP
 Void pointer casting for shellcode injection may fail, as
the memory location used is not explicitly marked X
(*(void(*)()) shellcode)();
 Most systems tend to default to an opt-in DEP
enforcement policy
 if the executable you're running opts-in, void pointer
casting will fail with a memory access violation
A Weird Veil Bug
 Python void pointer payloads worked as .py files, but
failed as Pyinstaller executables
 The python.exe interpreter used by Pyinstaller is not
DEP enabled, but the resulting Pyinstaller payloads do
in fact opt in to this protection
see http://www.veil-evasion.com/dep-pyinstaller/
How Pyinstaller Works
 Pyinstaller uses the CArchive data structure to package
up the main python .dll, any necessary libraries, and
your target script
 Basically like a compressed ZIP container
 This CArchive is attached to then end of a “launcher”
executable
 We use the runw.exe version so we can hide the window,
making execution transparent to the user
Pyinstaller
.exe’s
http://www.pyinstaller.org/expor
t/develop/project/doc/Manua
l.html#two-pass-execution
How Pyinstaller Works
 On execution, the launcher executable:
 Decompresses the CArchive to a temporary location
 Loads the python15.dll using LoadLibraryExA
 Maps all the entry points in the python .dll for necessary
methods
 Sets up env stuff and starts the Python process
 Imports all specified necessary modules
 Runs the extracted script using PyRun_SimpleString
How Pyinstaller Works:
English
 When the Pyinstaller produced executable is run, a
minimal Python environment is extracted from a
compressed attachment
 Components necessary for the environment are
registered and set up
 The script attached is run
 Lets you run Python scripts without Python being
installed on a target machine!
Solving The Veil Bug
 So the DEP opt-in policy is determined by the launcher
.exe, not the Python interpreter
 Our next step was to generate a Pyinstaller launcher that
didn’t opt-in to DEP
 Luckily Pyinstaller is open source
 https://www.veil-framework.com/dep-pyinstaller/
Solving The Veil Bug
 Pyinstaller holds precompiled copies of 32-bit and 64-bit
loaders for Linux, OSX and Windows in
pyinstaller/support/loader/*
 The sources for the loaders are included in
pyinstaller/source/*
 runw.exe is the loader we want to regenerate
 used for “windowed” executables
DEP
Turning Off DEP
 The binaries utilize the WAF build system to build the
loaders
 ./pyinstaller/source/wscript
 add conf.env.append_value('LINKFLAGS',
'/NXCOMPAT:NO') right after the other flags on lines 209
and 211
 This will instruct the Visual Studio linker to turn off DEP
compatibility
Problem?
 Sweet, we have a shiny new launcher.exe
 But our project is focused on evading AV
 Including a static, custom-compiled launcher executable
is a GREAT way to say “Hey vendors, check out this
Veil-Evasion payload! Signatures lolz”
Solution
 Besides running Pyinstaller itself natively on Kali, we
can dynamically recompile the Pyinstaller launcher on
using mingw!
 This makes it trivial to makes some small changes and
get a different SHA1 signature each time
 Why don’t we make it *a little* harder to flag on?
Obfuscation: Phase 1
 There are only a handful of source files needed to
recompile runw.exe
 utils.c - some helper methods (246 lines)
 launch.c - “where the magic happens” (1617 lines)
 main.c - invokes launch.c (165 lines)
 ./zlib/* - extract of zlib v1.2.3
 Lets start with some basic obfuscation
Obfuscation: Phase 1
 The initial goal: make ssdeep as useless as possible
against “families” of our generated launcher
 Any unnecessary code was stripped out (i.e. code for
OSX and Linux binaries)
 Thought process: randomize/shuffle wherever we can
 A selection of random libraries imports thrown in
Obfuscation: Phase 2
 Let’s go just a bit further and have a some fun with
anything doing basic dynamic analysis
 How about interspersing lots of nested processing
methods throughout the code
 similar to our c/meterpreter/* payloads
 This mucks up the call tree of the program without
altering the actual execution
Finishing Touches
 The Pyinstaller icon is kind
of recognizable
 How about some randomized .ico’s instead?
Putting It All Together
 The end result, every time the generator runs:
 obfuscated code for all* source files associated with the
Pyinstaller launcher are generated
 a randomized icon is chosen for the final packaged result
 mingw32 is used to compile everything into a new
runw.exe, all on Kali
 the new runw.exe is copied into the correct resource
location to be used by Pyinstaller
*except some known zlib libraries
ssdeep comparison
 ssdeep is a ‘fuzzy hashing’ static malware comparison tool,
allowing for the comparison of malware families
 Generated a run of 1000 runw.exe loaders
 (1000 choose 2) = 499500 possible comparison combinations
 367,073 pairings (74%) scored 30/100 or better
 228,961 pairings (46%) scored 50/100 or better
 34,420 pairings (7%) scored 70/100 or better
 0 pairings scored at 90/100 or better
 What this means: none of the loader pairings scored as a
closely ‘similar’ malware family
ssdeep comparison
0
5000
10000
15000
20000
25000
30000
35000
40000
45000
0 10 20 30 40 50 60 70 80 90 100
occurances
match %
ssdeep matches
In Plain English
 Each generated Pyinstaller loader is reasonably unique
from a basic static malware analysis perspective
 Competent reversers will be able to figure out what’s
going on in very little time
 But hopefully this is relatively resistant against static
signatures
 I’m sure there are better obfuscation methods, so go
implement them!
Pwnstaller v1.0
 http://www.harmj0y.net/blog/python/pwnstaller-1-
0/
 The code is up on github:
https://github.com/HarmJ0y/Pwnstaller
 And it’s been integrated into Veil-Evasion
 In the development branch now, hitting the master branch
on the 5/15/2014 V-Day
 All Python payloads can now utilize a dynamically
generated Pwnstaller loader by choosing “2 - Pwnstaller”
from the Python compilation menu
Pwnstaller in Veil-
Evasion
Recap
 Pyinstaller is some cool stuff
 Pwnstaller will hopefully extend the lifetime of Veil-
Evasion Python payloads by making static signatures
reasonably difficult to write
 “This is script-kiddie garbage that will harm users of
Pyinstaller when AVs flag it without benefiting
anyone who matters. Hope you get booed off at
Bsides.” – The Internet
Shameless Sidebar
 Want to research cool stuff like this?
 Want to work with 9 x OSCPs and 4 x OSCEs?
 Want to do some sweet red teaming?
 Hit me up to join the Adaptive Threat Division
Questions?
 Contact me:
 @harmj0y
 will@harmj0y.net
 Read more:
 http://www.harmj0y.net/blog/python/pwnstaller-1-0/
 Get Pwnstaller:
 https://github.com/HarmJ0y/Pwnstaller
 Now in Veil-Evasion!

Contenu connexe

Tendances

Power on, Powershell
Power on, PowershellPower on, Powershell
Power on, Powershell
Roo7break
 
Mitigating Exploits Using Apple's Endpoint Security
Mitigating Exploits Using Apple's Endpoint SecurityMitigating Exploits Using Apple's Endpoint Security
Mitigating Exploits Using Apple's Endpoint Security
Csaba Fitzl
 
Attacking Oracle with the Metasploit Framework
Attacking Oracle with the Metasploit FrameworkAttacking Oracle with the Metasploit Framework
Attacking Oracle with the Metasploit Framework
Chris Gates
 

Tendances (20)

PowerUp - Automating Windows Privilege Escalation
PowerUp - Automating Windows Privilege EscalationPowerUp - Automating Windows Privilege Escalation
PowerUp - Automating Windows Privilege Escalation
 
PowerShell for Cyber Warriors - Bsides Knoxville 2016
PowerShell for Cyber Warriors - Bsides Knoxville 2016PowerShell for Cyber Warriors - Bsides Knoxville 2016
PowerShell for Cyber Warriors - Bsides Knoxville 2016
 
Continuous intrusion: Why CI tools are an attacker’s best friends
Continuous intrusion: Why CI tools are an attacker’s best friendsContinuous intrusion: Why CI tools are an attacker’s best friends
Continuous intrusion: Why CI tools are an attacker’s best friends
 
Get-Help: An intro to PowerShell and how to Use it for Evil
Get-Help: An intro to PowerShell and how to Use it for EvilGet-Help: An intro to PowerShell and how to Use it for Evil
Get-Help: An intro to PowerShell and how to Use it for Evil
 
PowerShell for Penetration Testers
PowerShell for Penetration TestersPowerShell for Penetration Testers
PowerShell for Penetration Testers
 
Defending Your "Gold"
Defending Your "Gold"Defending Your "Gold"
Defending Your "Gold"
 
Defcon - Veil-Pillage
Defcon - Veil-PillageDefcon - Veil-Pillage
Defcon - Veil-Pillage
 
Power on, Powershell
Power on, PowershellPower on, Powershell
Power on, Powershell
 
Obfuscating The Empire
Obfuscating The EmpireObfuscating The Empire
Obfuscating The Empire
 
Invoke-Obfuscation nullcon 2017
Invoke-Obfuscation nullcon 2017Invoke-Obfuscation nullcon 2017
Invoke-Obfuscation nullcon 2017
 
BH Arsenal '14 TurboTalk: The Veil-framework
BH Arsenal '14 TurboTalk: The Veil-frameworkBH Arsenal '14 TurboTalk: The Veil-framework
BH Arsenal '14 TurboTalk: The Veil-framework
 
Pwning with powershell
Pwning with powershellPwning with powershell
Pwning with powershell
 
Teensy Programming for Everyone
Teensy Programming for EveryoneTeensy Programming for Everyone
Teensy Programming for Everyone
 
Windows Attacks AT is the new black
Windows Attacks   AT is the new blackWindows Attacks   AT is the new black
Windows Attacks AT is the new black
 
Veil-PowerView - NovaHackers
Veil-PowerView - NovaHackersVeil-PowerView - NovaHackers
Veil-PowerView - NovaHackers
 
Workshop: PowerShell for Penetration Testers
Workshop: PowerShell for Penetration TestersWorkshop: PowerShell for Penetration Testers
Workshop: PowerShell for Penetration Testers
 
A Case Study in Attacking KeePass
A Case Study in Attacking KeePassA Case Study in Attacking KeePass
A Case Study in Attacking KeePass
 
Mitigating Exploits Using Apple's Endpoint Security
Mitigating Exploits Using Apple's Endpoint SecurityMitigating Exploits Using Apple's Endpoint Security
Mitigating Exploits Using Apple's Endpoint Security
 
The Dirty Little Secrets They Didn’t Teach You In Pentesting Class
The Dirty Little Secrets They Didn’t Teach You In Pentesting ClassThe Dirty Little Secrets They Didn’t Teach You In Pentesting Class
The Dirty Little Secrets They Didn’t Teach You In Pentesting Class
 
Attacking Oracle with the Metasploit Framework
Attacking Oracle with the Metasploit FrameworkAttacking Oracle with the Metasploit Framework
Attacking Oracle with the Metasploit Framework
 

En vedette

En vedette (13)

Trusts You Might Have Missed - 44con
Trusts You Might Have Missed - 44conTrusts You Might Have Missed - 44con
Trusts You Might Have Missed - 44con
 
I Have the Power(View)
I Have the Power(View)I Have the Power(View)
I Have the Power(View)
 
Derbycon - Passing the Torch
Derbycon - Passing the TorchDerbycon - Passing the Torch
Derbycon - Passing the Torch
 
Bridging the Gap
Bridging the GapBridging the Gap
Bridging the Gap
 
I hunt sys admins 2.0
I hunt sys admins 2.0I hunt sys admins 2.0
I hunt sys admins 2.0
 
PSConfEU - Offensive Active Directory (With PowerShell!)
PSConfEU - Offensive Active Directory (With PowerShell!)PSConfEU - Offensive Active Directory (With PowerShell!)
PSConfEU - Offensive Active Directory (With PowerShell!)
 
Building an EmPyre with Python
Building an EmPyre with PythonBuilding an EmPyre with Python
Building an EmPyre with Python
 
The Travelling Pentester: Diaries of the Shortest Path to Compromise
The Travelling Pentester: Diaries of the Shortest Path to CompromiseThe Travelling Pentester: Diaries of the Shortest Path to Compromise
The Travelling Pentester: Diaries of the Shortest Path to Compromise
 
Trusts You Might Have Missed
Trusts You Might Have MissedTrusts You Might Have Missed
Trusts You Might Have Missed
 
Powerpreter: Post Exploitation like a Boss
Powerpreter: Post Exploitation like a BossPowerpreter: Post Exploitation like a Boss
Powerpreter: Post Exploitation like a Boss
 
Six Degrees of Domain Admin - BloodHound at DEF CON 24
Six Degrees of Domain Admin - BloodHound at DEF CON 24Six Degrees of Domain Admin - BloodHound at DEF CON 24
Six Degrees of Domain Admin - BloodHound at DEF CON 24
 
Drilling deeper with Veil's PowerTools
Drilling deeper with Veil's PowerToolsDrilling deeper with Veil's PowerTools
Drilling deeper with Veil's PowerTools
 
AMSI: How Windows 10 Plans to Stop Script-Based Attacks and How Well It Does It
AMSI: How Windows 10 Plans to Stop Script-Based Attacks and How Well It Does ItAMSI: How Windows 10 Plans to Stop Script-Based Attacks and How Well It Does It
AMSI: How Windows 10 Plans to Stop Script-Based Attacks and How Well It Does It
 

Similaire à Pwnstaller

2600 av evasion_deuce
2600 av evasion_deuce2600 av evasion_deuce
2600 av evasion_deuce
Db Cooper
 
5 minute intro to virtualenv
5 minute intro to virtualenv5 minute intro to virtualenv
5 minute intro to virtualenv
amenasse
 
Serial Killer - Silently Pwning your Java Endpoints // OWASP BeNeLux Day 2016
Serial Killer - Silently Pwning your Java Endpoints // OWASP BeNeLux Day 2016Serial Killer - Silently Pwning your Java Endpoints // OWASP BeNeLux Day 2016
Serial Killer - Silently Pwning your Java Endpoints // OWASP BeNeLux Day 2016
Christian Schneider
 
Exploit Frameworks
Exploit FrameworksExploit Frameworks
Exploit Frameworks
phanleson
 

Similaire à Pwnstaller (20)

AWS EC2 Ubuntu Instance - Step-by-Step Deployment Guide
AWS EC2 Ubuntu Instance - Step-by-Step Deployment GuideAWS EC2 Ubuntu Instance - Step-by-Step Deployment Guide
AWS EC2 Ubuntu Instance - Step-by-Step Deployment Guide
 
2600 av evasion_deuce
2600 av evasion_deuce2600 av evasion_deuce
2600 av evasion_deuce
 
5 minute intro to virtualenv
5 minute intro to virtualenv5 minute intro to virtualenv
5 minute intro to virtualenv
 
Django Deployment
Django DeploymentDjango Deployment
Django Deployment
 
Porting your favourite cmdline tool to Android
Porting your favourite cmdline tool to AndroidPorting your favourite cmdline tool to Android
Porting your favourite cmdline tool to Android
 
Inside the Matrix,How to Build Transparent Sandbox for Malware Analysis
Inside the Matrix,How to Build Transparent Sandbox for Malware AnalysisInside the Matrix,How to Build Transparent Sandbox for Malware Analysis
Inside the Matrix,How to Build Transparent Sandbox for Malware Analysis
 
Dev Environments: The Next Generation
Dev Environments: The Next GenerationDev Environments: The Next Generation
Dev Environments: The Next Generation
 
Packaging in Python? Don't Roll the Dice.
Packaging in Python? Don't Roll the Dice.Packaging in Python? Don't Roll the Dice.
Packaging in Python? Don't Roll the Dice.
 
Prizm Installation Guide
Prizm Installation GuidePrizm Installation Guide
Prizm Installation Guide
 
AntiVirus Evasion Reconstructed - Veil 3.0
AntiVirus Evasion Reconstructed - Veil 3.0AntiVirus Evasion Reconstructed - Veil 3.0
AntiVirus Evasion Reconstructed - Veil 3.0
 
Serial Killer - Silently Pwning your Java Endpoints // OWASP BeNeLux Day 2016
Serial Killer - Silently Pwning your Java Endpoints // OWASP BeNeLux Day 2016Serial Killer - Silently Pwning your Java Endpoints // OWASP BeNeLux Day 2016
Serial Killer - Silently Pwning your Java Endpoints // OWASP BeNeLux Day 2016
 
LVPHP.org
LVPHP.orgLVPHP.org
LVPHP.org
 
Docker Starter Pack
Docker Starter PackDocker Starter Pack
Docker Starter Pack
 
Boot-To-Root KIOPTRIX Level -1
Boot-To-Root KIOPTRIX Level -1Boot-To-Root KIOPTRIX Level -1
Boot-To-Root KIOPTRIX Level -1
 
Lessons Learned Building a Container App Library
Lessons Learned Building a Container App LibraryLessons Learned Building a Container App Library
Lessons Learned Building a Container App Library
 
PHP Dependency Management with Composer
PHP Dependency Management with ComposerPHP Dependency Management with Composer
PHP Dependency Management with Composer
 
Exploit Frameworks
Exploit FrameworksExploit Frameworks
Exploit Frameworks
 
Introduction to Ansible (Pycon7 2016)
Introduction to Ansible (Pycon7 2016)Introduction to Ansible (Pycon7 2016)
Introduction to Ansible (Pycon7 2016)
 
HPX and PVS-Studio
HPX and PVS-StudioHPX and PVS-Studio
HPX and PVS-Studio
 
Why everyone is excited about Docker (and you should too...) - Carlo Bonamic...
Why everyone is excited about Docker (and you should too...) -  Carlo Bonamic...Why everyone is excited about Docker (and you should too...) -  Carlo Bonamic...
Why everyone is excited about Docker (and you should too...) - Carlo Bonamic...
 

Plus de Will Schroeder

Plus de Will Schroeder (11)

Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
Apres-Cyber - The Data Dilemma: Bridging Offensive Operations and Machine Lea...
 
Nemesis - SAINTCON.pdf
Nemesis - SAINTCON.pdfNemesis - SAINTCON.pdf
Nemesis - SAINTCON.pdf
 
ReCertifying Active Directory
ReCertifying Active DirectoryReCertifying Active Directory
ReCertifying Active Directory
 
Certified Pre-Owned
Certified Pre-OwnedCertified Pre-Owned
Certified Pre-Owned
 
SpecterOps Webinar Week - Kerberoasting Revisisted
SpecterOps Webinar Week - Kerberoasting RevisistedSpecterOps Webinar Week - Kerberoasting Revisisted
SpecterOps Webinar Week - Kerberoasting Revisisted
 
DerbyCon 2019 - Kerberoasting Revisited
DerbyCon 2019 - Kerberoasting RevisitedDerbyCon 2019 - Kerberoasting Revisited
DerbyCon 2019 - Kerberoasting Revisited
 
Not a Security Boundary
Not a Security BoundaryNot a Security Boundary
Not a Security Boundary
 
Derbycon - The Unintended Risks of Trusting Active Directory
Derbycon - The Unintended Risks of Trusting Active DirectoryDerbycon - The Unintended Risks of Trusting Active Directory
Derbycon - The Unintended Risks of Trusting Active Directory
 
The Unintended Risks of Trusting Active Directory
The Unintended Risks of Trusting Active DirectoryThe Unintended Risks of Trusting Active Directory
The Unintended Risks of Trusting Active Directory
 
An ACE in the Hole - Stealthy Host Persistence via Security Descriptors
An ACE in the Hole - Stealthy Host Persistence via Security DescriptorsAn ACE in the Hole - Stealthy Host Persistence via Security Descriptors
An ACE in the Hole - Stealthy Host Persistence via Security Descriptors
 
Ace Up the Sleeve
Ace Up the SleeveAce Up the Sleeve
Ace Up the Sleeve
 

Dernier

Dernier (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 

Pwnstaller

  • 2. $ whoami  Security researcher and penetration tester/red teamer for Veris Group’s Adaptive Threat Division  Co-founder of the Veil-Framework #avlol  www.veil-framework.com  Shmoocon ‘14: AV Evasion with the Veil Framework  co-wrote Veil-Evasion, wrote Veil-Catapult and Veil- PowerView  BSides ATX ‘14: Wielding a Cortana  Defcon ‘14 (accepted): Post-Exploitation 2.0
  • 3. tl;dr  Why we use Pyinstaller  DEP, Pyinstaller, and a weird Veil-Evasion bug  How Pyinstaller works  Pwnstaller v1.0  Questions
  • 4. Caveat  This is a proof of concept based off of an idea  Going to detail through the problem that prompted thinking about this, and walk through the thought process that led to the PoC solution  Probably a better way to do this, but seemed like an interesting concept and wanted to get the idea out there
  • 5. Pyinstaller 101  Pyinstaller is “a program that converts (packages) Python programs into stand-alone executables”  http://www.pyinstaller.org/  Packages Python scripts into OSX, Linux, or Windows self-extracting executables  Lets developers distribute projects without relying on an existing Python installation
  • 6. Pyinstaller Repurposed  Pentesters realized a few years ago that we could use it to package malicious scripts  advantageous, as legitimate projects use Pyinstaller  www.pyinstaller.org/wiki/ProjectsUsingPyInstaller  Dave Kennedy’s “PyInjector” was released in 2012 based on Debasish Mandal’s original post:  https://www.trustedsec.com/august-2012/new-tool- pyinjector-released-python-shellcode-injection/  http://www.debasish.in/2012_04_01_archive.html
  • 7. Pyinstaller in Veil- Evasion  Veil-Evasion sets up Pyinstaller under Wine so Python payloads can be compliled natively to Windows .exe’s  Generation is transparent to the user  Allows for the dynamic generation of Windows Python payloads, all on Kali!  We always want to preserve a single attack platform
  • 8. Veil Payloads and DEP  Void pointer casting for shellcode injection may fail, as the memory location used is not explicitly marked X (*(void(*)()) shellcode)();  Most systems tend to default to an opt-in DEP enforcement policy  if the executable you're running opts-in, void pointer casting will fail with a memory access violation
  • 9. A Weird Veil Bug  Python void pointer payloads worked as .py files, but failed as Pyinstaller executables  The python.exe interpreter used by Pyinstaller is not DEP enabled, but the resulting Pyinstaller payloads do in fact opt in to this protection see http://www.veil-evasion.com/dep-pyinstaller/
  • 10. How Pyinstaller Works  Pyinstaller uses the CArchive data structure to package up the main python .dll, any necessary libraries, and your target script  Basically like a compressed ZIP container  This CArchive is attached to then end of a “launcher” executable  We use the runw.exe version so we can hide the window, making execution transparent to the user
  • 12. How Pyinstaller Works  On execution, the launcher executable:  Decompresses the CArchive to a temporary location  Loads the python15.dll using LoadLibraryExA  Maps all the entry points in the python .dll for necessary methods  Sets up env stuff and starts the Python process  Imports all specified necessary modules  Runs the extracted script using PyRun_SimpleString
  • 13. How Pyinstaller Works: English  When the Pyinstaller produced executable is run, a minimal Python environment is extracted from a compressed attachment  Components necessary for the environment are registered and set up  The script attached is run  Lets you run Python scripts without Python being installed on a target machine!
  • 14. Solving The Veil Bug  So the DEP opt-in policy is determined by the launcher .exe, not the Python interpreter  Our next step was to generate a Pyinstaller launcher that didn’t opt-in to DEP  Luckily Pyinstaller is open source  https://www.veil-framework.com/dep-pyinstaller/
  • 15. Solving The Veil Bug  Pyinstaller holds precompiled copies of 32-bit and 64-bit loaders for Linux, OSX and Windows in pyinstaller/support/loader/*  The sources for the loaders are included in pyinstaller/source/*  runw.exe is the loader we want to regenerate  used for “windowed” executables DEP
  • 16. Turning Off DEP  The binaries utilize the WAF build system to build the loaders  ./pyinstaller/source/wscript  add conf.env.append_value('LINKFLAGS', '/NXCOMPAT:NO') right after the other flags on lines 209 and 211  This will instruct the Visual Studio linker to turn off DEP compatibility
  • 17. Problem?  Sweet, we have a shiny new launcher.exe  But our project is focused on evading AV  Including a static, custom-compiled launcher executable is a GREAT way to say “Hey vendors, check out this Veil-Evasion payload! Signatures lolz”
  • 18. Solution  Besides running Pyinstaller itself natively on Kali, we can dynamically recompile the Pyinstaller launcher on using mingw!  This makes it trivial to makes some small changes and get a different SHA1 signature each time  Why don’t we make it *a little* harder to flag on?
  • 19. Obfuscation: Phase 1  There are only a handful of source files needed to recompile runw.exe  utils.c - some helper methods (246 lines)  launch.c - “where the magic happens” (1617 lines)  main.c - invokes launch.c (165 lines)  ./zlib/* - extract of zlib v1.2.3  Lets start with some basic obfuscation
  • 20. Obfuscation: Phase 1  The initial goal: make ssdeep as useless as possible against “families” of our generated launcher  Any unnecessary code was stripped out (i.e. code for OSX and Linux binaries)  Thought process: randomize/shuffle wherever we can  A selection of random libraries imports thrown in
  • 21.
  • 22. Obfuscation: Phase 2  Let’s go just a bit further and have a some fun with anything doing basic dynamic analysis  How about interspersing lots of nested processing methods throughout the code  similar to our c/meterpreter/* payloads  This mucks up the call tree of the program without altering the actual execution
  • 23. Finishing Touches  The Pyinstaller icon is kind of recognizable  How about some randomized .ico’s instead?
  • 24. Putting It All Together  The end result, every time the generator runs:  obfuscated code for all* source files associated with the Pyinstaller launcher are generated  a randomized icon is chosen for the final packaged result  mingw32 is used to compile everything into a new runw.exe, all on Kali  the new runw.exe is copied into the correct resource location to be used by Pyinstaller *except some known zlib libraries
  • 25. ssdeep comparison  ssdeep is a ‘fuzzy hashing’ static malware comparison tool, allowing for the comparison of malware families  Generated a run of 1000 runw.exe loaders  (1000 choose 2) = 499500 possible comparison combinations  367,073 pairings (74%) scored 30/100 or better  228,961 pairings (46%) scored 50/100 or better  34,420 pairings (7%) scored 70/100 or better  0 pairings scored at 90/100 or better  What this means: none of the loader pairings scored as a closely ‘similar’ malware family
  • 26. ssdeep comparison 0 5000 10000 15000 20000 25000 30000 35000 40000 45000 0 10 20 30 40 50 60 70 80 90 100 occurances match % ssdeep matches
  • 27. In Plain English  Each generated Pyinstaller loader is reasonably unique from a basic static malware analysis perspective  Competent reversers will be able to figure out what’s going on in very little time  But hopefully this is relatively resistant against static signatures  I’m sure there are better obfuscation methods, so go implement them!
  • 28. Pwnstaller v1.0  http://www.harmj0y.net/blog/python/pwnstaller-1- 0/  The code is up on github: https://github.com/HarmJ0y/Pwnstaller  And it’s been integrated into Veil-Evasion  In the development branch now, hitting the master branch on the 5/15/2014 V-Day  All Python payloads can now utilize a dynamically generated Pwnstaller loader by choosing “2 - Pwnstaller” from the Python compilation menu
  • 30. Recap  Pyinstaller is some cool stuff  Pwnstaller will hopefully extend the lifetime of Veil- Evasion Python payloads by making static signatures reasonably difficult to write  “This is script-kiddie garbage that will harm users of Pyinstaller when AVs flag it without benefiting anyone who matters. Hope you get booed off at Bsides.” – The Internet
  • 31. Shameless Sidebar  Want to research cool stuff like this?  Want to work with 9 x OSCPs and 4 x OSCEs?  Want to do some sweet red teaming?  Hit me up to join the Adaptive Threat Division
  • 32. Questions?  Contact me:  @harmj0y  will@harmj0y.net  Read more:  http://www.harmj0y.net/blog/python/pwnstaller-1-0/  Get Pwnstaller:  https://github.com/HarmJ0y/Pwnstaller  Now in Veil-Evasion!

Notes de l'éditeur

  1. Around since ~2003/2004
  2. VirtualAlloc / RtlMoveMemory / CreateThread / WaitForSingleObject
  3. manually revamp this image?
  4. aka “context triggered piecewise hashes” “can match inputs that have homologies” – biology term, fancy way to saying degree of shared ancestry