Skip to content

Commit

Permalink
Add overwrite options
Browse files Browse the repository at this point in the history
Added the --overwrite-host and --overwrite-port options that allow to
overwrite the destination endpoint pointed to by a bound name (see #42).
  • Loading branch information
qtc-de committed Mar 25, 2024
1 parent 37f7e6d commit 7db5b05
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@
*
* @author Tobias Neitzel (@qtc_de)
*/
public class LoopbackSocketFactory extends RMISocketFactory {

public class LoopbackSocketFactory extends RMISocketFactory
{
private String host;
private int port;
private RMISocketFactory fac;
private boolean printInfo = true;
private boolean followRedirect = false;
Expand All @@ -43,12 +44,14 @@ public class LoopbackSocketFactory extends RMISocketFactory {
* Creates a new LoopbackSocketFactory.
*
* @param host remote host that is expected to get all further RMI connections
* @param port if not set to zero, redirect connections to the specified port
* @param fac original socket factory to create sockets from
* @param followRedirect if true, connections are not redirected to the expected host
*/
public LoopbackSocketFactory(String host, RMISocketFactory fac, boolean followRedirect)
public LoopbackSocketFactory(String host, int port, RMISocketFactory fac, boolean followRedirect)
{
this.host = host;
this.port = port;
this.fac = fac;
this.followRedirect= followRedirect;
}
Expand All @@ -67,38 +70,54 @@ public Socket createSocket(String host, int port) throws IOException
{
Socket sock = null;

if(!this.host.equals(host)) {

if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) {
if (!this.host.equals(host))
{
if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool())
{
Logger.printInfoBox();
Logger.printlnMixedBlue("RMI object tries to connect to different remote host:", host);
}

if( this.followRedirect ) {
if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() )
if (this.followRedirect)
{
if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool())
{
Logger.println("Following redirect to new target...");
}

} else {
}

else
{
host = this.host;

if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) {
if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool())
{
Logger.printlnMixedBlue("Redirecting the connection back to", host);
Logger.printlnMixedYellow("You can use", "--follow", "to prevent this.");
}
}

if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) {
if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool())
{
Logger.decreaseIndent();
}

this.printInfo = false;
}

try {
if (this.port > 0 && this.port != port)
{
port = this.port;
}

try
{
sock = fac.createSocket(host, port);
}

} catch( UnknownHostException e ) {
catch (UnknownHostException e)
{
ExceptionHandler.unknownHost(e, host, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@
*
* @author Tobias Neitzel (@qtc_de)
*/
public class LoopbackSslSocketFactory extends SSLSocketFactory {

public class LoopbackSslSocketFactory extends SSLSocketFactory
{
public static String host = "";
public static int port = 0;
public static SSLSocketFactory fac = null;
public static boolean printInfo = true;
public static boolean followRedirect = false;
Expand All @@ -50,38 +51,54 @@ public Socket createSocket(String target, int port) throws IOException
{
Socket sock = null;

if(!host.equals(target)) {

if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) {
if (!host.equals(target))
{
if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool())
{
Logger.printInfoBox();
Logger.printlnMixedBlue("RMI object tries to connect to different remote host:", target);
}

if( followRedirect ) {
if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() )
if (followRedirect)
{
if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool())
{
Logger.println("Following SSL redirect to new target...");
}

} else {
}

else
{
target = host;

if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) {
if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool())
{
Logger.printlnMixedBlue("Redirecting the SSL connection back to", host);
Logger.printlnMixedYellow("You can use", "--follow", "to prevent this.");
}
}

if( printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool() ) {
if (printInfo && BeanshooterOption.GLOBAL_VERBOSE.getBool())
{
Logger.decreaseIndent();
}

printInfo = false;
}

try {
if (LoopbackSslSocketFactory.port > 0 && LoopbackSslSocketFactory.port != port)
{
port = LoopbackSslSocketFactory.port;
}

try
{
sock = fac.createSocket(host, port);
}

} catch( UnknownHostException e ) {
catch (UnknownHostException e)
{
ExceptionHandler.unknownHost(e, host, true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,18 @@ public RMIRegistryEndpoint(RMIEndpoint rmi)

private synchronized static void SocketFactorySetup(String host, int port)
{
if( setupComplete )
if (setupComplete)
{
return;
}

try {
try
{
RMISocketFactory.setSocketFactory(PluginSystem.getDefaultRMISocketFactory(host, port));
}

} catch (IOException e) {
catch (IOException e)
{
Logger.eprintlnMixedBlue("Unable to set custom", "RMISocketFactory.", "Host redirection will probably not work.");
ExceptionHandler.showStackTrace(e);
Logger.eprintln("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OBJID_CONNECTION,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_FOLLOW,
BeanshooterOption.CONN_SSL,
BeanshooterOption.CONN_JMXMP,
Expand Down Expand Up @@ -58,6 +60,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_PORT,
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_FOLLOW,
BeanshooterOption.CONN_SSL,
BeanshooterOption.CONN_JMXMP,
Expand Down Expand Up @@ -89,6 +93,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OBJID_CONNECTION,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_FOLLOW,
BeanshooterOption.CONN_SSL,
BeanshooterOption.CONN_JMXMP,
Expand Down Expand Up @@ -121,6 +127,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OBJID_CONNECTION,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_JMXMP,
BeanshooterOption.CONN_JOLOKIA,
BeanshooterOption.CONN_JOLOKIA_ENDPOINT,
Expand All @@ -146,6 +154,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OBJID_CONNECTION,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_JMXMP,
BeanshooterOption.CONN_JOLOKIA,
BeanshooterOption.CONN_JOLOKIA_ENDPOINT,
Expand Down Expand Up @@ -176,6 +186,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OBJID_CONNECTION,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_FOLLOW,
BeanshooterOption.CONN_SSL,
BeanshooterOption.CONN_JMXMP,
Expand Down Expand Up @@ -225,6 +237,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OBJID_CONNECTION,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_JMXMP,
BeanshooterOption.CONN_JOLOKIA,
BeanshooterOption.CONN_JOLOKIA_ENDPOINT,
Expand Down Expand Up @@ -252,6 +266,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OBJID_CONNECTION,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_FOLLOW,
BeanshooterOption.CONN_SSL,
BeanshooterOption.CONN_JMXMP,
Expand Down Expand Up @@ -283,6 +299,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OBJID_CONNECTION,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_FOLLOW,
BeanshooterOption.CONN_SSL,
BeanshooterOption.CONN_JMXMP,
Expand Down Expand Up @@ -311,6 +329,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OBJID_CONNECTION,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_FOLLOW,
BeanshooterOption.CONN_JOLOKIA,
BeanshooterOption.CONN_JOLOKIA_ENDPOINT,
Expand Down Expand Up @@ -356,6 +376,8 @@ public enum BeanshooterOperation implements Operation {
BeanshooterOption.TARGET_BOUND_NAME,
BeanshooterOption.TARGET_OBJID_SERVER,
BeanshooterOption.TARGET_OBJID_CONNECTION,
BeanshooterOption.TARGET_OVERWRITE_HOST,
BeanshooterOption.TARGET_OVERWRITE_PORT,
BeanshooterOption.CONN_FOLLOW,
BeanshooterOption.CONN_JOLOKIA,
BeanshooterOption.CONN_JOLOKIA_ENDPOINT,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ public enum BeanshooterOption implements Option {
ArgType.INT,
"port"),

TARGET_OVERWRITE_HOST("--overwrite-host",
"overwrite the host a boundname points to",
Arguments.store(),
OptionGroup.TARGET,
ArgType.STRING,
"host"),

TARGET_OVERWRITE_PORT("--overwrite-port",
"overwrite the port a boundname points to",
Arguments.store(),
OptionGroup.TARGET,
ArgType.INT,
"port"),

TARGET_BOUND_NAME("--bound-name",
"target bound name within an RMI registry",
Arguments.store(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ else if (!enumerated)
if (BeanshooterOption.CONN_JOLOKIA.getBool())
enumHelper.enumJolokiaVersion();

else if (!BeanshooterOption.CONN_JNDI.<String>getValue().contains("service:jmx:remote+"))
else if (!BeanshooterOption.CONN_JNDI.<String>getValue("").contains("service:jmx:remote+"))
enumHelper.enumSerial();

if (!access)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,38 @@ public RMIClientSocketFactory getRMIClientSocketFactory(String host, int port)
* are looked up from the RMI registry use the RMISocketFactory.getDefaultSocketFactory function to
* obtain a SocketFactory. This factory is then used for explicit calls (method invocations) and for
* implicit calls (DGC actions like clean or dirty).
*
* Since beanshooter v5.0.0, LoopbackSocketFactory also performs port redirection if the
* corresponding command line arguments have been specified.
*/
@Override
public RMISocketFactory getDefaultRMISocketFactory(String host, int port)
{
RMISocketFactory fac = RMISocketFactory.getDefaultSocketFactory();
return new LoopbackSocketFactory(host, fac, BeanshooterOption.CONN_FOLLOW.getBool());

String factoryHost = BeanshooterOption.TARGET_OVERWRITE_HOST.getValue(host);
int factoryPort = BeanshooterOption.TARGET_OVERWRITE_PORT.getValue(0);

return new LoopbackSocketFactory(factoryHost, factoryPort, fac, BeanshooterOption.CONN_FOLLOW.getBool());
}

/**
* The default SSLRMISocketFactory used by beanshooter is the LoopbackSslSocketFactory, which
* redirects all connection to the original target and thus prevents unwanted RMI redirections.
*
* Since beanshooter v5.0.0, LoopbackSslSocketFactory also performs port redirection if the
* corresponding command line arguments have been specified.
*/
@Override
public String getDefaultSSLSocketFactoryClass(String host, int port)
{
TrustAllSocketFactory trustAllFax = new TrustAllSocketFactory();

LoopbackSslSocketFactory.host = host;
String factoryHost = BeanshooterOption.TARGET_OVERWRITE_HOST.getValue(host);
int factoryPort = BeanshooterOption.TARGET_OVERWRITE_PORT.getValue(0);

LoopbackSslSocketFactory.host = factoryHost;
LoopbackSslSocketFactory.port = factoryPort;
LoopbackSslSocketFactory.fac = trustAllFax.getSSLSocketFactory();
LoopbackSslSocketFactory.followRedirect = BeanshooterOption.CONN_FOLLOW.getBool();

Expand Down

0 comments on commit 7db5b05

Please sign in to comment.