Skip to content

Commit

Permalink
Merge pull request kimocoder#3 from Moocow9m/rewrite
Browse files Browse the repository at this point in the history
Merge changes
  • Loading branch information
kimocoder authored Nov 9, 2019
2 parents dff87bc + 9d2ee8e commit 3520829
Show file tree
Hide file tree
Showing 9 changed files with 167 additions and 100 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
kotlin("jvm") version "1.3.50"
}

group = "com.wavProductions.www"
group = "com.wavproductions.www"
version = "0.1-SNAPSHOT"

repositories {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
37 changes: 0 additions & 37 deletions src/main/kotlin/com/wavProductions/www/armitage/Main.kt

This file was deleted.

59 changes: 0 additions & 59 deletions src/main/kotlin/com/wavProductions/www/armitage/Metasploit.kt

This file was deleted.

44 changes: 44 additions & 0 deletions src/main/kotlin/com/wavproductions/www/armitage/Main.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.wavproductions.www.armitage

import com.wavproductions.viken.Viken
import com.wavproductions.viken.VikenImage
import com.wavproductions.www.armitage.Config.loadConfig
import java.io.InputStream
import java.nio.file.Files
import java.nio.file.Paths

fun main(args: Array<String>) {
val resources = Paths.get("resources").toAbsolutePath()
if (Files.notExists(resources)) {
Files.createDirectories(resources)
}
loadConfig()
val conn = Metasploit()
val debug = args.contains("debug-cmd") //testing metasploit connection and calls
if (debug) {
conn.load(debug = true)

} else {
val iconPath = Paths.get(resources.toString(), "icon.png")
if (Files.notExists(iconPath)) {
val stream: InputStream = Config.javaClass.getResourceAsStream("/icon.png")
?: throw RuntimeException("Corrupted jar resources!")
Files.write(iconPath, stream.readAllBytes())
stream.close()
}
Viken.init()
val icon = VikenImage(iconPath)
val window = Viken.createSync(800, 800, "Armitage ReWrite")
requireNotNull(window) { "Could not create Window!" }
window.setIconSync(icon.convertToIcon())
window.showSync()
Viken.loop()
while (conn.consoleActive()) {
Thread.onSpinWait()
}
Thread.sleep(20000) //run for 20 seconds for testing
Viken.cleanup()
icon.dispose()
}

}
119 changes: 119 additions & 0 deletions src/main/kotlin/com/wavproductions/www/armitage/Metasploit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package com.wavproductions.www.armitage

import java.io.Closeable
import java.io.IOException
import java.net.InetAddress
import java.net.Socket
import java.security.InvalidParameterException
import javax.net.ssl.SSLSocketFactory

class Metasploit : Closeable {
private var console: Process?
private var rpc: Socket? = null //will be a direct connection, so most likely a socket

init {
console = null
}

fun load(
username: String = "msf",
password: String = "pass",
ip: InetAddress = InetAddress.getLoopbackAddress(),
port: Int = 55552,
ssl: Boolean = false,
program: String? = null,
local: Boolean = true,
debug: Boolean = false
) {
if (port < 0 || port > 65535) {
throw InvalidParameterException("port is invalid! Valid values are 0-65535")
}
var connected = false
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()
//await startup
console?.outputStream?.write("load msgrpc ServerHost='${ip.hostAddress}' ServerPort=$port User='$username' Pass='$password' SSL=$ssl\n".toByteArray())
console?.outputStream?.flush()
//await rpc start
connected = attemptConnect(username, password, ip, port, ssl)
}
if (!connected) {
throw IOException("Failed to contact the RPC server")
}
}

private fun locateMetasploit(): String? {
val builder = ProcessBuilder("which", "msfconsole") //linux lookup for now... might add windows later
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 {
val socket = if (ssl) SSLSocketFactory.getDefault().createSocket(ip, port) else Socket(ip, port)
if (socket.isConnected) {
rpc = socket
return true
}
return false
}

fun consoleActive(): Boolean {
return console?.isAlive ?: false
}

fun startRPC(): Boolean {
if (!consoleActive()) { //make sure console is active to init rpc
return false
}
return false //rpc failed to init!
}

fun readRPC(): ByteArray {
return rpc?.inputStream?.readAllBytes() ?: ByteArray(0)
}

fun sendRPCCommand(command: ByteArray, flush: Boolean = false) {
rpc?.outputStream?.write(command)
if (flush) {
rpc?.outputStream?.flush()
}
}

override fun close() { //destroy resources
console?.destroyForcibly()
rpc?.close()
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package com.wavproductions.www.armitage

0 comments on commit 3520829

Please sign in to comment.