SlideShare a Scribd company logo
JAVA
SH3LL C0DE

EXECUTION
Me
Me
Ryan Wincey
-Security Researcher
-Coder
-Penetration Tester
WHY JAVA
WHY JAVA
WHY JAVA
•CROSSPLATFORM
WHY JAVA
•CROSSPLATFORM
•EXTENSIVE
LIBRARY
WHY JAVA
•CROSSPLATFORM
•EXTENSIVE
LIBRARY
•WIDELY
DEPLOYED
PROBLEM:
PROBLEM:
HOW CAN I RUN
SHELLCODE FROM JAVA?
#NOPS
char shellcode[] = "x90x90x90x90”
int main(int argc, char **argv){
int (*f)();
f = (int (*)())shellcode;
(int)(*f)();
}
http://blog.strategiccyber.com/2013/08/29/how-to-inject-shellcode-from-java/
https://github.com/schierlm/JavaPayload.
package javapayload.stage;
/*
* Running shellcode from Java
without JNI (i. e. loading a DLL
from disk).
(c) 2011 Michael Schierl <schierlm
at gmx dot de> (Twitter @mihi42)
* This version has been tested on:
*
* Oracle 1.4.2_11 Win32 (-client, -server)
* Oracle 1.5.0_06 Win32 (-client, -server)
* Oracle 1.6.0_19 Win32 (-client, -server)
* Oracle 1.7.0_01 Win32 (-client, -server)
*
* Oracle 1.6.0_26 Linux32 (-client, -server)
* Oracle 1.7.0_01 Linux32 (-client, -server)
Java Shellcode Execution
http://www.research.ibm.com/trl/projects/jit/index_e.htm
Java Shellcode Execution
Java Shellcode Execution
Java Shellcode Execution
Java Shellcode Execution
Java Shellcode Execution
Java Shellcode Execution
DEMO
//===========================================================================
/**
* Java method that gets overwritten by shellcode
* The native method pointer is then overwritten with a
* pointer to this method
*/
private static void jitme() {
executed = true;
// On x86: each volatile inc/dec needs 18 bytes,
// all 320 of them need 5760 bytes,
// whole JIT method needs 5842 bytes.
// if you need more shellcode, make a longer method
v1++; v2++; v3++; v4++; v5++;
v1++; v2++; v3++; v4++; v5--;
v1++; v2++; v3++; v4--; v5++;
v1++; v2++; v3++; v4--; v5--;
v1++; v2++; v3--; v4++; v5++;
v1++; v2++; v3--; v4++; v5--;
v1++; v2++; v3--; v4--; v5++;
v1++; v2++; v3--; v4--; v5--;
v1++; v2--; v3++; v4++; v5++;
v1++; v2--; v3++; v4++; v5--;
v1++; v2--; v3++; v4--; v5++;
v1++; v2--; v3++; v4--; v5--;
v1++; v2--; v3--; v4++; v5++;
v1++; v2--; v3--; v4++; v5--;
v1++; v2--; v3--; v4--; v5++;
v1++; v2--; v3--; v4--; v5--;
executed = true;
DEMO
2
Questions?
Contacts
Ryan Wincey @rwincey
winceyr@gmail.com
https://github.com/rwincey
Michael Schierl @mihi42
https://github.com/schierlm/JavaPayload

More Related Content

What's hot (20)

PPT
Java Basics
Sunil OS
 
PPTX
Packages,static,this keyword in java
Vishnu Suresh
 
PDF
VMの歩む道。 Dalvik、ART、そしてJava VM
yy yank
 
PDF
Idiomatic Kotlin
intelliyole
 
PDF
使用 Pytest 進行單元測試 (PyCon TW 2021)
Max Lai
 
PDF
Kotlin for Android Development
Speck&Tech
 
PPT
JUnit 4
Sunil OS
 
PPTX
Clean Code
Dmytro Turskyi
 
PDF
Modern c++ (C++ 11/14)
Geeks Anonymes
 
PDF
Inheritance and interface
Shubham Sharma
 
PPTX
Php internal architecture
Elizabeth Smith
 
PDF
動的なILの生成と編集
terurou
 
PDF
Clean Lambdas & Streams in Java8
Victor Rentea
 
PPTX
C++ memory leak detection
Võ Hòa
 
PDF
OOPs & Inheritance Notes
Shalabh Chaudhary
 
PPSX
OOP with Java - Part 3
Hitesh-Java
 
ODP
Java 9 Features
NexThoughts Technologies
 
KEY
JavaOne 2011 - JVM Bytecode for Dummies
Charles Nutter
 
PPSX
C# - Part 1
Md. Mahedee Hasan
 
PPTX
Inheritance In Java
Darpan Chelani
 
Java Basics
Sunil OS
 
Packages,static,this keyword in java
Vishnu Suresh
 
VMの歩む道。 Dalvik、ART、そしてJava VM
yy yank
 
Idiomatic Kotlin
intelliyole
 
使用 Pytest 進行單元測試 (PyCon TW 2021)
Max Lai
 
Kotlin for Android Development
Speck&Tech
 
JUnit 4
Sunil OS
 
Clean Code
Dmytro Turskyi
 
Modern c++ (C++ 11/14)
Geeks Anonymes
 
Inheritance and interface
Shubham Sharma
 
Php internal architecture
Elizabeth Smith
 
動的なILの生成と編集
terurou
 
Clean Lambdas & Streams in Java8
Victor Rentea
 
C++ memory leak detection
Võ Hòa
 
OOPs & Inheritance Notes
Shalabh Chaudhary
 
OOP with Java - Part 3
Hitesh-Java
 
Java 9 Features
NexThoughts Technologies
 
JavaOne 2011 - JVM Bytecode for Dummies
Charles Nutter
 
C# - Part 1
Md. Mahedee Hasan
 
Inheritance In Java
Darpan Chelani
 

Viewers also liked (20)

ODP
Design and implementation_of_shellcodes
Amr Ali
 
PPTX
07 - Bypassing ASLR, or why X^W matters
Alexandre Moneger
 
PDF
Anatomy of A Shell Code, Reverse engineering
Abhineet Ayan
 
PDF
Efficient Bytecode Analysis: Linespeed Shellcode Detection
Georg Wicherski
 
PDF
Linux Shellcode disassembling
Harsh Daftary
 
PDF
Shellcode and heapspray detection in phoneyc
Z Chen
 
PPTX
05 - Bypassing DEP, or why ASLR matters
Alexandre Moneger
 
PPTX
Exploit Research and Development Megaprimer: Unicode Based Exploit Development
Ajin Abraham
 
PDF
Rooting Your Internals: Inter-Protocol Exploitation, custom shellcode and BeEF
Michele Orru
 
PDF
Talking about exploit writing
sbha0909
 
PPTX
Anton Dorfman. Shellcode Mastering.
Positive Hack Days
 
PPTX
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
midnite_runr
 
PDF
Hacking school computers for fun profit and better grades short
Vincent Ohprecio
 
PDF
Shellcode Analysis - Basic and Concept
Julia Yu-Chin Cheng
 
PPTX
Exploit Research and Development Megaprimer: Win32 Egghunter
Ajin Abraham
 
PDF
One Shellcode to Rule Them All: Cross-Platform Exploitation
Quinn Wilton
 
PPT
Software Exploits
KevinCSmallwood
 
PDF
Shellcode injection
Dhaval Kapil
 
PPT
Writing Metasploit Plugins
amiable_indian
 
PDF
Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...
Michele Orru
 
Design and implementation_of_shellcodes
Amr Ali
 
07 - Bypassing ASLR, or why X^W matters
Alexandre Moneger
 
Anatomy of A Shell Code, Reverse engineering
Abhineet Ayan
 
Efficient Bytecode Analysis: Linespeed Shellcode Detection
Georg Wicherski
 
Linux Shellcode disassembling
Harsh Daftary
 
Shellcode and heapspray detection in phoneyc
Z Chen
 
05 - Bypassing DEP, or why ASLR matters
Alexandre Moneger
 
Exploit Research and Development Megaprimer: Unicode Based Exploit Development
Ajin Abraham
 
Rooting Your Internals: Inter-Protocol Exploitation, custom shellcode and BeEF
Michele Orru
 
Talking about exploit writing
sbha0909
 
Anton Dorfman. Shellcode Mastering.
Positive Hack Days
 
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
midnite_runr
 
Hacking school computers for fun profit and better grades short
Vincent Ohprecio
 
Shellcode Analysis - Basic and Concept
Julia Yu-Chin Cheng
 
Exploit Research and Development Megaprimer: Win32 Egghunter
Ajin Abraham
 
One Shellcode to Rule Them All: Cross-Platform Exploitation
Quinn Wilton
 
Software Exploits
KevinCSmallwood
 
Shellcode injection
Dhaval Kapil
 
Writing Metasploit Plugins
amiable_indian
 
Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...
Michele Orru
 
Ad

Similar to Java Shellcode Execution (7)

PPTX
GOTO Night with Charles Nutter Slides
Alexandra Masterson
 
PPTX
Java is a new language on the mainframe
Michael Erichsen
 
PDF
Beyond JVM - YOW Melbourne 2013
Charles Nutter
 
PPTX
Java JIT Performance Testing and Results
Adam Feldscher
 
PPTX
The latest features coming to Java 12
NexSoftsys
 
PDF
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
chen yuki
 
PDF
Beyond JVM - YOW! Brisbane 2013
Charles Nutter
 
GOTO Night with Charles Nutter Slides
Alexandra Masterson
 
Java is a new language on the mainframe
Michael Erichsen
 
Beyond JVM - YOW Melbourne 2013
Charles Nutter
 
Java JIT Performance Testing and Results
Adam Feldscher
 
The latest features coming to Java 12
NexSoftsys
 
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
chen yuki
 
Beyond JVM - YOW! Brisbane 2013
Charles Nutter
 
Ad

Recently uploaded (20)

PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PPTX
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
PDF
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
OpenID AuthZEN - Analyst Briefing July 2025
David Brossard
 
Reverse Engineering of Security Products: Developing an Advanced Microsoft De...
nwbxhhcyjv
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
DevBcn - Building 10x Organizations Using Modern Productivity Metrics
Justin Reock
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 

Java Shellcode Execution

Editor's Notes

  • #2: REMEMBER TO USE PRESENTER VIEW
  • #4: I’m Ryan Wincey, I’m a developer and pentration tester. This is my first presentation on.. Pretty much Anything. So bare with me. It might be a little dry, and technical.I’m going to give you a little of my background to provide some back story on my talk today. I’ve been writing code professionally for about 6 years, with a primary focus on enterprise level remote system administration. The target operating environment for the software projects I’ve been a part of has been mixed, with mostly Windows systems and a handful of Solaris and Linux systems. Given the diversity in operating systems and architecture, Java was our programming language of choice.So with my current job, I’m not developing full time anymore, but I do write a fair amount of code to aid in pentesting and maintaining persistence on computers that we compromise. So once again I’ve found myself in the situation where I have a mixed target base that my software has to run on. So I decided to go with what I was used to, and write tools using Java.
  • #5: So why Java.
  • #6: So why Java. I definetly found the people on the internet that have a distaste for it.
  • #7: It runs on just about everything. Phones, Tablets, Laptops, Desktops, Servers, u name it.
  • #8: I’m super lazy and don’t want to have to write anymore code than I have to. Tons of resources online to borrow code snippets…etc.
  • #9: Sun’s claims “3 billion devices” have Java installed on it. Good chance it will be on a system that you are targeting. It also blends in well because all java programs look the same from the task manager. People see Java running, they probably don’t think much of it.
  • #10: So let’s fast forward to the topic of this talk, I found myself with a assortment of Java based security tools, some I’ve written, some open-source. Then I came across a pretty serious limitation I was used to be able to do with native languages.
  • #11: How do I run shellcode directly from a Java application. So just as a quick refresher, shellcode is pretty much compiled native code that can be injected directly into memory and executed. I’d say it’s most common use is for software exploitation … but it can also be used to load code into a program without that code ever being written to disk, Which can be pretty important when you are trying to avade an antivirus.Searching the Internet didn’t provideIn C, this is a pretty trivial exercise
  • #12: In C, this is a pretty trivial exercise as shown aboveAfter searching the Internet, I can up with a couple options. The first technique involves writing a native library in ( C/C++) and using either JNI (Java Native Interface) or JNA ( Java Native Access ) to pass the shellcode from Java to the DLL to execute it. This technique is rather clunky since a DLL has to be transported alongside the Java code and also adds one more artifact on disk that a virus signature could be made for.https://github.com/schierlm/JavaPayload
  • #13: After searching the Internet, I came up with a couple options. The first technique involves writing a native library in ( C/C++) and using either JNI (Java Native Interface) or JNA ( Java Native Access ) to pass the shellcode from Java to the DLL to execute it. This technique is rather clunky since the DLL has to be transported alongside the Java code and also adds an additional artifact on disk that a virus signature could be created for.
  • #14: The other option I came across was buried in a project on SourceForge called JavaPayload written by Michael Schierl. After perusing the source code a little bit I found a class called JITShellCodeRunnerhttp://javapayload.sourceforge.net/, also located on GitHub at https://github.com/schierlm/JavaPayload. I read
  • #15: Inside the JavaPayload/payload/stage package I found a class named JITShellCodeRunner that had these comments at the top. I downloaded the source and executed the calculator executing shellcode on my machine running the latest JVM from Oracle. Unfortunately all I got JVM crash.
  • #16: I read through more of the comments and noticed that the most recent JVM the code had been tested on was Java 1.7 update 1. So I downloaded Java 7 update 1 dated (Oct 18, 2011) and upon execution I got this. Success.
  • #17: Success. A welcome sight for those that have much experience writing exploits. Now I knew that this voodoo was possible, I tried to dive deeper into the inner workings of this class so I could try and get it to work across all JVMs. The comments in the code yielded very little insight into what was actually going on so I emailed Mr. Schierl hoping for an explanation. Mr. Schierl was very helpful and explained in detail what he had done.
  • #18: Just to give a little refresher, Java is an interpreted language that compiles Java instructions into byte code. Byte code is then interpreted by the JVM into native instructions and executed on a system. To increase performance, the Just-In-Time (JIT) compiler inside the JVM will compile frequently used methods from byte code into native code on the fly, and cache them in memory.
  • #19: So now we know that the JVM not only executes byte code, but also caches natively compiled shellcode. If only we could find a way to replace the native code compiled by the JIT, with some of our own, and then execute it. Turns out there is, and Java provides it, Kinda.
  • #20: The Unsafe class. The Unsafe class is a Sun proprietary class that can be used to access memory directly. As the name suggest, it is extremely unsafe to use because any native exceptions that are raised while trying to access memory cannot be caught, and will cause the JVM to crash. The generalprocess for locating the a method’s native pointer consist of walking the memory structure of the Java Class instance using the Unsafe class functions. The original algorithm that was implemented by Mr. Schierl begins by first obtaining the base address of a static field in the Class
  • #21: That pointer references another structure that contains a pointer to an array of all of the methods in the class. The arrows point at the fields surrounding the method array pointer that act as indicators for locating the correct pointer.
  • #22: Each pointer in that array, then points to a structure that contains a pointer to a structure representing the native function.
  • #23: Lastly the native function structure contains the pointer to the entry point for the JIT compiled code. Once this pointer has been located, the shellcode can be inserted into native memory space using the Unsafe class. All that’s left is to call the method.Now for the fun part, I started exploring why this novel technique didn’t seem to work anymore. I found that there are several inconsistencies in the class layout across the various architectures and releases that would make it difficult to make a comprehensive solution. Although the sequence of fields in a class appears to be fairly consistent, the offsets to the fields tended to change across JVM releases. In all likely-hood this because new fields or members are being added or removed to the underling classes. Another compatibility issue was the introduction of compressed pointers in the 64bit JVMs that required an additional call to the Unsafe class to decompress the pointer if it was compressed.The most significant issue I found was the realignment of the base memory object starting in Java 1.7. Since this is the first step in the process, choosing the wrong initial pointer throws off the rest of the memory walk. What makes this significant is an incorrect offset into the class memory can cause the JVM to crash since it will cause a native exception to be thrown.
  • #24: To mitigate this problem, I added code to brute force the initial offset by spawning a separate JVM for each attempt. This technique protects the original JVM while the correct offset is located.
  • #26: Just a few technical notes, for those that might look at this code later. The java method who’s native code is replaced, is filled with what appear to be a arbitrary set of operations. However these operations are responsible for the size of memory that is allocated for the native code produced when it is JIT compiled. The operations are sequenced in this manner so that the operations will not be optimized when the Java code is compiled.Another caveat that needs attention when using this technique is to properly align the stack after running the desired shellcode. If this is not done, the JVM will crash. This might not be a concern but it should be noted.
  • #27: In case I had time I wanted to show what kinds of things can be done with this technique to extend some known penetration tools.
  • #28: Each pointer in that array, then points to a structure that contains a pointer to a structure for its JIT compiler code.
  • #29: For those that want to try the code out, I’ve sent my updates to Mr. Schierl to merge into the JavaPayload project, but also plan on putting the updated code on my Github this weekend.Thanks for letting me come talk about everything. I wanted to thank Micheal Schierl for the great research and for being an expert reference on how this stuff works.Also wanted to thank my boss for going through the hassle of getting this talk approved to be presented.