added interactive port argument, so it is either random by default or selected. Added static build to the makefile

This commit is contained in:
Stefan Etringer 2025-06-25 14:38:34 +00:00
parent 653bd91e94
commit 9a8161b54c
3 changed files with 58 additions and 53 deletions

View File

@ -16,6 +16,10 @@ build: ## Build the application
# $(GO_BUILD) -o $(BINARY) # $(GO_BUILD) -o $(BINARY)
$(GO_BUILD) -ldflags "-w" -o $(BINARY) $(GO_BUILD) -ldflags "-w" -o $(BINARY)
build-static: ## Build a static application
@echo "Building static $(APP_NAME)..."
$(GO_BUILD) --ldflags '-linkmode=external -extldflags=-static -w' -o $(BINARY)-static
install: build ## Install the application install: build ## Install the application
@echo "Installing $(APP_NAME)..." @echo "Installing $(APP_NAME)..."
$(GO_INSTALL) $(GO_INSTALL)

74
main.go
View File

@ -6,6 +6,7 @@ import (
"bytes" "bytes"
"embed" "embed"
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"html/template" "html/template"
"io" "io"
@ -18,6 +19,7 @@ import (
"os/user" "os/user"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv"
"strings" "strings"
"sync" "sync"
@ -491,7 +493,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
tmpl.Execute(w, data) tmpl.Execute(w, data)
} }
func startInteractiveServer() (int, net.Listener) { func startInteractiveServer(cliInteractivePort string) (string, net.Listener) {
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
http.HandleFunc("/upload", fileUploadHandler) http.HandleFunc("/upload", fileUploadHandler)
@ -499,37 +501,63 @@ func startInteractiveServer() (int, net.Listener) {
http.HandleFunc("/terminal", terminalHandler) http.HandleFunc("/terminal", terminalHandler)
http.Handle("/static/", http.FileServer(http.FS(staticFiles))) http.Handle("/static/", http.FileServer(http.FS(staticFiles)))
listener, err := net.Listen("tcp", ":0") var addPort string
if err != nil { var listener net.Listener
log.Fatal(err) var err error
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)
} }
port := listener.Addr().(*net.TCPAddr).Port if err != nil {
log.Println("Using port:", port) log.Fatal(err)
}
return port, listener return addPort, listener
}
// func connectionArgs () (string, string) {
func connectionArgs () (string, string) {
// Get IP address and port of the server through the
var serverAddress string
// var serverPort string
var serverWebsocketPort string
var interactivePort string
flag.StringVar(&serverAddress, "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, "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.Parse()
fmt.Println("Server address is at ", serverAddress)
// fmt.Println("Server web port is ", serverPort)
fmt.Println("Server websocket port is ", serverWebsocketPort)
// webServerAddr := serverAddress + ":" + serverPort
webSocketAddr := serverAddress + ":" + serverWebsocketPort
// return webServerAddr, webSocketAddr
return webSocketAddr, interactivePort
} }
func main() { func main() {
// http.HandleFunc("/", handler)
// http.HandleFunc("/upload", fileUploadHandler)
// http.HandleFunc("/download", fileDownloadHandler)
// http.HandleFunc("/terminal", terminalHandler)
// http.Handle("/static/", http.FileServer(http.FS(staticFiles)))
// // fmt.Println("Starting server on :8080")
// // log.Fatal(http.ListenAndServe(":8080", nil))
// /* This section opens the server on a random port which is also free to use */ webSocketAddr, cliInteractivePort := connectionArgs()
// listener, err := net.Listen("tcp", ":0")
// if err != nil {
// panic(err)
// }
// log.Println("Using port:", listener.Addr().(*net.TCPAddr).Port)
// log.Fatal(http.Serve(listener, nil))
port, listener := startInteractiveServer() addPort, listener := startInteractiveServer(cliInteractivePort)
log.Println("Using interactive port:", addPort)
log.Printf("You can connect to port %d through your browser as well", listener.Addr().(*net.TCPAddr).Port)
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(2) wg.Add(2)
@ -541,7 +569,7 @@ func main() {
go func() { go func() {
defer wg.Done() defer wg.Done()
agentconnector.StartServer(port) agentconnector.StartServer(addPort, webSocketAddr)
}() }()
wg.Wait() wg.Wait()

View File

@ -2,7 +2,7 @@ package agentconnector
import ( import (
"encoding/json" "encoding/json"
"flag" // "flag"
"fmt" "fmt"
"log" "log"
// "net/http" // "net/http"
@ -210,43 +210,16 @@ func GetLocalIPs() []net.IP {
return ips return ips
} }
// func connectionArgs () (string, string) {
func connectionArgs () string {
// Get IP address and port of the server through the
var serverAddress string
// var serverPort string
var serverWebsocketPort string
flag.StringVar(&serverAddress, "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, "port", "5555", "Websocket port of the C2 server")
flag.Parse()
fmt.Println("Server address is at ", serverAddress)
// fmt.Println("Server web port is ", serverPort)
fmt.Println("Server websocket port is ", serverWebsocketPort)
// webServerAddr := serverAddress + ":" + serverPort
webSocketAddr := serverAddress + ":" + serverWebsocketPort
// return webServerAddr, webSocketAddr
return webSocketAddr
}
// func main() { // func main() {
func StartServer(agentInteractivePort int){ func StartServer(addPort, webSocketAddr string){
webSocketAddr := connectionArgs() // webSocketAddr, cliInteractivePort := connectionArgs()
// agentInteractivePort is only needed for interactive sessions // 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() agentIp := GetLocalIP().String()
agentType := "Interactive" agentType := "Interactive"
addPort := strconv.Itoa(agentInteractivePort)
hostname, _ := os.Hostname() hostname, _ := os.Hostname()
log.Printf("AgentId: %s", agentId) log.Printf("AgentId: %s", agentId)