SlideShare a Scribd company logo
Code Injection on WindowsRaashidBhatKashmirStudent Computer Security2nd year BE http://Twitter.com/raashidbhatt!
AgendaWhy Inject Code?Ways to Inject CodeQuestions?
Why inject Code?Trivially bypass anti-virus softwareTo be stealthy Malware makes the heavy use of injectionStealing credentials (Post Form grabbers, HTML injection etc. .etc.)Etc. etc.
Portable Executable(PE) FormatFile format for Windows executableConsists of Section having characteristics examples (.text, .bss,.data,.reloc , .debug)Imports and Exports by EXE file are stored in idata and rdata sectionsTexe 1.2 by Raashid Bhatt(PE Dumper) http://texe.codeplex.comBriefly Documented in <winnt.h>
Code injection Technique #1  # PE File Infection
PE File InfectionOverwrite the .code section ( or any section convenient for infection )Change the Entry Point of the ExecutableSave the registers , ESP, EBP etcReturn to original EP by Either Push EP ; RetOr JMP EP
The bad News?Calling functions egLoadlibrary() , GetprocAddress() in kernel32.dll when ASLR(address space layout randomization) is enabled. (/Fixed:NO MSVC)Sections .data,.bss are usually marked as writable and readable
RemedyUse PEB(Process Environment Block) to find kernel32.dll address PEB is located at FS[0x30]Consists heaps, binary information and loaded module information.Further Reading > The Last Stage of DeleriumWin32 Assembly Components. http://www.lsd-pl.net/documents/winasm-1.0.1.pdf;
Non-Executable SectionsSections .data,.bss.idata.edataetc are not executable as they are marked 0xC0000040          	INITIALIZED_DATA|READ|WRITEChange >>PIMAGE_SECTION_HEADER-> Characteristics = IMAGE_SCN_CNT_CODE (documented in Winnt.h)
Code injection Technique #2 # IAT Hooking
IATIAT(import address table)  holds information regarding the DLL to be loaded by a PE fileFunctions are Linked either by a ordinal or by name.Stored in .idatasection of PE file.Define in struct _IMAGE_IMPORT_DESCRIPTOR <winnt.h>
IAT hooking Used by botnets for Credential stealing (POST Form Grabbers, 0n-fly  html Injection)Can be achieved by changing the name of the Dll inside the import address table(IAT) table to proxy DllActivated when any function is called in org DLL
Proxy Dll(user32.dll)dllmain(...)int WINAPI MessageBoxA(...){	user32.ldd_MessageBoxA(...);	/* user code */}.Example for user32.dll proxy dll
Code injection Technique #3# Runtime Code Injection
CreateRemoteThreadWindows has CreateRemoteThread() APIAccording to MSDN “The CreateRemoteThread function creates a thread that runs in the virtual address space of another process”memory allocation in another process (possible) using VirtualAllocEx() APIForeign process memory read and write using WriteProcessMemory() & ReadProcessMemory()
1: DLL Loading  DLL’s can be loaded in another process using CreateRemoteThread. Steps:1: Allocate memory for the DLL name in the remote target process2:Write the DLL name, including full path, to the allocated memory.3:Mapping our DLL to the remote process via CreateRemoteThread & LoadLibrary
pLibRemote= VirtualAllocEx(hProc, NULL, sizeof(szDllPath), MEM_COMMIT, PAGE_READWRITE );bWriteCheck= WriteProcessMemory(hProc, pLibRemote, (void*)szDllPath, sizeof(szDllPath), NULL );	hThread = CreateRemoteThread(	hProc,NULL,NULL,(LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32,"LoadLibraryA"),pLibRemote,NULL, NULL);	Equivalent to LoadlibraryA(“Dll name”);
2:In memory Execution First Documented as  “Reflective DLL Injection By Stephen Fewer” Harmony Security Implemented in MetasploitPlayloadInvolves Writing a Exe or dll file in the memory and executing from within Stealthy Execution
2:In memory Execution Implementing a minimal Portable Executable (PE) file loader. 1: Allocate Memory and Copy the file to memory2:Parse the Import Address table of PE File and Perform Fixups3:calculate the new base and Perform relocation (IMPORTANT)4:JUMP to Entry point of The PE File
Image RelocationsCertain hardcoded addresses need to be fixedInt x; int *p = &x;(hardcoded into p)PE file Stores Relocation Entries in .reloc section .reloc section stores offsets to the addresses to be fixed
Example of .reloc section0x0001 --- DD (pointer) 0x0013 >>0x0010 --- 0xdeadbeef0x0011 --- 0xdeadbeef0x0013 --- 0xdeadbeef..reloc sectionRELOC TYPE (4BITS) OFFSET(12bits) RVA
			         Thanks Questions?

More Related Content

What's hot (20)

PDF
Automating the mundanity of technique IDs with ATT&CK Detections Collector
MITRE ATT&CK
 
PPTX
Password cracking and brute force tools
zeus7856
 
PPTX
Number theory and cryptography
Yasser Ali
 
PPT
Elementary cryptography
Prachi Gulihar
 
PPTX
Sessions in php
Mudasir Syed
 
PPT
Hash Function & Analysis
Pawandeep Kaur
 
PPTX
Security vulnerability
A. Shamel
 
PPT
IIS
Giritharan V
 
PDF
05 wireless
Setia Juli Irzal Ismail
 
PPTX
Difference between Github vs Gitlab vs Bitbucket
jeetendra mandal
 
PDF
End-to-End Security Analytics with the Elastic Stack
Elasticsearch
 
PPTX
Web application security
Kapil Sharma
 
PPTX
Introduction to Metasploit
GTU
 
PPTX
Introduction to php
shanmukhareddy dasi
 
PPTX
Cryptography.ppt
Uday Meena
 
PDF
Menggunakan Kali Linux Untuk Mengetahui Kelemahan Implementasi TI
Ismail Fahmi
 
PPTX
COM Hijacking Techniques - Derbycon 2019
David Tulis
 
PPTX
Public Key Cryptosystem
Devakumar Kp
 
PPTX
Metasploit framwork
Deepanshu Gajbhiye
 
Automating the mundanity of technique IDs with ATT&CK Detections Collector
MITRE ATT&CK
 
Password cracking and brute force tools
zeus7856
 
Number theory and cryptography
Yasser Ali
 
Elementary cryptography
Prachi Gulihar
 
Sessions in php
Mudasir Syed
 
Hash Function & Analysis
Pawandeep Kaur
 
Security vulnerability
A. Shamel
 
Difference between Github vs Gitlab vs Bitbucket
jeetendra mandal
 
End-to-End Security Analytics with the Elastic Stack
Elasticsearch
 
Web application security
Kapil Sharma
 
Introduction to Metasploit
GTU
 
Introduction to php
shanmukhareddy dasi
 
Cryptography.ppt
Uday Meena
 
Menggunakan Kali Linux Untuk Mengetahui Kelemahan Implementasi TI
Ismail Fahmi
 
COM Hijacking Techniques - Derbycon 2019
David Tulis
 
Public Key Cryptosystem
Devakumar Kp
 
Metasploit framwork
Deepanshu Gajbhiye
 

Viewers also liked (11)

PPTX
Injection on Steroids: Codeless code injection and 0-day techniques
enSilo
 
PDF
Process injection - Malware style
Sander Demeester
 
PPTX
Steelcon 2014 - Process Injection with Python
infodox
 
PDF
Packers
Ange Albertini
 
PDF
Hollow Process Injection - Reversing and Investigating Malware Evasive Tactics
securityxploded
 
PPT
IPS Product Comparison of Cisco 4255 & TippingPoint 5000E
allengalvan
 
PDF
Hollow process injection
Cysinfo Cyber Security Community
 
PPTX
Remnux tutorial-1 Statically Analyse Portable Executable(PE) Files
Rhydham Joshi
 
PPTX
Intrusion Prevention System
Vishwanath Badiger
 
PPT
Intrusion detection system ppt
Sheetal Verma
 
PPSX
Intrusion detection system
gaurav koriya
 
Injection on Steroids: Codeless code injection and 0-day techniques
enSilo
 
Process injection - Malware style
Sander Demeester
 
Steelcon 2014 - Process Injection with Python
infodox
 
Hollow Process Injection - Reversing and Investigating Malware Evasive Tactics
securityxploded
 
IPS Product Comparison of Cisco 4255 & TippingPoint 5000E
allengalvan
 
Hollow process injection
Cysinfo Cyber Security Community
 
Remnux tutorial-1 Statically Analyse Portable Executable(PE) Files
Rhydham Joshi
 
Intrusion Prevention System
Vishwanath Badiger
 
Intrusion detection system ppt
Sheetal Verma
 
Intrusion detection system
gaurav koriya
 
Ad

Similar to Code Injection in Windows (20)

PDF
Bh Usa 07 Butler And Kendall
KarlFrank99
 
PPTX
Reversing malware analysis training part3 windows pefile formatbasics
Cysinfo Cyber Security Community
 
PDF
CNIT 126 12: Covert Malware Launching
Sam Bowne
 
PDF
Failure Of DEP And ASLR
n|u - The Open Security Community
 
PDF
DLL Injection
Hossein Yavari
 
PPTX
OS Internals and Portable Executable File Format
Aitezaz Mohsin
 
PPTX
Taking Hunting to the Next Level: Hunting in Memory
Joe Desimone
 
PDF
Piratng Avs to bypass exploit mitigation
Priyanka Aash
 
PDF
Captain Hook: Pirating AVs to Bypass Exploit Mitigations
enSilo
 
PDF
Process Doppelgänging
KarlFrank99
 
PDF
Binary art - Byte-ing the PE that fails you (extended offline version)
Ange Albertini
 
PPT
bh-europe-01-clowes
guest3e5046
 
ODP
Such a weird Processor: messing with opcodes (...and a little bit of PE) (Has...
Ange Albertini
 
PDF
Practical Malware Analysis Ch12
Sam Bowne
 
PDF
fg.workshop: Software vulnerability
fg.informatik Universität Basel
 
PDF
HES2011 - Aaron Portnoy and Logan Brown - Black Box Auditing Adobe Shockwave
Hackito Ergo Sum
 
PPT
Dll injection
KarlFrank99
 
PPTX
DLL Injection
Neeraj Godkhindi (OSCP)
 
PDF
CNIT 127 Ch 6: The Wild World of Windows
Sam Bowne
 
ODP
x86 & PE
Ange Albertini
 
Bh Usa 07 Butler And Kendall
KarlFrank99
 
Reversing malware analysis training part3 windows pefile formatbasics
Cysinfo Cyber Security Community
 
CNIT 126 12: Covert Malware Launching
Sam Bowne
 
Failure Of DEP And ASLR
n|u - The Open Security Community
 
DLL Injection
Hossein Yavari
 
OS Internals and Portable Executable File Format
Aitezaz Mohsin
 
Taking Hunting to the Next Level: Hunting in Memory
Joe Desimone
 
Piratng Avs to bypass exploit mitigation
Priyanka Aash
 
Captain Hook: Pirating AVs to Bypass Exploit Mitigations
enSilo
 
Process Doppelgänging
KarlFrank99
 
Binary art - Byte-ing the PE that fails you (extended offline version)
Ange Albertini
 
bh-europe-01-clowes
guest3e5046
 
Such a weird Processor: messing with opcodes (...and a little bit of PE) (Has...
Ange Albertini
 
Practical Malware Analysis Ch12
Sam Bowne
 
fg.workshop: Software vulnerability
fg.informatik Universität Basel
 
HES2011 - Aaron Portnoy and Logan Brown - Black Box Auditing Adobe Shockwave
Hackito Ergo Sum
 
Dll injection
KarlFrank99
 
CNIT 127 Ch 6: The Wild World of Windows
Sam Bowne
 
x86 & PE
Ange Albertini
 
Ad

More from n|u - The Open Security Community (20)

PDF
Hardware security testing 101 (Null - Delhi Chapter)
n|u - The Open Security Community
 
PPTX
SSRF exploit the trust relationship
n|u - The Open Security Community
 
PDF
Metasploit primary
n|u - The Open Security Community
 
PDF
Api security-testing
n|u - The Open Security Community
 
PDF
Introduction to TLS 1.3
n|u - The Open Security Community
 
PDF
Gibson 101 -quick_introduction_to_hacking_mainframes_in_2020_null_infosec_gir...
n|u - The Open Security Community
 
PDF
Talking About SSRF,CRLF
n|u - The Open Security Community
 
PPTX
Building active directory lab for red teaming
n|u - The Open Security Community
 
PPTX
Owning a company through their logs
n|u - The Open Security Community
 
PPTX
Introduction to shodan
n|u - The Open Security Community
 
PDF
Detecting persistence in windows
n|u - The Open Security Community
 
PPTX
Frida - Objection Tool Usage
n|u - The Open Security Community
 
PDF
OSQuery - Monitoring System Process
n|u - The Open Security Community
 
PDF
DevSecOps Jenkins Pipeline -Security
n|u - The Open Security Community
 
PDF
Extensible markup language attacks
n|u - The Open Security Community
 
PPTX
Linux for hackers
n|u - The Open Security Community
 
PDF
Android Pentesting
n|u - The Open Security Community
 
Hardware security testing 101 (Null - Delhi Chapter)
n|u - The Open Security Community
 
SSRF exploit the trust relationship
n|u - The Open Security Community
 
Api security-testing
n|u - The Open Security Community
 
Introduction to TLS 1.3
n|u - The Open Security Community
 
Gibson 101 -quick_introduction_to_hacking_mainframes_in_2020_null_infosec_gir...
n|u - The Open Security Community
 
Talking About SSRF,CRLF
n|u - The Open Security Community
 
Building active directory lab for red teaming
n|u - The Open Security Community
 
Owning a company through their logs
n|u - The Open Security Community
 
Introduction to shodan
n|u - The Open Security Community
 
Detecting persistence in windows
n|u - The Open Security Community
 
Frida - Objection Tool Usage
n|u - The Open Security Community
 
OSQuery - Monitoring System Process
n|u - The Open Security Community
 
DevSecOps Jenkins Pipeline -Security
n|u - The Open Security Community
 
Extensible markup language attacks
n|u - The Open Security Community
 

Recently uploaded (20)

PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PPTX
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PPTX
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
PDF
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
PDF
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PDF
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PDF
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
PDF
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Impact of IEEE Computer Society in Advancing Emerging Technologies including ...
Hironori Washizaki
 
Top Managed Service Providers in Los Angeles
Captain IT
 
Extensions Framework (XaaS) - Enabling Orchestrate Anything
ShapeBlue
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
Darren Mills The Migration Modernization Balancing Act: Navigating Risks and...
AWS Chicago
 
Wojciech Ciemski for Top Cyber News MAGAZINE. June 2025
Dr. Ludmila Morozova-Buss
 
CIFDAQ Weekly Market Wrap for 11th July 2025
CIFDAQ
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
NewMind AI Journal - Weekly Chronicles - July'25 Week II
NewMind AI
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
The Builder’s Playbook - 2025 State of AI Report.pdf
jeroen339954
 
TrustArc Webinar - Data Privacy Trends 2025: Mid-Year Insights & Program Stra...
TrustArc
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 

Code Injection in Windows

  • 1. Code Injection on WindowsRaashidBhatKashmirStudent Computer Security2nd year BE http://Twitter.com/raashidbhatt!
  • 2. AgendaWhy Inject Code?Ways to Inject CodeQuestions?
  • 3. Why inject Code?Trivially bypass anti-virus softwareTo be stealthy Malware makes the heavy use of injectionStealing credentials (Post Form grabbers, HTML injection etc. .etc.)Etc. etc.
  • 4. Portable Executable(PE) FormatFile format for Windows executableConsists of Section having characteristics examples (.text, .bss,.data,.reloc , .debug)Imports and Exports by EXE file are stored in idata and rdata sectionsTexe 1.2 by Raashid Bhatt(PE Dumper) http://texe.codeplex.comBriefly Documented in <winnt.h>
  • 5. Code injection Technique #1 # PE File Infection
  • 6. PE File InfectionOverwrite the .code section ( or any section convenient for infection )Change the Entry Point of the ExecutableSave the registers , ESP, EBP etcReturn to original EP by Either Push EP ; RetOr JMP EP
  • 7. The bad News?Calling functions egLoadlibrary() , GetprocAddress() in kernel32.dll when ASLR(address space layout randomization) is enabled. (/Fixed:NO MSVC)Sections .data,.bss are usually marked as writable and readable
  • 8. RemedyUse PEB(Process Environment Block) to find kernel32.dll address PEB is located at FS[0x30]Consists heaps, binary information and loaded module information.Further Reading > The Last Stage of DeleriumWin32 Assembly Components. http://www.lsd-pl.net/documents/winasm-1.0.1.pdf;
  • 9. Non-Executable SectionsSections .data,.bss.idata.edataetc are not executable as they are marked 0xC0000040 INITIALIZED_DATA|READ|WRITEChange >>PIMAGE_SECTION_HEADER-> Characteristics = IMAGE_SCN_CNT_CODE (documented in Winnt.h)
  • 10. Code injection Technique #2 # IAT Hooking
  • 11. IATIAT(import address table) holds information regarding the DLL to be loaded by a PE fileFunctions are Linked either by a ordinal or by name.Stored in .idatasection of PE file.Define in struct _IMAGE_IMPORT_DESCRIPTOR <winnt.h>
  • 12. IAT hooking Used by botnets for Credential stealing (POST Form Grabbers, 0n-fly html Injection)Can be achieved by changing the name of the Dll inside the import address table(IAT) table to proxy DllActivated when any function is called in org DLL
  • 13. Proxy Dll(user32.dll)dllmain(...)int WINAPI MessageBoxA(...){ user32.ldd_MessageBoxA(...); /* user code */}.Example for user32.dll proxy dll
  • 14. Code injection Technique #3# Runtime Code Injection
  • 15. CreateRemoteThreadWindows has CreateRemoteThread() APIAccording to MSDN “The CreateRemoteThread function creates a thread that runs in the virtual address space of another process”memory allocation in another process (possible) using VirtualAllocEx() APIForeign process memory read and write using WriteProcessMemory() & ReadProcessMemory()
  • 16. 1: DLL Loading DLL’s can be loaded in another process using CreateRemoteThread. Steps:1: Allocate memory for the DLL name in the remote target process2:Write the DLL name, including full path, to the allocated memory.3:Mapping our DLL to the remote process via CreateRemoteThread & LoadLibrary
  • 17. pLibRemote= VirtualAllocEx(hProc, NULL, sizeof(szDllPath), MEM_COMMIT, PAGE_READWRITE );bWriteCheck= WriteProcessMemory(hProc, pLibRemote, (void*)szDllPath, sizeof(szDllPath), NULL ); hThread = CreateRemoteThread( hProc,NULL,NULL,(LPTHREAD_START_ROUTINE)GetProcAddress(hKernel32,"LoadLibraryA"),pLibRemote,NULL, NULL); Equivalent to LoadlibraryA(“Dll name”);
  • 18. 2:In memory Execution First Documented as “Reflective DLL Injection By Stephen Fewer” Harmony Security Implemented in MetasploitPlayloadInvolves Writing a Exe or dll file in the memory and executing from within Stealthy Execution
  • 19. 2:In memory Execution Implementing a minimal Portable Executable (PE) file loader. 1: Allocate Memory and Copy the file to memory2:Parse the Import Address table of PE File and Perform Fixups3:calculate the new base and Perform relocation (IMPORTANT)4:JUMP to Entry point of The PE File
  • 20. Image RelocationsCertain hardcoded addresses need to be fixedInt x; int *p = &x;(hardcoded into p)PE file Stores Relocation Entries in .reloc section .reloc section stores offsets to the addresses to be fixed
  • 21. Example of .reloc section0x0001 --- DD (pointer) 0x0013 >>0x0010 --- 0xdeadbeef0x0011 --- 0xdeadbeef0x0013 --- 0xdeadbeef..reloc sectionRELOC TYPE (4BITS) OFFSET(12bits) RVA
  • 22. Thanks Questions?