Weitere ähnliche Inhalte Ähnlich wie Binary patching of Java classes for fun and profit - Jfokus 2011 (18) Mehr von Anton Arhipov (20) Kürzlich hochgeladen (20) Binary patching of Java classes for fun and profit - Jfokus 20114. 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) 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(); } } 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)); 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(…); Hinweis der Redaktion 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 Agent implementation: any class with premain, Agent-Class in manifest Agent implementation: any class with premain, Agent-Class in manifest Agent implementation: any class with premain, Agent-Class in manifest Add smiley face Add smiley face