Reversing Java (Malware) with Radare Adam Pridgen April 2014 About - - PowerPoint PPT Presentation

reversing java malware with radare
SMART_READER_LITE
LIVE PREVIEW

Reversing Java (Malware) with Radare Adam Pridgen April 2014 About - - PowerPoint PPT Presentation

Reversing Java (Malware) with Radare Adam Pridgen April 2014 About me Rice SecLab, a PhD Student Independent InfoSec Consultant/Contractor Overview Typical Java Reversing Talk o Decompile Code o Make Changes o Recompile and Win?


slide-1
SLIDE 1

Reversing Java (Malware) with Radare

Adam Pridgen April 2014

slide-2
SLIDE 2
  • Rice SecLab, a PhD Student
  • Independent InfoSec Consultant/Contractor

About me

slide-3
SLIDE 3

Overview

  • Typical Java Reversing Talk
  • Decompile Code
  • Make Changes
  • Recompile and Win?
  • Java Malware: Fail!
slide-4
SLIDE 4

Overview

Has this happened to you?

slide-5
SLIDE 5

Overview

  • IDA Pro 6.4 does not include meta-data
slide-6
SLIDE 6

Overview

  • Malicious code analysis is hard
  • Relevant information is key
  • Tools assume code is complete or correct
slide-7
SLIDE 7
  • Reversing JVM Bytecode viewed as a “simple” problem

○ Until you need to actually do it ○ Or you need to extract some type of information

  • Too Long Didn’t Listen (tldl;)

○ Radare now supports basic class file manipulations ○ Hooking by rewriting class and method names ○ Manipulation of Access Flags ○ Inserting values in constant pool ○ More detailed inspection of files

Overview

slide-8
SLIDE 8

Multiple Architectures Command Based Open Source

2048

GDB Interface Hex Editor Supports IO Layers Extendible Components Multi-Language w/ Ctypes IL in progress

Cross Platform

Web UI

slide-9
SLIDE 9

Agenda

  • Discuss Java Class File and Format
  • Discuss Java Malware and Obfuscation
  • Introduce Java Reversing with Radare
  • Discuss Some Techniques
  • Conclude with Future Work
slide-10
SLIDE 10

Java Overview

slide-11
SLIDE 11

JVM Bytecode

  • ~203 Operations
  • Fairly easy to disassemble
  • Except for the built in “switch-tables”
  • JVM is Stack Based
  • Local Variables are stored in a local variable

position

slide-12
SLIDE 12

JVM Bytecode

  • Caller copy the entire thread stack to caller
  • JVM resolves Class Name, Method Name,

and argument types

  • Types are not important until they are

important

slide-13
SLIDE 13

Java Malware Obfuscation

  • Static Obfuscation Techniques
  • Dynamic Techniques
slide-14
SLIDE 14

Java Malware via Static Obfuscation

  • Flatten Classes and Package Hierarchy
  • Homogenous type signatures
  • Make class names uninterpretable
  • Exploit compiler features
  • Dead code
  • Local variable Type overloading
  • Hiding strings or files in strange places
slide-15
SLIDE 15

Java Malware via Dynamic Obfuscation

  • Reflection or Custom Class loaders
  • Starting a new process
  • Scripting Engine
  • String Manipulation
  • Encryptions
slide-16
SLIDE 16

Java Malware Reversing

  • Not easily decompilable (if at all)
  • No standard tools for inspections
  • Modification is tedious to do by hand
slide-17
SLIDE 17

What Radare can do with Java?

  • Basic hooking of class methods
  • Change constant pool Values
  • Modify method and field access flags
  • Disassemble code
  • Load classes from strings
  • Open the JAR and view all the files
  • Yank a file to disk or insert it in the JAR
slide-18
SLIDE 18

Class File Organization

slide-19
SLIDE 19

Class File Organization

slide-20
SLIDE 20

Class File Organization

  • Magic Bytes
  • Version Information
slide-21
SLIDE 21

Class File Organization

  • Constant Values

○ Long, Integers ○ Float, Doubles ○ Strings

  • Class Definitions
  • Field Definitions
  • Method Definitions
slide-22
SLIDE 22

Class File Organization

  • Omitted, but worth

Mentioning

  • Class Definition
  • Super Class Info
slide-23
SLIDE 23

Class File Organization

  • Interface

Information

slide-24
SLIDE 24

Class File Organization

  • Access Flags
  • Name and Description
  • Attributes

○ Runtime Annotations ○ Constant Value

slide-25
SLIDE 25

Class File Organization

  • Access Flags
  • Name and Description
  • Attributes

○ Runtime Annotations ○ Code & Exceptions ○ Stack Map Table ○ Local Variable Tables ○ Inner Classes ○ ...

slide-26
SLIDE 26

Class File Organization

  • Class File Attributes

○ Runtime Annotations

○ Source File ○ User defined ○ ...

slide-27
SLIDE 27

Hooking Java Methods

  • Easiest all references to a class
  • Write an implementation that wraps the target class
  • Rewrite all of the strings
  • Modify access flags
  • Put the class in the class path
  • Run the JAR File
slide-28
SLIDE 28

Hooking the Easy Way

Swap StringBuilder with sb class

slide-29
SLIDE 29

Hooking the Easy Way

Swap StringBuilder with sb class

slide-30
SLIDE 30

Hooking the Easy Way

Swap StringBuilder with sb class

slide-31
SLIDE 31

Hooking the Easy Way

ClassNotFound exception: 1

slide-32
SLIDE 32

Hooking the Easy Way

ClassNotFound exception: 2.

slide-33
SLIDE 33

Hooking the Easy Way

Copy classes to path and it works.

slide-34
SLIDE 34

Hooking the Easy Way

Wrapper classes

slide-35
SLIDE 35

Hooking Java Methods +1 Complexity

  • Insert CP Objects
  • Append the CP Objects to define the new class
  • Class Info, Method Info, and Descriptor Info
  • Update the CP Object Counts
  • Modify code section and update the reference
  • Put the class in the class path
  • Run the JAR File
slide-36
SLIDE 36

Primer Constant Pool Definition

class FooClass { String getItMethod (); }

slide-37
SLIDE 37

Assume tag idx = 2

Primer Constant Pool Definition

slide-38
SLIDE 38

Constant Pool Definition

Resolving the Class Name: FooClass

slide-39
SLIDE 39

Constant Pool Definition

Resolving the Method Name: getItMethod

slide-40
SLIDE 40

Constant Pool Definition

Resolving the Method Type: ()Ljava/lang/String;

slide-41
SLIDE 41

Constant Pool Definition

class FooClass { String getItMethod (); }

slide-42
SLIDE 42

Hooking Java Methods ++1 Complexity

  • Direct code insertion
  • Extend the code section attribute
  • Update attribute size
  • Modify code section and insert the code
  • Update the exception handling table
slide-43
SLIDE 43

Changing Access Flags

Target Java Function: exploitAnnotations

slide-44
SLIDE 44

Changing Access Flags

Insight is good, note the flag values.

slide-45
SLIDE 45

Changing Access Flags

Apply some Radare Magic Sauce

slide-46
SLIDE 46

Changing Access Flags

Here is what JD-Gui shows.

slide-47
SLIDE 47

Changing Access Flags

slide-48
SLIDE 48

Extracting jCrypt Classloader Key

List Files: zip://zip_file.whatevs Access Files with: ::[index] or //path/

slide-49
SLIDE 49

Extracting jCrypt Classloader Key

List Files: zip://zip_file.whatevs Access Files with: ::[index] or //path/

slide-50
SLIDE 50

Extracting jCrypt Classloader Key

Loading /c.dat from the archive, whats that?

slide-51
SLIDE 51

Extracting jCrypt Classloader Key

Loading /c.dat from the archive, whats that?

slide-52
SLIDE 52

Extracting jCrypt Classloader Key

slide-53
SLIDE 53

Extracting the Encrypted JAR File

slide-54
SLIDE 54

Using Prototypes

slide-55
SLIDE 55

Using Prototypes

slide-56
SLIDE 56

Using Prototypes

slide-57
SLIDE 57

Using Prototypes

a type is an Enum, created from the string this.a.z

slide-58
SLIDE 58

Using CFR Decompiler

Problems with the Exception table? [=] Lets dump it CFR Decompiler to extract Java code

slide-59
SLIDE 59

CFR Decompiler Augmentation

Use exc: ‘java exc 0x937’ Use prototypes: ‘java prototypes a’

slide-60
SLIDE 60

Future Work

  • Enable some more static conveniences
  • Tie into a JVM for run-time information
  • Enable code instrumentation via Code Attribute
  • Look at reversing native code with JVM code
  • Move on to other managed code

implementations

slide-61
SLIDE 61

Conclusion

  • Discussed some basic constructs in Java classfile
  • Introduced improvements to Radare
  • Talked about how an analyst could use them
slide-62
SLIDE 62

Questions and Contact Info

Thanks For Your Time.

email: adam.pridgen@thecoverofnight.com twitter: @apridgen github/bitbucket: deeso

slide-63
SLIDE 63

Java Reversing Tools

slide-64
SLIDE 64
slide-65
SLIDE 65

Radare Architecture

slide-66
SLIDE 66

Recent Additions to Radare

  • Testing Framework
  • Gameboy Reversing and Emulation
  • Java Support
  • Loading/reloading binaries from buffer
  • Extending (inserting bytes in the middle)
  • Opening multiple files
  • Zip URI support