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

PDF
Codeql Variant Analysis
PPTX
Nested class
PPTX
Strings in c++
PDF
file handling c++
PDF
강좌 05 통신용 PC 프로그래밍
PPT
C++ - Constructors,Destructors, Operator overloading and Type conversion
PDF
Object-oriented Programming-with C#
PDF
Semmle Codeql
Codeql Variant Analysis
Nested class
Strings in c++
file handling c++
강좌 05 통신용 PC 프로그래밍
C++ - Constructors,Destructors, Operator overloading and Type conversion
Object-oriented Programming-with C#
Semmle Codeql

What's hot (20)

PDF
spinlock.pdf
PDF
Object oriented approach in python programming
PPTX
Exception handling in Java
PPTX
Storage classes in C
PPT
pre processor directives in C
PPTX
Python OOPs
PPTX
Single inheritance
PPTX
Conditional statement in c
PPT
Lecture 1
PPTX
[OOP - Lec 19] Static Member Functions
PPT
Object Oriented Programming Concepts using Java
PPTX
Type script - advanced usage and practices
PDF
PDF
1182586869 Sr301 Kor
PPTX
Python decorators
PPTX
Typescript ppt
PDF
Strings in python
PPTX
Java Method, Static Block
PDF
Object oriented programming With C#
PDF
JavaScript - Chapter 6 - Basic Functions
spinlock.pdf
Object oriented approach in python programming
Exception handling in Java
Storage classes in C
pre processor directives in C
Python OOPs
Single inheritance
Conditional statement in c
Lecture 1
[OOP - Lec 19] Static Member Functions
Object Oriented Programming Concepts using Java
Type script - advanced usage and practices
1182586869 Sr301 Kor
Python decorators
Typescript ppt
Strings in python
Java Method, Static Block
Object oriented programming With C#
JavaScript - Chapter 6 - Basic Functions
Ad

Viewers also liked (20)

ODP
Design and implementation_of_shellcodes
PPTX
07 - Bypassing ASLR, or why X^W matters
PDF
Anatomy of A Shell Code, Reverse engineering
PDF
Efficient Bytecode Analysis: Linespeed Shellcode Detection
PDF
Linux Shellcode disassembling
PDF
Shellcode and heapspray detection in phoneyc
PPTX
05 - Bypassing DEP, or why ASLR matters
PPTX
Exploit Research and Development Megaprimer: Unicode Based Exploit Development
PDF
Rooting Your Internals: Inter-Protocol Exploitation, custom shellcode and BeEF
PDF
Talking about exploit writing
PPTX
Anton Dorfman. Shellcode Mastering.
PPTX
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
PDF
Hacking school computers for fun profit and better grades short
PDF
Shellcode Analysis - Basic and Concept
PPTX
Exploit Research and Development Megaprimer: Win32 Egghunter
PDF
One Shellcode to Rule Them All: Cross-Platform Exploitation
PPT
Software Exploits
PDF
Shellcode injection
PPT
Writing Metasploit Plugins
PDF
Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...
Design and implementation_of_shellcodes
07 - Bypassing ASLR, or why X^W matters
Anatomy of A Shell Code, Reverse engineering
Efficient Bytecode Analysis: Linespeed Shellcode Detection
Linux Shellcode disassembling
Shellcode and heapspray detection in phoneyc
05 - Bypassing DEP, or why ASLR matters
Exploit Research and Development Megaprimer: Unicode Based Exploit Development
Rooting Your Internals: Inter-Protocol Exploitation, custom shellcode and BeEF
Talking about exploit writing
Anton Dorfman. Shellcode Mastering.
Patching Windows Executables with the Backdoor Factory | DerbyCon 2013
Hacking school computers for fun profit and better grades short
Shellcode Analysis - Basic and Concept
Exploit Research and Development Megaprimer: Win32 Egghunter
One Shellcode to Rule Them All: Cross-Platform Exploitation
Software Exploits
Shellcode injection
Writing Metasploit Plugins
Rooting your internals - Exploiting Internal Network Vulns via the Browser Us...
Ad

Similar to Java Shellcode Execution (20)

PDF
Building High Performance Android Applications in Java and C++
PDF
Defending against Java Deserialization Vulnerabilities
PDF
Abusing Java Remote Interfaces
PPTX
Core java over view basics introduction by quontra solutions
PPTX
Mastering Java Bytecode - JAX.de 2012
PDF
How to reverse engineer Android applications
PDF
How to reverse engineer Android applications—using a popular word game as an ...
PDF
Automating Security Testing with the OWTF
PDF
javacourse in c.pdf
PDF
javacourse in c.pdf
PDF
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
PDF
Certifications Java
PPTX
GOTO Night with Charles Nutter Slides
PDF
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
PDF
JavaScript TDD with Jasmine and Karma
PDF
Introduction to YouDebug - Scriptable Java Debugger
PPTX
Java programing language unit 1 introduction
PDF
The Future of Security and Productivity in Our Newly Remote World
PDF
Understanding the Dalvik Virtual Machine
KEY
JavaOne 2011 - JVM Bytecode for Dummies
Building High Performance Android Applications in Java and C++
Defending against Java Deserialization Vulnerabilities
Abusing Java Remote Interfaces
Core java over view basics introduction by quontra solutions
Mastering Java Bytecode - JAX.de 2012
How to reverse engineer Android applications
How to reverse engineer Android applications—using a popular word game as an ...
Automating Security Testing with the OWTF
javacourse in c.pdf
javacourse in c.pdf
2013 syscan360 yuki_chen_syscan360_exploit your java native vulnerabilities o...
Certifications Java
GOTO Night with Charles Nutter Slides
[HES2013] Virtually secure, analysis to remote root 0day on an industry leadi...
JavaScript TDD with Jasmine and Karma
Introduction to YouDebug - Scriptable Java Debugger
Java programing language unit 1 introduction
The Future of Security and Productivity in Our Newly Remote World
Understanding the Dalvik Virtual Machine
JavaOne 2011 - JVM Bytecode for Dummies

Recently uploaded (20)

PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
NewMind AI Monthly Chronicles - July 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Encapsulation_ Review paper, used for researhc scholars
PPT
Teaching material agriculture food technology
PDF
Empathic Computing: Creating Shared Understanding
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
“AI and Expert System Decision Support & Business Intelligence Systems”
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Digital-Transformation-Roadmap-for-Companies.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
The AUB Centre for AI in Media Proposal.docx
Network Security Unit 5.pdf for BCA BBA.
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
20250228 LYD VKU AI Blended-Learning.pptx
Unlocking AI with Model Context Protocol (MCP)
Diabetes mellitus diagnosis method based random forest with bat algorithm
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
NewMind AI Monthly Chronicles - July 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
cuic standard and advanced reporting.pdf
Encapsulation_ Review paper, used for researhc scholars
Teaching material agriculture food technology
Empathic Computing: Creating Shared Understanding

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.