Add SSL support to connection starting/connecting.

git-svn-id: file:///home/svn/framework3/trunk@10810 4d416f70-5f16-0410-b530-b9f4589650da
This commit is contained in:
Matt Weeks 2010-10-24 20:09:04 +00:00
parent 2021b1628e
commit ec15abd8dc
5 changed files with 101 additions and 24 deletions

Binary file not shown.

View File

@ -29,29 +29,31 @@
<Component id="titleLabel" alignment="0" min="-2" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<Group type="103" groupAlignment="0" attributes="0">
<Group type="102" attributes="0">
<Component id="usernameLabel" min="-2" pref="67" max="-2" attributes="0"/>
<Group type="102" alignment="0" attributes="0">
<EmptySpace min="-2" pref="6" max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Component id="jLabel1" alignment="1" min="-2" max="-2" attributes="0"/>
<Component id="hostLabel" alignment="1" min="-2" max="-2" attributes="0"/>
<Component id="passwordLabel" alignment="1" min="-2" max="-2" attributes="0"/>
<Component id="portLabel" min="-2" max="-2" attributes="0"/>
</Group>
</Group>
<Component id="usernameLabel" min="-2" pref="67" max="-2" attributes="0"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" attributes="0">
<Component id="usernameField" alignment="0" pref="431" max="32767" attributes="0"/>
<Component id="hostField" pref="431" max="32767" attributes="0"/>
<Component id="passwordField" alignment="0" pref="431" max="32767" attributes="0"/>
<Component id="portField" alignment="0" pref="431" max="32767" attributes="0"/>
<Component id="sslBox" pref="372" max="32767" attributes="0"/>
<Component id="usernameField" alignment="0" pref="372" max="32767" attributes="0"/>
<Component id="hostField" alignment="0" pref="372" max="32767" attributes="0"/>
<Component id="passwordField" alignment="0" pref="372" max="32767" attributes="0"/>
<Component id="portField" alignment="0" pref="372" max="32767" attributes="0"/>
</Group>
</Group>
<Group type="102" alignment="1" attributes="0">
<Component id="startNewButton" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="pathButton" min="-2" max="-2" attributes="0"/>
<EmptySpace pref="134" max="32767" attributes="0"/>
<EmptySpace pref="61" max="32767" attributes="0"/>
<Component id="cancelButton" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="connectButton" min="-2" max="-2" attributes="0"/>
@ -86,7 +88,12 @@
<Component id="portField" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="portLabel" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace type="unrelated" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="0" max="-2" attributes="0">
<Component id="jLabel1" max="32767" attributes="1"/>
<Component id="sslBox" alignment="0" max="32767" attributes="1"/>
</Group>
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="1" attributes="0">
<Group type="103" alignment="1" groupAlignment="3" attributes="0">
<Component id="startNewButton" alignment="3" pref="37" max="32767" attributes="0"/>
@ -220,5 +227,17 @@
<EventHandler event="actionPerformed" listener="java.awt.event.ActionListener" parameters="java.awt.event.ActionEvent" handler="pathButtonActionPerformed"/>
</Events>
</Component>
<Component class="javax.swing.JCheckBox" name="sslBox">
<Properties>
<Property name="text" type="java.lang.String" resourceKey="sslBox.text"/>
<Property name="name" type="java.lang.String" value="sslBox" noResource="true"/>
</Properties>
</Component>
<Component class="javax.swing.JLabel" name="jLabel1">
<Properties>
<Property name="text" type="java.lang.String" resourceKey="jLabel1.text"/>
<Property name="name" type="java.lang.String" value="jLabel1" noResource="true"/>
</Properties>
</Component>
</SubComponents>
</Form>

View File

@ -37,7 +37,8 @@ public class OpenConnectionDialog extends javax.swing.JDialog {
String password = info.get("password").toString();
String host = info.get("host").toString();
int port = Integer.parseInt(info.get("port").toString());
return new RpcConnection(username, password.toCharArray(), host, port);
boolean ssl = Boolean.parseBoolean(info.get("ssl").toString());
return new RpcConnection(username, password.toCharArray(), host, port, ssl);
} catch (MsfException mex) {
} catch (NullPointerException nex) {//generated when attributes dont exist.
}
@ -73,6 +74,8 @@ public class OpenConnectionDialog extends javax.swing.JDialog {
cancelButton = new javax.swing.JButton();
startNewButton = new javax.swing.JButton();
pathButton = new javax.swing.JButton();
sslBox = new javax.swing.JCheckBox();
jLabel1 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(msfgui.MsfguiApp.class).getContext().getResourceMap(OpenConnectionDialog.class);
@ -160,6 +163,12 @@ public class OpenConnectionDialog extends javax.swing.JDialog {
}
});
sslBox.setText(resourceMap.getString("sslBox.text")); // NOI18N
sslBox.setName("sslBox"); // NOI18N
jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
jLabel1.setName("jLabel1"); // NOI18N
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
@ -170,24 +179,26 @@ public class OpenConnectionDialog extends javax.swing.JDialog {
.addComponent(titleLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(usernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(6, 6, 6)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(jLabel1)
.addComponent(hostLabel)
.addComponent(passwordLabel)
.addComponent(portLabel)))
.addComponent(usernameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))
.addComponent(portLabel))))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
.addComponent(hostField, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
.addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)
.addComponent(portField, javax.swing.GroupLayout.DEFAULT_SIZE, 431, Short.MAX_VALUE)))
.addComponent(sslBox, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
.addComponent(usernameField, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
.addComponent(hostField, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
.addComponent(passwordField, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)
.addComponent(portField, javax.swing.GroupLayout.DEFAULT_SIZE, 372, Short.MAX_VALUE)))
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(startNewButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(pathButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 81, Short.MAX_VALUE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 61, Short.MAX_VALUE)
.addComponent(cancelButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(connectButton)))
@ -214,7 +225,11 @@ public class OpenConnectionDialog extends javax.swing.JDialog {
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(portField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(portLabel))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sslBox, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(startNewButton, javax.swing.GroupLayout.DEFAULT_SIZE, 37, Short.MAX_VALUE)
@ -233,8 +248,9 @@ public class OpenConnectionDialog extends javax.swing.JDialog {
char[] password = passwordField.getPassword();
String host = hostField.getText();
int port = Integer.parseInt(portField.getText());
boolean ssl = sslBox.isSelected();
try {
rpcConn = new RpcConnection(username, password, host, port);
rpcConn = new RpcConnection(username, password, host, port, ssl);
} catch (MsfException mex) {
rpcConn = null;
}
@ -270,6 +286,7 @@ public class OpenConnectionDialog extends javax.swing.JDialog {
if(hostField.getText().length() > 0)
RpcConnection.defaultHost = hostField.getText();
RpcConnection.defaultPort = Integer.parseInt(portField.getText());
RpcConnection.defaultSsl = sslBox.isSelected();
//do the action. There's probably a "right" way to do Oh well.
mainframe.getContext().getActionMap(mainframe).get("startRpc").actionPerformed(new java.awt.event.ActionEvent(startNewButton,1234,""));
setVisible(false);
@ -299,11 +316,13 @@ public class OpenConnectionDialog extends javax.swing.JDialog {
private javax.swing.JButton connectButton;
private javax.swing.JTextField hostField;
private javax.swing.JLabel hostLabel;
private javax.swing.JLabel jLabel1;
private javax.swing.JPasswordField passwordField;
private javax.swing.JLabel passwordLabel;
private javax.swing.JButton pathButton;
private javax.swing.JTextField portField;
private javax.swing.JLabel portLabel;
private javax.swing.JCheckBox sslBox;
private javax.swing.JButton startNewButton;
private javax.swing.JLabel titleLabel;
private javax.swing.JTextField usernameField;

View File

@ -6,6 +6,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.text.ParseException;
import java.text.SimpleDateFormat;
@ -14,6 +16,10 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
@ -40,23 +46,44 @@ public class RpcConnection {
private Map callCache = new HashMap();
public static String defaultUser = "msf",defaultPass = null, defaultHost = "127.0.0.1";
public static int defaultPort = 55553;
public static boolean defaultSsl = false;
private Socket connection;
private OutputStream sout; //socket output/input
private InputStream sin;
private final Object lockObject = new Object();//to synchronize one request at a time
private String username, password, host;
private int port;
private boolean ssl;
/** Constructor sets up a connection and authenticates. */
RpcConnection(String username, char[] password, String host, int port) throws MsfException {
RpcConnection(String username, char[] password, String host, int port, boolean ssl) throws MsfException {
boolean haveRpcd=false;
this.username = username;
this.password = new String(password);
this.host = host;
this.port = port;
this.ssl = ssl;
String message = "";
try {
if(ssl){
TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
}
}
};
// Let us create the factory where we can set some parameters for the connection
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
connection = sc.getSocketFactory().createSocket(host,port);
} else {
connection = new Socket(host, port);
}
sout = connection.getOutputStream();
sin = connection.getInputStream();
Map results = exec("auth.login",new Object[]{username, this.password});
@ -67,6 +94,8 @@ public class RpcConnection {
} catch (IOException io){
message = io.getLocalizedMessage();
} catch (NullPointerException nex){
} catch (NoSuchAlgorithmException nsax){
} catch (KeyManagementException kmx){
}
if(!haveRpcd)
throw new MsfException("Error connecting. "+message);
@ -75,6 +104,7 @@ public class RpcConnection {
root.put("password", this.password);
root.put("host", this.host);
root.put("port", Integer.toString(this.port));
root.put("ssl", Boolean.toString(this.ssl));
}
public String toString(){
@ -306,10 +336,17 @@ public class RpcConnection {
defaultPass = password.toString();
}
if (defaultSsl)
setMessage("Starting msfrpcd. \"msfrpcd -P " + defaultPass + " -t Basic -U metasploit -a 127.0.0.1\"");
else
setMessage("Starting msfrpcd. \"msfrpcd -P " + defaultPass + " -t Basic -S -U metasploit -a 127.0.0.1\"");
setProgress(0.2f);
Process proc = null;
try {
if(defaultSsl)
proc = MsfguiApp.startMsfProc(new String[]{
"msfrpcd","-P",defaultPass,"-t","Basic","-U",defaultUser,"-a","127.0.0.1"});
else
proc = MsfguiApp.startMsfProc(new String[]{
"msfrpcd","-P",defaultPass,"-t","Basic","-S","-U",defaultUser,"-a","127.0.0.1"});
} catch (MsfException ex) {
@ -324,7 +361,7 @@ public class RpcConnection {
boolean connected = false;
for (int tries = 0; tries < 1000; tries++) { //it usually takes a minute to get started
try {
myRpcConn = new RpcConnection(defaultUser, defaultPass.toCharArray(), "127.0.0.1", defaultPort);
myRpcConn = new RpcConnection(defaultUser, defaultPass.toCharArray(), "127.0.0.1", defaultPort, defaultSsl);
connected = true;
break;
} catch (MsfException mex) {

View File

@ -13,3 +13,5 @@ Form.title=msfgui - connection
main.icon=msf_file.png
startNewButton.text=Start new msfrpcd
pathButton.text=Change path
sslBox.text=
jLabel1.text=SSL