SlideShare ist ein Scribd-Unternehmen logo
1 von 27
Binary patching of Java classes for funand profit Jfokus 2011, Stockholm
whoami Anton Arhipov ZeroTurnaround JRebel http://arhipov.blogspot.com @antonarhipov @javarebel
What’s Binary Patching? Ninja.class’ Ninja.class 1010101010111000101010101010100010001000111011011101011 1010101010111100001010101010100010001000111011011101110
Why Binary Patching? ClassLoader MyClass.class JRebel agent New code: 10010100100100101011001010 MyObject
Why Binary Patching? ClassLoader MyClass.class Framework JRebel agent New code: 10010100100100101011001010 MyObject
Why Binary Patching? ClassLoader MyClass.class Framework JRebel agent New code: 10010100100100101011001010 MyObject Configuration (XML, annotations)
How? Using –javaagentto hook into class loading process Using bytecode manipulation libraries (e.g. Javassist)
java.lang.instrument importjava.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String args, Instrumentationinst) throws Exception { inst.addTransformer(new ClassFileTransformer { … });   } }
java.lang.instrument importjava.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String args, Instrumentationinst) throws Exception { inst.addTransformer(new ClassFileTransformer { … });   } } META-INF/MANIFEST.MF Agent-Class: Agent
java.lang.instrument importjava.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String args, Instrumentationinst) throws Exception { inst.addTransformer(new ClassFileTransformer { … });   } } META-INF/MANIFEST.MF Agent-Class: Agent java –javaagent:agent.jar …
java.lang.instrument + Javassist newClassFileTransformer() { public byte[] transform(ClassLoaderloader, String className, Class<?>classBeingRedefined, ProtectionDomainprotectionDomain, byte[] classfileBuffer){    } }
java.lang.instrument + Javassist newClassFileTransformer() { public byte[] transform(ClassLoaderloader, String className, Class<?>classBeingRedefined, ProtectionDomainprotectionDomain, byte[] classfileBuffer){ ClassPoolcp= ClassPool.getDefault(); CtClassct= pool.makeClass(new ByteArrayInputStream(classfileBuffer));    } }
java.lang.instrument + Javassist newClassFileTransformer() { public byte[] transform(ClassLoaderloader, String className, Class<?>classBeingRedefined, ProtectionDomainprotectionDomain, byte[] classfileBuffer){ ClassPoolcp= ClassPool.getDefault(); CtClassct= pool.makeClass(new ByteArrayInputStream(classfileBuffer)); transformClass(ct, cp);    } }
java.lang.instrument + Javassist newClassFileTransformer() { public byte[] transform(ClassLoaderloader, String className, Class<?>classBeingRedefined, ProtectionDomainprotectionDomain, byte[] classfileBuffer){ ClassPoolcp= ClassPool.getDefault(); CtClassct = pool.makeClass(new ByteArrayInputStream(classfileBuffer)); transformClass(ct, cp);  returnct.toBytecode();    } }
JRebel SDK
Javassist + JRebel cp.importPackage("org.zeroturnaround.javarebel");
Javassist + JRebel cp.importPackage("org.zeroturnaround.javarebel"); ct.addInterface( cp.get(ClassEventListener.class.getName()));
Javassist + JRebel cp.importPackage("org.zeroturnaround.javarebel"); ct.addInterface( cp.get(ClassEventListener.class.getName())); ct.addMethod(CtNewMethod.make( "public void onClassEvent(inteventType, Class clazz) {"+      "cache.evict();" + "}", ct));
Javassist + JRebel CtClassct = … CtConstructor[] cs = ct.getConstructors();
Javassist + JRebel CtClassct = … CtConstructor[] cs = ct.getConstructors(); for (CtConstructor c : cs) {   if(c.callsSuper()) { c.insertAfter("ReloaderFactory.getInstance()                .addClassReloadListener($0);");   } }
Javassist + JRebel CtClassct = … ct.getDeclaredMethod("service") .insertBefore( "ReloaderFactory.getInstance()        .checkAndReload(Application.class);");
Integration Highlights Implement ClassEventListener Register listener instance to JRebel Trigger the re-load event ReloaderFactory#addClassReloadListener(…); ReloaderFactory#checkAndReload(…);
Pros/Cons
Pros/Cons
Pros/Cons
Thx! Want JRebel free?  Come to our booth!

Weitere ähnliche Inhalte

Was ist angesagt?

Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's GuideEvolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Victor Rentea
 

Was ist angesagt? (14)

Definitive Guide to Working With Exceptions in Java
Definitive Guide to Working With Exceptions in JavaDefinitive Guide to Working With Exceptions in Java
Definitive Guide to Working With Exceptions in Java
 
The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable Design
 
The Proxy Fairy, and The Magic of Spring Framework
The Proxy Fairy, and The Magic of Spring FrameworkThe Proxy Fairy, and The Magic of Spring Framework
The Proxy Fairy, and The Magic of Spring Framework
 
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's GuideEvolving a Clean, Pragmatic Architecture - A Craftsman's Guide
Evolving a Clean, Pragmatic Architecture - A Craftsman's Guide
 
Selenium web driver | java
Selenium web driver | javaSelenium web driver | java
Selenium web driver | java
 
Clean Code with Java 8 - Functional Patterns and Best Practices
Clean Code with Java 8 - Functional Patterns and Best PracticesClean Code with Java 8 - Functional Patterns and Best Practices
Clean Code with Java 8 - Functional Patterns and Best Practices
 
Spring @Transactional Explained
Spring @Transactional ExplainedSpring @Transactional Explained
Spring @Transactional Explained
 
Integration testing with spring @JAX Mainz
Integration testing with spring @JAX MainzIntegration testing with spring @JAX Mainz
Integration testing with spring @JAX Mainz
 
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
Java Classes | Java Tutorial for Beginners | Java Classes and Objects | Java ...
 
JavaFX Dependency Injection with FxContainer
JavaFX Dependency Injection with FxContainerJavaFX Dependency Injection with FxContainer
JavaFX Dependency Injection with FxContainer
 
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | EdurekaJava Programming | Java Tutorial For Beginners | Java Training | Edureka
Java Programming | Java Tutorial For Beginners | Java Training | Edureka
 
Unit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of PurityUnit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of Purity
 
Functional Patterns with Java8 @Bucharest Java User Group
Functional Patterns with Java8 @Bucharest Java User GroupFunctional Patterns with Java8 @Bucharest Java User Group
Functional Patterns with Java8 @Bucharest Java User Group
 
Refactoring Asynchronous TypeScript Code
Refactoring Asynchronous TypeScript CodeRefactoring Asynchronous TypeScript Code
Refactoring Asynchronous TypeScript Code
 

Andere mochten auch (6)

Java bytecode and classes
Java bytecode and classesJava bytecode and classes
Java bytecode and classes
 
Chapter 4 - Classes in Java
Chapter 4 - Classes in JavaChapter 4 - Classes in Java
Chapter 4 - Classes in Java
 
Practical OOP In Java
Practical OOP In JavaPractical OOP In Java
Practical OOP In Java
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 

Ähnlich wie Binary patching of Java classes for fun and profit - Jfokus 2011

Wdjhit javaone-2011-aa
Wdjhit javaone-2011-aaWdjhit javaone-2011-aa
Wdjhit javaone-2011-aa
Dmitry Buzdin
 
Why Doesn't Java Has Instant Turnaround - Con-FESS 2012
Why Doesn't Java Has Instant Turnaround - Con-FESS 2012Why Doesn't Java Has Instant Turnaround - Con-FESS 2012
Why Doesn't Java Has Instant Turnaround - Con-FESS 2012
Anton Arhipov
 
Rapid java application development @ JUG.ru 25.02.2012
Rapid java application development @ JUG.ru 25.02.2012Rapid java application development @ JUG.ru 25.02.2012
Rapid java application development @ JUG.ru 25.02.2012
Anton Arhipov
 
Ten Man-Years of JavaFX: Real World Project Experiences
Ten Man-Years of JavaFX: Real World Project ExperiencesTen Man-Years of JavaFX: Real World Project Experiences
Ten Man-Years of JavaFX: Real World Project Experiences
Henrik Olsson
 
Bea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guideBea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guide
Pankaj Singh
 

Ähnlich wie Binary patching of Java classes for fun and profit - Jfokus 2011 (18)

Wdjhit javaone-2011-aa
Wdjhit javaone-2011-aaWdjhit javaone-2011-aa
Wdjhit javaone-2011-aa
 
Why Doesn't Java Has Instant Turnaround - Con-FESS 2012
Why Doesn't Java Has Instant Turnaround - Con-FESS 2012Why Doesn't Java Has Instant Turnaround - Con-FESS 2012
Why Doesn't Java Has Instant Turnaround - Con-FESS 2012
 
Rapid java application development @ JUG.ru 25.02.2012
Rapid java application development @ JUG.ru 25.02.2012Rapid java application development @ JUG.ru 25.02.2012
Rapid java application development @ JUG.ru 25.02.2012
 
Perceptrion using tensorflow
Perceptrion using tensorflowPerceptrion using tensorflow
Perceptrion using tensorflow
 
Ten Man-Years of JavaFX: Real World Project Experiences
Ten Man-Years of JavaFX: Real World Project ExperiencesTen Man-Years of JavaFX: Real World Project Experiences
Ten Man-Years of JavaFX: Real World Project Experiences
 
Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8Desenvolva plugins para o compilador do Java 8
Desenvolva plugins para o compilador do Java 8
 
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
 
Javaee6 Overview
Javaee6 OverviewJavaee6 Overview
Javaee6 Overview
 
Making The Move To Java 17 (JConf 2022)
Making The Move To Java 17 (JConf 2022)Making The Move To Java 17 (JConf 2022)
Making The Move To Java 17 (JConf 2022)
 
Scripting with Java FX - Cédric Tabin - December 2007
Scripting with Java FX - Cédric Tabin - December 2007Scripting with Java FX - Cédric Tabin - December 2007
Scripting with Java FX - Cédric Tabin - December 2007
 
Bea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guideBea weblogic job_interview_preparation_guide
Bea weblogic job_interview_preparation_guide
 
Branch by Abstraction - Software Architecture Conference 2018
Branch by Abstraction - Software Architecture Conference 2018Branch by Abstraction - Software Architecture Conference 2018
Branch by Abstraction - Software Architecture Conference 2018
 
Proper Null handling with modern java techniques
Proper Null handling with modern java techniquesProper Null handling with modern java techniques
Proper Null handling with modern java techniques
 
Con-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With JavassistCon-FESS 2015 - Having Fun With Javassist
Con-FESS 2015 - Having Fun With Javassist
 
Selenium Interview Questions and Answers | Selenium Tutorial | Selenium Train...
Selenium Interview Questions and Answers | Selenium Tutorial | Selenium Train...Selenium Interview Questions and Answers | Selenium Tutorial | Selenium Train...
Selenium Interview Questions and Answers | Selenium Tutorial | Selenium Train...
 
Java7
Java7Java7
Java7
 
Java J2EE Training in Chennai, Tambaram
Java J2EE  Training in Chennai, TambaramJava J2EE  Training in Chennai, Tambaram
Java J2EE Training in Chennai, Tambaram
 
Functional programming with Java 8
Functional programming with Java 8Functional programming with Java 8
Functional programming with Java 8
 

Mehr von Anton Arhipov

JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
Anton Arhipov
 
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloadingRiga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Anton Arhipov
 

Mehr von Anton Arhipov (20)

JavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdfJavaZone 2022 - Building Kotlin DSL.pdf
JavaZone 2022 - Building Kotlin DSL.pdf
 
Idiomatic kotlin
Idiomatic kotlinIdiomatic kotlin
Idiomatic kotlin
 
TechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервьюTechTrain 2019 - (Не)адекватное техническое интервью
TechTrain 2019 - (Не)адекватное техническое интервью
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
Devoxx Ukraine 2018 - Kotlin DSL in under an hourDevoxx Ukraine 2018 - Kotlin DSL in under an hour
Devoxx Ukraine 2018 - Kotlin DSL in under an hour
 
GeeCON Prague 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hourGeeCON Prague 2018 - Kotlin DSL in under an hour
GeeCON Prague 2018 - Kotlin DSL in under an hour
 
Build pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSLBuild pipelines with TeamCity and Kotlin DSL
Build pipelines with TeamCity and Kotlin DSL
 
Build pipelines with TeamCity
Build pipelines with TeamCityBuild pipelines with TeamCity
Build pipelines with TeamCity
 
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainersJavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
 
GeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainersGeeCON Prague 2017 - TestContainers
GeeCON Prague 2017 - TestContainers
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
 
JavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassleJavaOne 2017 - TestContainers: integration testing without the hassle
JavaOne 2017 - TestContainers: integration testing without the hassle
 
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloadingJavaOne 2017 - The hitchhiker’s guide to Java class reloading
JavaOne 2017 - The hitchhiker’s guide to Java class reloading
 
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloadingJavaZone 2017 - The Hitchhiker’s guide to Java class reloading
JavaZone 2017 - The Hitchhiker’s guide to Java class reloading
 
JUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentationJUG.ua 20170225 - Java bytecode instrumentation
JUG.ua 20170225 - Java bytecode instrumentation
 
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloadingRiga DevDays 2017 - The hitchhiker’s guide to Java class reloading
Riga DevDays 2017 - The hitchhiker’s guide to Java class reloading
 
GeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassleGeeCON 2017 - TestContainers. Integration testing without the hassle
GeeCON 2017 - TestContainers. Integration testing without the hassle
 
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloadingJEEConf 2017 - The hitchhiker’s guide to Java class reloading
JEEConf 2017 - The hitchhiker’s guide to Java class reloading
 
JEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with JavassistJEEConf 2017 - Having fun with Javassist
JEEConf 2017 - Having fun with Javassist
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
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
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
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)
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Binary patching of Java classes for fun and profit - Jfokus 2011

  • 1.
  • 2. Binary patching of Java classes for funand profit Jfokus 2011, Stockholm
  • 3. whoami Anton Arhipov ZeroTurnaround JRebel http://arhipov.blogspot.com @antonarhipov @javarebel
  • 4. What’s Binary Patching? Ninja.class’ Ninja.class 1010101010111000101010101010100010001000111011011101011 1010101010111100001010101010100010001000111011011101110
  • 5. Why Binary Patching? ClassLoader MyClass.class JRebel agent New code: 10010100100100101011001010 MyObject
  • 6. Why Binary Patching? ClassLoader MyClass.class Framework JRebel agent New code: 10010100100100101011001010 MyObject
  • 7. Why Binary Patching? ClassLoader MyClass.class Framework JRebel agent New code: 10010100100100101011001010 MyObject Configuration (XML, annotations)
  • 8. How? Using –javaagentto hook into class loading process Using bytecode manipulation libraries (e.g. Javassist)
  • 9. java.lang.instrument importjava.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String args, Instrumentationinst) throws Exception { inst.addTransformer(new ClassFileTransformer { … }); } }
  • 10. java.lang.instrument importjava.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String args, Instrumentationinst) throws Exception { inst.addTransformer(new ClassFileTransformer { … }); } } META-INF/MANIFEST.MF Agent-Class: Agent
  • 11. java.lang.instrument importjava.lang.instrument.ClassFileTransformer; import java.lang.instrument.Instrumentation; public class Agent { public static void premain(String args, Instrumentationinst) throws Exception { inst.addTransformer(new ClassFileTransformer { … }); } } META-INF/MANIFEST.MF Agent-Class: Agent java –javaagent:agent.jar …
  • 12. java.lang.instrument + Javassist newClassFileTransformer() { public byte[] transform(ClassLoaderloader, String className, Class<?>classBeingRedefined, ProtectionDomainprotectionDomain, byte[] classfileBuffer){ } }
  • 13. java.lang.instrument + Javassist newClassFileTransformer() { public byte[] transform(ClassLoaderloader, String className, Class<?>classBeingRedefined, ProtectionDomainprotectionDomain, byte[] classfileBuffer){ ClassPoolcp= ClassPool.getDefault(); CtClassct= pool.makeClass(new ByteArrayInputStream(classfileBuffer)); } }
  • 14. java.lang.instrument + Javassist newClassFileTransformer() { public byte[] transform(ClassLoaderloader, String className, Class<?>classBeingRedefined, ProtectionDomainprotectionDomain, byte[] classfileBuffer){ ClassPoolcp= ClassPool.getDefault(); CtClassct= pool.makeClass(new ByteArrayInputStream(classfileBuffer)); transformClass(ct, cp); } }
  • 15. java.lang.instrument + Javassist newClassFileTransformer() { public byte[] transform(ClassLoaderloader, String className, Class<?>classBeingRedefined, ProtectionDomainprotectionDomain, byte[] classfileBuffer){ ClassPoolcp= ClassPool.getDefault(); CtClassct = pool.makeClass(new ByteArrayInputStream(classfileBuffer)); transformClass(ct, cp); returnct.toBytecode(); } }
  • 17. Javassist + JRebel cp.importPackage("org.zeroturnaround.javarebel");
  • 18. Javassist + JRebel cp.importPackage("org.zeroturnaround.javarebel"); ct.addInterface( cp.get(ClassEventListener.class.getName()));
  • 19. Javassist + JRebel cp.importPackage("org.zeroturnaround.javarebel"); ct.addInterface( cp.get(ClassEventListener.class.getName())); ct.addMethod(CtNewMethod.make( "public void onClassEvent(inteventType, Class clazz) {"+ "cache.evict();" + "}", ct));
  • 20. Javassist + JRebel CtClassct = … CtConstructor[] cs = ct.getConstructors();
  • 21. Javassist + JRebel CtClassct = … CtConstructor[] cs = ct.getConstructors(); for (CtConstructor c : cs) { if(c.callsSuper()) { c.insertAfter("ReloaderFactory.getInstance() .addClassReloadListener($0);"); } }
  • 22. Javassist + JRebel CtClassct = … ct.getDeclaredMethod("service") .insertBefore( "ReloaderFactory.getInstance() .checkAndReload(Application.class);");
  • 23. Integration Highlights Implement ClassEventListener Register listener instance to JRebel Trigger the re-load event ReloaderFactory#addClassReloadListener(…); ReloaderFactory#checkAndReload(…);
  • 27. Thx! Want JRebel free? Come to our booth!

Hinweis der Redaktion

  1. The what/why/how of runtime binary patching of JAVA applications, frameworks and application servers to provide custom integration. We will follow the JRebel integration story from dirty bytecode insertions to readable Java code patches. The session is intended for advanced Java developers interested in patching or integrating with binary java applications. We will look at the different problems that arise when doing runtime binary patching of different Java applications and frameworks. * What and why is a binary patching? * Tools, methods and tips. * Sample use-cases of binary patching different systems * Pros/cons
  2. Agent implementation: any class with premain, Agent-Class in manifest
  3. Agent implementation: any class with premain, Agent-Class in manifest
  4. Agent implementation: any class with premain, Agent-Class in manifest
  5. Add smiley face
  6. Add smiley face