SlideShare a Scribd company logo
Dynamic
Binary
Instrumentation
Using Intel’s PIN
What is Instrumentation
● Inserting extra lines of code into the processes memory.
● Intel’s PIN, Google’s Address Sanitizer, DynamoRIO, Valgrind, GDB.
● Useful for reverse engineering and malware analysis.
Why DBI
…...
if (size < sizeof(min_buf)) {
iov_to_buf(iov, iovcnt, 0, min_buf, size);
memset(&min_buf[size], 0, sizeof(min_buf) - size);
}
else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
/* This is very unlikely, but may happen. */
iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
filter_buf = min_buf;
}
…...
….
if (size < sizeof(min_buf)) {
printf(“Good size branchn”);
iov_to_buf(iov, iovcnt, 0, min_buf, size);
memset(&min_buf[size], 0, sizeof(min_buf) - size);
}
else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) {
/* This is very unlikely, but may happen. */
printf(“Got a rare casen”);
iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN);
filter_buf = min_buf;
}
….
Installation guidelines
● https://labs.portcullis.co.uk/blog/an-introduction-to-binary-dynamic-
analysis/
● https://software.intel.
com/sites/landingpage/pintool/docs/76991/Pin/html/
Where’s the code?
● Comes with pre-existing scripts.
● Feature to add custom scripts.
● Written in C or C++.
● Examples in ~/pin/source/tools/SimpleExamples/
#include <iostream>
#include "pin.H"
UINT64 icount = 0;
VOID IncCounter() {
icount++;}
VOID Instruction(INS ins, VOID *v) {
INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)IncCounter, IARG_END);}
VOID Fini(INT32 code, VOID *v) {
std::cerr << "Count " << icount << endl;}
int main(int argc, char * argv[]) {
PIN_Init(argc, argv);
INS_AddInstrumentFunction(Instruction, 0);
PIN_AddFiniFunction(Fini, 0);
PIN_StartProgram();
return 0;}
Analysis
Instrumentation
Execution
$ pin -t inscount.so -- /bin/ls
inscount.cpp inscount.so inscount.o
Count 422838 Output of inscount
$
Detecting Heap bugs
● Keep list of used and free chunks.
● If input is read to any of these chunks, check sizes and number of bytes
being read.
● In case of structure objects, check for input being read to an address
inside a chunk.
● If input is read to free chunk => UAF.
● If number of bytes read > size of chunk => heap overflow.
● If chunk_start + size - address < number of bytes => heap overflow.
The code
● Heap_trace.cpp
● Need to check for other functions like scanf(), strncpy(), memcpy() etc.
● Alerts:
○ When the same chunk is returned by malloc more than once.
○ When the same chunk is going to be freed more than once.
○ When input crosses chunk boundaries.
○ When input is copied to free chunks.
● Around 200 lines of code (excluding the nice comments).
● Let’s have a look.
● Demo1
C or C++ ? That’s it?
● Kudos to the owners.
● Blankwall - Python Pin.
● A python wrapper to PIN.
● Not yet complete.
import sys, pin
total = 0
info = file("inscount.out", "w")
def counter(trace_addr):
global total
x = pin.TRACE_BblHead(trace_addr)
y = pin.BBL_Address(x)
instrucs = pin.BBL_NumIns(x)
total += instrucs
info.write("Basic Block @ %x SIZE: %x NUM INS= IN BLOCK: %x TOTAL: %xn" % (y, pin.BBL_Size
(x), instrucs, total ))
pin.TRACE_AddInstrumentFunction(counter)
$ pin -t obj-intel64/Python_Pin.so -m ins_count.py -- /bin/ls
$ cat inscount.out|head
Basic Block @ 7ffff7ddb2d0 SIZE: 8 NUM INS= IN BLOCK: 2 TOTAL: 2
Basic Block @ 7ffff7ddea40 SIZE: 55 NUM INS= IN BLOCK: 16 TOTAL: 18
Basic Block @ 7ffff7ddeaef SIZE: 6 NUM INS= IN BLOCK: 2 TOTAL: 1a
Basic Block @ 7ffff7ddead8 SIZE: 17 NUM INS= IN BLOCK: 6 TOTAL: 20
Heap_Tracer.py ?
● 90 lines of code. ( Hurray! )
● Let’s take a look.
● Demo2

More Related Content

PDF
Binary instrumentation - dc9723
PPTX
Reversing & Malware Analysis Training Part 6 - Practical Reversing (I)
PDF
nullcon 2011 - Automatic Program Analysis using Dynamic Binary Instrumentation
PPTX
Reversing malware analysis training part4 assembly programming basics
PPTX
Buffer overflow attacks
PPTX
Buffer overflow
PDF
Presentation buffer overflow attacks and theircountermeasures
PPTX
Buffer Overflow Demo by Saurabh Sharma
Binary instrumentation - dc9723
Reversing & Malware Analysis Training Part 6 - Practical Reversing (I)
nullcon 2011 - Automatic Program Analysis using Dynamic Binary Instrumentation
Reversing malware analysis training part4 assembly programming basics
Buffer overflow attacks
Buffer overflow
Presentation buffer overflow attacks and theircountermeasures
Buffer Overflow Demo by Saurabh Sharma

What's hot (20)

PPTX
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
PPTX
Advanced malware analysis training session5 reversing automation
PPT
Buffer Overflows
PPTX
Control hijacking
PPTX
Buffer overflow attacks
PPTX
Anatomy of a Buffer Overflow Attack
PPTX
Advanced malwareanalysis training session2 botnet analysis part1
PDF
Source Boston 2009 - Anti-Debugging A Developers Viewpoint
PPT
6 buffer overflows
PPTX
CodeChecker summary 21062021
PPT
Buffer Overflow Attacks
PPTX
Reversing malware analysis training part3 windows pefile formatbasics
PDF
Dynamic PHP web-application analysis
PPTX
Reversing malware analysis training part6 practical reversing
PPTX
08 - Return Oriented Programming, the chosen one
PDF
Course lecture - An introduction to the Return Oriented Programming
PPTX
CodeChecker Overview Nov 2019
PPTX
Advanced malware analysis training session4 anti-analysis techniques
PDF
Possibility of arbitrary code execution by Step-Oriented Programming
PPTX
Return oriented programming (ROP)
Reversing & Malware Analysis Training Part 4 - Assembly Programming Basics
Advanced malware analysis training session5 reversing automation
Buffer Overflows
Control hijacking
Buffer overflow attacks
Anatomy of a Buffer Overflow Attack
Advanced malwareanalysis training session2 botnet analysis part1
Source Boston 2009 - Anti-Debugging A Developers Viewpoint
6 buffer overflows
CodeChecker summary 21062021
Buffer Overflow Attacks
Reversing malware analysis training part3 windows pefile formatbasics
Dynamic PHP web-application analysis
Reversing malware analysis training part6 practical reversing
08 - Return Oriented Programming, the chosen one
Course lecture - An introduction to the Return Oriented Programming
CodeChecker Overview Nov 2019
Advanced malware analysis training session4 anti-analysis techniques
Possibility of arbitrary code execution by Step-Oriented Programming
Return oriented programming (ROP)
Ad

Viewers also liked (20)

PDF
Format string vunerability
PDF
Buffer overflow Attacks
PPTX
Dll preloading-attack
PPTX
Watering hole attacks case study analysis
PPTX
Dissecting Android APK
PPTX
Homomorphic encryption
PPTX
Reversing malware analysis training part11 exploit development advanced
PPTX
Advanced malware analysis training session11 part2 dissecting the heart beat ...
PPTX
Investigating Malware using Memory Forensics
PPTX
Advanced malware analysis training session3 botnet analysis part2
PPTX
Reversing malware analysis training part10 exploit development basics
ODP
Introduction to Binary Exploitation
PPTX
Exploits & Mitigations - Memory Corruption Techniques
PDF
POS Malware: Is your Debit/Credit Transcations Secure?
PPTX
Introduction to ICS/SCADA security
PDF
Understanding APT1 malware techniques using malware analysis and reverse engi...
PPTX
Hunting rootkit from dark corners of memory
PPTX
XXE - XML External Entity Attack
PDF
Linux Malware Analysis
Format string vunerability
Buffer overflow Attacks
Dll preloading-attack
Watering hole attacks case study analysis
Dissecting Android APK
Homomorphic encryption
Reversing malware analysis training part11 exploit development advanced
Advanced malware analysis training session11 part2 dissecting the heart beat ...
Investigating Malware using Memory Forensics
Advanced malware analysis training session3 botnet analysis part2
Reversing malware analysis training part10 exploit development basics
Introduction to Binary Exploitation
Exploits & Mitigations - Memory Corruption Techniques
POS Malware: Is your Debit/Credit Transcations Secure?
Introduction to ICS/SCADA security
Understanding APT1 malware techniques using malware analysis and reverse engi...
Hunting rootkit from dark corners of memory
XXE - XML External Entity Attack
Linux Malware Analysis
Ad

Similar to Dynamic Binary Instrumentation (20)

PPTX
Why learn Internals?
PDF
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
PDF
Programar para GPUs
ODP
Linux kernel tracing superpowers in the cloud
PPTX
grsecurity and PaX
PDF
Cluj.py Meetup: Extending Python in C
PPTX
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
DOCX
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
PDF
Jurczyk windows kernel reference count vulnerabilities. case study
PDF
The walking 0xDEAD
PDF
PPU Optimisation Lesson
PDF
How to avoid Go gotchas - Ivan Daniluk - Codemotion Milan 2016
PDF
JVM Performance Magic Tricks
PPTX
C Programming Training in Ambala ! Batra Computer Centre
PDF
JIT compilation for CPython
PDF
PythonBrasil[8] - CPython for dummies
PDF
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
PPTX
Programming python quick intro for schools
PDF
Unmanaged Parallelization via P/Invoke
PDF
Csw2016 wheeler barksdale-gruskovnjak-execute_mypacket
Why learn Internals?
Semtex.c [CVE-2013-2094] - A Linux Privelege Escalation
Programar para GPUs
Linux kernel tracing superpowers in the cloud
grsecurity and PaX
Cluj.py Meetup: Extending Python in C
PVS-Studio 5.00, a solution for developers of modern resource-intensive appl...
20145-5SumII_CSC407_assign1.htmlCSC 407 Computer Systems II.docx
Jurczyk windows kernel reference count vulnerabilities. case study
The walking 0xDEAD
PPU Optimisation Lesson
How to avoid Go gotchas - Ivan Daniluk - Codemotion Milan 2016
JVM Performance Magic Tricks
C Programming Training in Ambala ! Batra Computer Centre
JIT compilation for CPython
PythonBrasil[8] - CPython for dummies
ExperiencesSharingOnEmbeddedSystemDevelopment_20160321
Programming python quick intro for schools
Unmanaged Parallelization via P/Invoke
Csw2016 wheeler barksdale-gruskovnjak-execute_mypacket

More from Cysinfo Cyber Security Community (20)

PDF
Understanding Malware Persistence Techniques by Monnappa K A
PDF
Understanding & analyzing obfuscated malicious web scripts by Vikram Kharvi
PDF
Getting started with cybersecurity through CTFs by Shruti Dixit & Geethna TK
PPTX
Emerging Trends in Cybersecurity by Amar Prusty
PDF
A look into the sanitizer family (ASAN & UBSAN) by Akul Pillai
PDF
Closer look at PHP Unserialization by Ashwin Shenoi
PDF
Unicorn: The Ultimate CPU Emulator by Akshay Ajayan
PDF
The Art of Executing JavaScript by Akhil Mahendra
PDF
Reversing and Decrypting Malware Communications by Monnappa
PPTX
DeViL - Detect Virtual Machine in Linux by Sreelakshmi
PPTX
Analysis of android apk using adhrit by Abhishek J.M
PDF
Understanding evasive hollow process injection techniques monnappa k a
PPTX
Security challenges in d2d communication by ajithkumar vyasarao
PPTX
S2 e (selective symbolic execution) -shivkrishna a
PPTX
Dynamic binary analysis using angr siddharth muralee
PPTX
Bit flipping attack on aes cbc - ashutosh ahelleya
PDF
Security Analytics using ELK stack
PDF
ATM Malware: Understanding the threat
PPT
Image (PNG) Forensic Analysis
PPT
Malware Detection using Machine Learning
Understanding Malware Persistence Techniques by Monnappa K A
Understanding & analyzing obfuscated malicious web scripts by Vikram Kharvi
Getting started with cybersecurity through CTFs by Shruti Dixit & Geethna TK
Emerging Trends in Cybersecurity by Amar Prusty
A look into the sanitizer family (ASAN & UBSAN) by Akul Pillai
Closer look at PHP Unserialization by Ashwin Shenoi
Unicorn: The Ultimate CPU Emulator by Akshay Ajayan
The Art of Executing JavaScript by Akhil Mahendra
Reversing and Decrypting Malware Communications by Monnappa
DeViL - Detect Virtual Machine in Linux by Sreelakshmi
Analysis of android apk using adhrit by Abhishek J.M
Understanding evasive hollow process injection techniques monnappa k a
Security challenges in d2d communication by ajithkumar vyasarao
S2 e (selective symbolic execution) -shivkrishna a
Dynamic binary analysis using angr siddharth muralee
Bit flipping attack on aes cbc - ashutosh ahelleya
Security Analytics using ELK stack
ATM Malware: Understanding the threat
Image (PNG) Forensic Analysis
Malware Detection using Machine Learning

Recently uploaded (20)

PDF
KodekX | Application Modernization Development
PDF
Approach and Philosophy of On baking technology
PPTX
A Presentation on Artificial Intelligence
PDF
NewMind AI Monthly Chronicles - July 2025
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Electronic commerce courselecture one. Pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
KodekX | Application Modernization Development
Approach and Philosophy of On baking technology
A Presentation on Artificial Intelligence
NewMind AI Monthly Chronicles - July 2025
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Electronic commerce courselecture one. Pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Encapsulation theory and applications.pdf
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
20250228 LYD VKU AI Blended-Learning.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”

Dynamic Binary Instrumentation

  • 2. What is Instrumentation ● Inserting extra lines of code into the processes memory. ● Intel’s PIN, Google’s Address Sanitizer, DynamoRIO, Valgrind, GDB. ● Useful for reverse engineering and malware analysis.
  • 3. Why DBI …... if (size < sizeof(min_buf)) { iov_to_buf(iov, iovcnt, 0, min_buf, size); memset(&min_buf[size], 0, sizeof(min_buf) - size); } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) { /* This is very unlikely, but may happen. */ iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN); filter_buf = min_buf; } …...
  • 4. …. if (size < sizeof(min_buf)) { printf(“Good size branchn”); iov_to_buf(iov, iovcnt, 0, min_buf, size); memset(&min_buf[size], 0, sizeof(min_buf) - size); } else if (iov->iov_len < MAXIMUM_ETHERNET_HDR_LEN) { /* This is very unlikely, but may happen. */ printf(“Got a rare casen”); iov_to_buf(iov, iovcnt, 0, min_buf, MAXIMUM_ETHERNET_HDR_LEN); filter_buf = min_buf; } ….
  • 5. Installation guidelines ● https://labs.portcullis.co.uk/blog/an-introduction-to-binary-dynamic- analysis/ ● https://software.intel. com/sites/landingpage/pintool/docs/76991/Pin/html/
  • 6. Where’s the code? ● Comes with pre-existing scripts. ● Feature to add custom scripts. ● Written in C or C++. ● Examples in ~/pin/source/tools/SimpleExamples/
  • 7. #include <iostream> #include "pin.H" UINT64 icount = 0; VOID IncCounter() { icount++;} VOID Instruction(INS ins, VOID *v) { INS_InsertCall(ins, IPOINT_BEFORE, (AFUNPTR)IncCounter, IARG_END);} VOID Fini(INT32 code, VOID *v) { std::cerr << "Count " << icount << endl;} int main(int argc, char * argv[]) { PIN_Init(argc, argv); INS_AddInstrumentFunction(Instruction, 0); PIN_AddFiniFunction(Fini, 0); PIN_StartProgram(); return 0;} Analysis Instrumentation
  • 8. Execution $ pin -t inscount.so -- /bin/ls inscount.cpp inscount.so inscount.o Count 422838 Output of inscount $
  • 9. Detecting Heap bugs ● Keep list of used and free chunks. ● If input is read to any of these chunks, check sizes and number of bytes being read. ● In case of structure objects, check for input being read to an address inside a chunk. ● If input is read to free chunk => UAF. ● If number of bytes read > size of chunk => heap overflow. ● If chunk_start + size - address < number of bytes => heap overflow.
  • 10. The code ● Heap_trace.cpp ● Need to check for other functions like scanf(), strncpy(), memcpy() etc. ● Alerts: ○ When the same chunk is returned by malloc more than once. ○ When the same chunk is going to be freed more than once. ○ When input crosses chunk boundaries. ○ When input is copied to free chunks. ● Around 200 lines of code (excluding the nice comments). ● Let’s have a look. ● Demo1
  • 11. C or C++ ? That’s it? ● Kudos to the owners. ● Blankwall - Python Pin. ● A python wrapper to PIN. ● Not yet complete.
  • 12. import sys, pin total = 0 info = file("inscount.out", "w") def counter(trace_addr): global total x = pin.TRACE_BblHead(trace_addr) y = pin.BBL_Address(x) instrucs = pin.BBL_NumIns(x) total += instrucs info.write("Basic Block @ %x SIZE: %x NUM INS= IN BLOCK: %x TOTAL: %xn" % (y, pin.BBL_Size (x), instrucs, total )) pin.TRACE_AddInstrumentFunction(counter)
  • 13. $ pin -t obj-intel64/Python_Pin.so -m ins_count.py -- /bin/ls $ cat inscount.out|head Basic Block @ 7ffff7ddb2d0 SIZE: 8 NUM INS= IN BLOCK: 2 TOTAL: 2 Basic Block @ 7ffff7ddea40 SIZE: 55 NUM INS= IN BLOCK: 16 TOTAL: 18 Basic Block @ 7ffff7ddeaef SIZE: 6 NUM INS= IN BLOCK: 2 TOTAL: 1a Basic Block @ 7ffff7ddead8 SIZE: 17 NUM INS= IN BLOCK: 6 TOTAL: 20
  • 14. Heap_Tracer.py ? ● 90 lines of code. ( Hurray! ) ● Let’s take a look. ● Demo2