Added more error handling and more verbosity where needed

This commit is contained in:
Pourliver 2019-06-03 09:25:38 -04:00
commit 06a93b44dd

View file

@ -10,6 +10,7 @@ import (
"io/ioutil" "io/ioutil"
golog "log" golog "log"
"net" "net"
"os"
"regexp" "regexp"
"time" "time"
"syscall" "syscall"
@ -102,6 +103,14 @@ func (mod RdpProxy) Author() string {
return "Alexandre Beaulieu <alex@segfault.me> && Maxime Carbonneau <pourliver@gmail.com>" return "Alexandre Beaulieu <alex@segfault.me> && Maxime Carbonneau <pourliver@gmail.com>"
} }
func (mod *RdpProxy) fileExists(name string) (bool, error) {
_, err := os.Stat(name)
if os.IsNotExist(err) {
return false, nil
}
return err != nil, err
}
func (mod *RdpProxy) isTarget(ip string) bool { func (mod *RdpProxy) isTarget(ip string) bool {
for _, addr := range mod.targets { for _, addr := range mod.targets {
if addr.String() == ip { if addr.String() == ip {
@ -112,7 +121,7 @@ func (mod *RdpProxy) isTarget(ip string) bool {
} }
// Verify if the target says anything about enforcing NLA. // Verify if the target says anything about enforcing NLA.
func verifyNLA(target string, payload []byte) (isNla bool, err error) { func (mod *RdpProxy) verifyNLA(target string, payload []byte) (isNla bool, err error) {
var conn net.Conn var conn net.Conn
if conn, err = net.Dial("tcp", target); err != nil { if conn, err = net.Dial("tcp", target); err != nil {
@ -152,9 +161,9 @@ func (mod *RdpProxy) isNLAEnforced(target string) (nla bool, err error){
var nlaCheck1 bool var nlaCheck1 bool
var nlaCheck2 bool var nlaCheck2 bool
if nlaCheck1, err = verifyNLA(target, rdpPayload); err != nil { if nlaCheck1, err = mod.verifyNLA(target, rdpPayload); err != nil {
return true, err return true, err
} else if nlaCheck2, err = verifyNLA(target, tlsPayload); err != nil { } else if nlaCheck2, err = mod.verifyNLA(target, tlsPayload); err != nil {
return true, err return true, err
} }
@ -184,7 +193,7 @@ func (mod *RdpProxy) startProxyInstance(client string, target string) (err error
// Wont't handle things like "port already in use" since it happens at runtime // Wont't handle things like "port already in use" since it happens at runtime
mod.Error("PyRDP Start error : %v", err.Error()) mod.Error("PyRDP Start error : %v", err.Error())
NewRdpProxyEvent(client, target, "Failed to start PyRDP, won't intercept target").Push() NewRdpProxyEvent(client, target, "Failed to start PyRDP, won't intercept target.").Push()
return err return err
} }
@ -252,6 +261,13 @@ func (mod *RdpProxy) configureFirewall(enable bool) (err error) {
"-p", "tcp", "-m", "tcp", "--dport", fmt.Sprintf("%d", mod.port), "-p", "tcp", "-m", "tcp", "--dport", fmt.Sprintf("%d", mod.port),
"-j", "NFQUEUE", "--queue-num", fmt.Sprintf("%d", mod.queueNum), "--queue-bypass", "-j", "NFQUEUE", "--queue-num", fmt.Sprintf("%d", mod.queueNum), "--queue-bypass",
}, },
// This rule tries to fix an optimization bug in recent versions of iptables
// The bug : if no rules in the nat table tries to modify the current packet, skip the nable
// The NFQueue doesn't count as a modification.
{ "-t", "nat", "-A", "BCAPRDP",
"-p", "tcp", "-m", "tcp", "-d", "127.0.0.1", "--dport", "3388",
"-j", "REDIRECT", "--to-ports", "62884",
},
} }
} else if !enable { } else if !enable {
@ -316,8 +332,11 @@ func (mod *RdpProxy) Configure() (err error) {
return return
} else if _, err = exec.LookPath(mod.cmd); err != nil { } else if _, err = exec.LookPath(mod.cmd); err != nil {
return return
} else if _, err = mod.fileExists(mod.cmd); err != nil {
return
} }
if mod.nlaMode == "RELAY" { if mod.nlaMode == "RELAY" {
mod.Info("Mode RELAY is unimplemented yet, fallbacking to mode IGNORE.") mod.Info("Mode RELAY is unimplemented yet, fallbacking to mode IGNORE.")
mod.nlaMode = "IGNORE" mod.nlaMode = "IGNORE"
@ -382,7 +401,7 @@ func (mod *RdpProxy) handleRdpConnection(payload *nfqueue.Payload) int {
// TODO : Find a way to disconnect user right after stealing credentials. // TODO : Find a way to disconnect user right after stealing credentials.
// Start a PyRDP instance to the preconfigured vulnerable host // Start a PyRDP instance to the preconfigured vulnerable host
// and forward packets to the target to this host instead // and forward packets to the target to this host instead
NewRdpProxyEvent(client, target, "Target has NLA enabled and mode REDIRECT, forwarding to the vulnerable host...").Push() NewRdpProxyEvent(client, target, "Target has NLA enabled and mode REDIRECT, forwarding to the vulnerable host.").Push()
redirectTarget := fmt.Sprintf("%s:%d", mod.redirectIP.String(), mod.redirectPort) redirectTarget := fmt.Sprintf("%s:%d", mod.redirectIP.String(), mod.redirectPort)
err := mod.startProxyInstance(client, redirectTarget) err := mod.startProxyInstance(client, redirectTarget)
@ -399,12 +418,13 @@ func (mod *RdpProxy) handleRdpConnection(payload *nfqueue.Payload) int {
mod.startPort += 1 mod.startPort += 1
default: default:
// Add an exception in the firewall to avoid intercepting packets to this destination and port // Add an exception in the firewall to avoid intercepting packets to this destination and port
NewRdpProxyEvent(client, target, "Target has NLA enabled and mode IGNORE, won't intercept").Push() NewRdpProxyEvent(client, target, "Target has NLA enabled and mode IGNORE, won't intercept.").Push()
mod.doReturn(dst, dport) mod.doReturn(dst, dport)
} }
} else { } else {
// Starts a PyRDP instance. // Starts a PyRDP instance.
NewRdpProxyEvent(client, target, "Target doesn't have NLA enabled, intercepting.").Push()
if err := mod.startProxyInstance(client, target); err != nil { if err := mod.startProxyInstance(client, target); err != nil {
// Add an exception in the firewall to avoid intercepting packets to this destination and port // Add an exception in the firewall to avoid intercepting packets to this destination and port
mod.doReturn(dst, dport) mod.doReturn(dst, dport)
@ -419,7 +439,7 @@ func (mod *RdpProxy) handleRdpConnection(payload *nfqueue.Payload) int {
} }
} }
} else { } else {
NewRdpProxyEvent(client, target, "Non-target, won't intercept").Push() NewRdpProxyEvent(client, target, "Non-target, won't intercept.").Push()
// Add an exception in the firewall to avoid intercepting packets to this destination and port // Add an exception in the firewall to avoid intercepting packets to this destination and port
@ -441,7 +461,7 @@ func (mod *RdpProxy) Start() error {
if mod.Running() { if mod.Running() {
return session.ErrAlreadyStarted(mod.Name()) return session.ErrAlreadyStarted(mod.Name())
} else if err := mod.Configure(); err != nil { } else if err := mod.Configure(); err != nil {
mod.Error("%s", err.Error()) mod.Fatal("%s", err.Error())
return err return err
} }