-
Notifications
You must be signed in to change notification settings - Fork 88
/
retriever.go
98 lines (83 loc) · 1.68 KB
/
retriever.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package connection
import (
"bytes"
"encoding/json"
"fmt"
"log"
"math/rand"
"net"
"os/exec"
"time"
)
type NoTLSConnErr string
func (f NoTLSConnErr) Error() string {
return fmt.Sprintf("No TLS Conn Received")
}
func Connect(domain, cipherscanbinPath string) ([]byte, error) {
ip := getRandomIP(domain)
if ip == "" {
e := fmt.Errorf("Could not resolve ip for: %s", domain)
log.Println(e)
return nil, e
}
cmd := cipherscanbinPath + " --no-tolerance -j --curves -servername " + domain + " " + ip + ":443 "
log.Println(cmd)
comm := exec.Command("bash", "-c", cmd)
var out bytes.Buffer
var stderr bytes.Buffer
comm.Stdout = &out
comm.Stderr = &stderr
err := comm.Start()
if err != nil {
log.Println(stderr.String())
log.Println(err)
return nil, err
}
waiter := make(chan error, 1)
go func() {
waiter <- comm.Wait()
}()
select {
case <-time.After(3 * time.Minute):
err = fmt.Errorf("cipherscan timed out after 3 minutes on target %s %s", domain, ip)
return nil, err
case err := <-waiter:
if err != nil {
log.Println(err)
return nil, err
}
}
info := CipherscanOutput{}
err = json.Unmarshal([]byte(out.String()), &info)
if err != nil {
log.Println(err)
return nil, err
}
info.Target = domain
info.IP = ip
c, err := info.Stored()
if err != nil {
log.Println(err)
return nil, err
}
return json.Marshal(c)
}
func getRandomIP(domain string) string {
ips, err := net.LookupIP(domain)
if err != nil {
return ""
}
max := len(ips)
for {
if max == 0 {
return ""
}
index := rand.Intn(len(ips))
if ips[index].To4() != nil {
return ips[index].String()
} else {
ips = append(ips[:index], ips[index+1:]...)
}
max--
}
}