From 1ccdc5f9d8fa94e79ccb29cf4f8456ddad77dd94 Mon Sep 17 00:00:00 2001 From: moocow9m Date: Mon, 4 Nov 2019 11:26:38 -0800 Subject: [PATCH] Cleanup Code Add debug option to load options Run which better --- build.gradle.kts | 2 +- .../com/wavproductions/www/armitage/Main.kt | 7 ++- .../wavproductions/www/armitage/Metasploit.kt | 58 ++++++++++--------- 3 files changed, 37 insertions(+), 30 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index a2fd8d51..d8ddbbfb 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { kotlin("jvm") version "1.3.50" } -group = "com.wavProductions.www" +group = "com.wavproductions.www" version = "0.1-SNAPSHOT" repositories { diff --git a/src/main/kotlin/com/wavproductions/www/armitage/Main.kt b/src/main/kotlin/com/wavproductions/www/armitage/Main.kt index ec4c4c26..8a57ff3c 100644 --- a/src/main/kotlin/com/wavproductions/www/armitage/Main.kt +++ b/src/main/kotlin/com/wavproductions/www/armitage/Main.kt @@ -7,16 +7,17 @@ import java.io.InputStream import java.nio.file.Files import java.nio.file.Paths -fun main(args: MutableList) { +fun main(args: Array) { val resources = Paths.get("resources").toAbsolutePath() if (Files.notExists(resources)) { Files.createDirectories(resources) } loadConfig() val conn = Metasploit() - val debug = args.contains("debug") //testing metasploit connection and calls + val debug = args.contains("debug-cmd") //testing metasploit connection and calls if (debug) { - conn.load() + conn.load(debug = true) + } else { val iconPath = Paths.get(resources.toString(), "icon.png") if (Files.notExists(iconPath)) { diff --git a/src/main/kotlin/com/wavproductions/www/armitage/Metasploit.kt b/src/main/kotlin/com/wavproductions/www/armitage/Metasploit.kt index 1bd0ba56..8ab17dd8 100644 --- a/src/main/kotlin/com/wavproductions/www/armitage/Metasploit.kt +++ b/src/main/kotlin/com/wavproductions/www/armitage/Metasploit.kt @@ -2,22 +2,16 @@ package com.wavproductions.www.armitage import java.io.Closeable import java.io.IOException -import java.io.InputStream import java.net.InetAddress import java.net.Socket -import java.nio.file.Files -import java.nio.file.Path import java.security.InvalidParameterException import javax.net.ssl.SSLSocketFactory class Metasploit : Closeable { private var console: Process? - private val temp: Path = Files.createTempFile("console", "MCArmitage") - private val fileIn: InputStream private var rpc: Socket? = null //will be a direct connection, so most likely a socket init { - fileIn = Files.newInputStream(temp) console = null } @@ -28,7 +22,8 @@ class Metasploit : Closeable { port: Int = 55552, ssl: Boolean = false, program: String? = null, - local: Boolean = true + local: Boolean = true, + debug: Boolean = false ) { if (port < 0 || port > 65535) { throw InvalidParameterException("port is invalid! Valid values are 0-65535") @@ -37,15 +32,25 @@ class Metasploit : Closeable { try { connected = attemptConnect(username, password, ip, port, ssl) } catch (ignored: Exception) { + if (debug) { + ignored.printStackTrace() + } } if (!connected && local) { val location = program ?: locateMetasploit() ?: throw NullPointerException("Unable to locate console!") val builder = ProcessBuilder(location) builder.redirectOutput(ProcessBuilder.Redirect.PIPE) builder.redirectError(ProcessBuilder.Redirect.PIPE) + if (debug) { + builder.redirectOutput(ProcessBuilder.Redirect.INHERIT) + builder.redirectError(ProcessBuilder.Redirect.INHERIT) + } builder.redirectInput(ProcessBuilder.Redirect.PIPE) console = builder.start() - //start rpc + //await startup + console?.outputStream?.write("load msgrpc ServerHost='${ip.hostAddress}' ServerPort=$port User='$username' Pass='$password'\n".toByteArray()) + console?.outputStream?.flush() + //await rpc start connected = attemptConnect(username, password, ip, port, ssl) } if (!connected) { @@ -55,11 +60,25 @@ class Metasploit : Closeable { private fun locateMetasploit(): String? { val builder = ProcessBuilder("which", "msfconsole") //linux lookup for now... might add windows later - builder.redirectOutput(ProcessBuilder.Redirect.INHERIT) - builder.redirectError(ProcessBuilder.Redirect.INHERIT) - builder.redirectInput(ProcessBuilder.Redirect.DISCARD) - console = builder.start() - return null + builder.redirectOutput(ProcessBuilder.Redirect.PIPE) + builder.redirectError(ProcessBuilder.Redirect.PIPE) + builder.redirectInput(ProcessBuilder.Redirect.PIPE) + val which = builder.start() + while (which.isAlive) { + Thread.onSpinWait() + } + if (String(which.errorStream.readAllBytes()).contains("which: no")) { + return null + } + val programs = String(which.inputStream.readAllBytes()).split("\n") + var selected: String? = null + for (x in programs) { + if (x.endsWith("msfconsole")) { //select first program located by which + selected = x + break + } + } + return selected } private fun attemptConnect(username: String, password: String, ip: InetAddress, port: Int, ssl: Boolean): Boolean { @@ -82,17 +101,6 @@ class Metasploit : Closeable { return false //rpc failed to init! } - fun sendConsoleCommand(command: ByteArray, flush: Boolean = false) { - console?.outputStream?.write(command) - if (flush) { - console?.outputStream?.flush() - } - } - - fun readConsole(): ByteArray { - return fileIn.readAllBytes() - } - fun readRPC(): ByteArray { return rpc?.inputStream?.readAllBytes() ?: ByteArray(0) } @@ -107,7 +115,5 @@ class Metasploit : Closeable { override fun close() { //destroy resources console?.destroyForcibly() rpc?.close() - fileIn.close() - Files.deleteIfExists(temp) } } \ No newline at end of file