Skip to content

Commit

Permalink
Continue switching argparse4j implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
qtc-de committed Jan 1, 2024
1 parent b7e64e3 commit 1b943d4
Showing 1 changed file with 129 additions and 56 deletions.
185 changes: 129 additions & 56 deletions src/eu/tneitzel/rmg/operations/Dispatcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

import javax.management.remote.rmi.RMIServer;

import eu.tneitzel.argparse4j.global.IOption;
import eu.tneitzel.argparse4j.global.exceptions.RequirementException;
import eu.tneitzel.rmg.endpoints.KnownEndpoint;
import eu.tneitzel.rmg.endpoints.KnownEndpointHolder;
import eu.tneitzel.rmg.exceptions.UnexpectedCharacterException;
Expand Down Expand Up @@ -150,11 +152,21 @@ private void createMethodCandidate()
*/
public RMIEndpoint getRMIEndpoint()
{
int port = RMGOption.require(RMGOption.TARGET_PORT);
String host = RMGOption.require(RMGOption.TARGET_HOST);
try
{
int port = RMGOption.TARGET_PORT.require();
String host = RMGOption.TARGET_HOST.require();

this.createMethodCandidate();
return new RMIEndpoint(host, port);
}

catch (RequirementException e)
{
ExceptionHandler.requirementException(e);
}

this.createMethodCandidate();
return new RMIEndpoint(host, port);
return null;
}

/**
Expand Down Expand Up @@ -192,7 +204,15 @@ private RMIRegistryEndpoint getRegistry(RMIEndpoint rmi)
*/
private RemoteObjectClient getRemoteObjectClient(RMIEndpoint rmi)
{
RMGOption.requireOneOf(RMGOption.TARGET_OBJID, RMGOption.TARGET_BOUND_NAME, RMGOption.TARGET_COMPONENT);
try
{
IOption.requireOneOf(RMGOption.TARGET_OBJID, RMGOption.TARGET_BOUND_NAME, RMGOption.TARGET_COMPONENT);
}

catch (RequirementException e)
{
ExceptionHandler.requirementException(e);
}

if (RMGOption.TARGET_BOUND_NAME.isNull() && RMGOption.TARGET_OBJID.isNull())
{
Expand Down Expand Up @@ -339,10 +359,18 @@ private Set<MethodCandidate> getCandidates()
*/
public void dispatchListen()
{
String listenerIP = RMGOption.require(RMGOption.LISTEN_IP);
int listenerPort = RMGOption.require(RMGOption.LISTEN_PORT);
try
{
String listenerIP = RMGOption.LISTEN_IP.require();
int listenerPort = RMGOption.LISTEN_PORT.require();

YsoIntegration.createJRMPListener(listenerIP, listenerPort, p.getGadget());
YsoIntegration.createJRMPListener(listenerIP, listenerPort, p.getGadget());
}

catch (RequirementException e)
{
ExceptionHandler.requirementException(e);
}
}

/**
Expand Down Expand Up @@ -431,8 +459,20 @@ public void dispatchCodebase()
{
RMGOption.requireTarget();

String codebase = RMGOption.require(RMGOption.CODEBASE_URL);
String className = RMGOption.require(RMGOption.CODEBASE_CLASS);
String codebase = "";
String className = "";

try
{
codebase = RMGOption.CODEBASE_URL.require();
className = RMGOption.CODEBASE_CLASS.require();
}

catch (RequirementException e)
{
ExceptionHandler.requirementException(e);
}

RMGUtils.setCodebase(codebase);

Object payload = null;
Expand Down Expand Up @@ -664,20 +704,28 @@ public void dispatchGuess()
*/
public void dispatchKnown()
{
String className = RMGOption.require(RMGOption.KNOWN_CLASS);
Formatter formatter = new Formatter();
try
{
String className = RMGOption.KNOWN_CLASS.require();
Formatter formatter = new Formatter();

KnownEndpointHolder keh = KnownEndpointHolder.getHolder();
KnownEndpoint endpoint = keh.lookup(className);
KnownEndpointHolder keh = KnownEndpointHolder.getHolder();
KnownEndpoint endpoint = keh.lookup(className);

if (endpoint == null)
{
Logger.eprintlnMixedYellow("The specified class name", className, "isn't a known class.");
if (endpoint == null)
{
Logger.eprintlnMixedYellow("The specified class name", className, "isn't a known class.");
}

else
{
formatter.listKnownEndpoint(endpoint);
}
}

else
catch (RequirementException e)
{
formatter.listKnownEndpoint(endpoint);
ExceptionHandler.requirementException(e);
}
}

Expand All @@ -687,32 +735,74 @@ public void dispatchKnown()
*/
public void dispatchPortScan()
{
String host = RMGOption.require(RMGOption.TARGET_HOST);
int[] rmiPorts = p.getRmiPorts();
try
{
String host = RMGOption.TARGET_HOST.require();
int[] rmiPorts = p.getRmiPorts();

Logger.printMixedYellow("Scanning", String.valueOf(rmiPorts.length), "Ports on ");
Logger.printlnPlainMixedBlueFirst(host, "for RMI services.");
Logger.lineBreak();
Logger.increaseIndent();
Logger.printMixedYellow("Scanning", String.valueOf(rmiPorts.length), "Ports on ");
Logger.printlnPlainMixedBlueFirst(host, "for RMI services.");
Logger.lineBreak();
Logger.increaseIndent();

PortScanner ps = new PortScanner(host, rmiPorts);
ps.portScan();
PortScanner ps = new PortScanner(host, rmiPorts);
ps.portScan();

Logger.decreaseIndent();
Logger.lineBreak();
Logger.decreaseIndent();
Logger.lineBreak();

Logger.println("Portscan finished.");
Logger.println("Portscan finished.");
}

catch (RequirementException e)
{
ExceptionHandler.requirementException(e);
}
}

/**
* Prints detailed information on the user specified ObjID.
*/
public void dispatchObjID()
{
String objIDString = RMGOption.require(RMGOption.OBJID_OBJID);
try
{
int listenerPort = RMGOption.LISTEN_PORT.require();
String listenerHost = RMGOption.LISTEN_IP.require();

RogueJMX rogueJMX = new RogueJMX(listenerHost, listenerPort, RMGOption.ROGUEJMX_OBJID.getValue());

if (RMGOption.ROGUEJMX_FORWARD_HOST.notNull())
{
String forwardHost = RMGOption.ROGUEJMX_FORWARD_HOST.getValue();
int forwardPort = RMGOption.ROGUEJMX_FORWARD_PORT.require();

String boundName = RMGOption.ROGUEJMX_FORWARD_BOUND_NAME.getValue();
String objid = RMGOption.ROGUEJMX_FORWARD_OBJID.getValue();

ObjID objID = RMGUtils.parseObjID(objIDString);
RMGUtils.printObjID(objID);
RMIEndpoint rmi = new RMIEndpoint(forwardHost, forwardPort);
RemoteObjectClient client = getRemoteObjectClient(objid, boundName, rmi);
client.assignInterface(RMIServer.class);

rogueJMX.forwardTo(client);
}

try
{
rogueJMX.export();
Logger.lineBreak();
}

catch (java.rmi.RemoteException e)
{
ExceptionHandler.unexpectedException(e, "exporting", "rogue JMX server", true);
}
}

catch (RequirementException e)
{
ExceptionHandler.requirementException(e);
}
}

/**
Expand All @@ -723,35 +813,18 @@ public void dispatchObjID()
*/
public void dispatchRogueJMX()
{
int listenerPort = RMGOption.require(RMGOption.LISTEN_PORT);
String listenerHost = RMGOption.require(RMGOption.LISTEN_IP);

RogueJMX rogueJMX = new RogueJMX(listenerHost, listenerPort, RMGOption.ROGUEJMX_OBJID.getValue());

if (RMGOption.ROGUEJMX_FORWARD_HOST.notNull())
try
{
String forwardHost = RMGOption.ROGUEJMX_FORWARD_HOST.getValue();
int forwardPort = RMGOption.require(RMGOption.ROGUEJMX_FORWARD_PORT);

String boundName = RMGOption.ROGUEJMX_FORWARD_BOUND_NAME.getValue();
String objid = RMGOption.ROGUEJMX_FORWARD_OBJID.getValue();
String objIDString = RMGOption.OBJID_OBJID.require();

RMIEndpoint rmi = new RMIEndpoint(forwardHost, forwardPort);
RemoteObjectClient client = getRemoteObjectClient(objid, boundName, rmi);
client.assignInterface(RMIServer.class);

rogueJMX.forwardTo(client);
ObjID objID = RMGUtils.parseObjID(objIDString);
RMGUtils.printObjID(objID);
}

try
catch (RequirementException e)
{
rogueJMX.export();
Logger.lineBreak();
ExceptionHandler.requirementException(e);
}

catch (java.rmi.RemoteException e)
{
ExceptionHandler.unexpectedException(e, "exporting", "rogue JMX server", true);
}
}
}

0 comments on commit 1b943d4

Please sign in to comment.