-
Notifications
You must be signed in to change notification settings - Fork 1
/
hpfeeds-client.go
279 lines (239 loc) · 5.46 KB
/
hpfeeds-client.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
// based on https://github.com/hpfeeds/go-hpfeeds
//
// (2017) which was very broken
//
// By default, just dumps out the json payload from hpfeeds
// Reworked by Noah Axon in 2013
// [email protected] | IG: @4x0nn | Twitter: @ax0n
package main
import (
"bytes"
"crypto/sha1"
"encoding/binary"
"fmt"
"log"
"net"
"os"
"strconv"
)
type Message struct {
Name string
Payload []byte
}
type rawMsgHeader struct {
Length uint32
Opcode uint8
}
const (
opcode_err = 0
opcode_info = 1
opcode_auth = 2
opcode_pub = 3
opcode_sub = 4
)
type Hpfeeds struct {
LocalAddr net.TCPAddr
conn *net.TCPConn
host string
port int
ident string
auth string
authSent chan bool
Disconnected chan error
channel map[string]chan Message
Log bool
}
func NewHpfeeds(ident, auth, host string, port int) Hpfeeds {
return Hpfeeds{
host: host,
port: port,
ident: ident,
auth: auth,
authSent: make(chan bool),
Disconnected: make(chan error, 1),
channel: make(map[string]chan Message),
}
}
func (hp *Hpfeeds) Connect() error {
hp.clearDisconnected()
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", hp.host, hp.port))
if err != nil {
return err
}
conn, err := net.DialTCP("tcp", &hp.LocalAddr, addr)
if err != nil {
return err
}
hp.conn = conn
go hp.recvLoop()
<-hp.authSent
select {
case err = <-hp.Disconnected:
return err
default:
}
return nil
}
func (hp *Hpfeeds) clearDisconnected() {
select {
case <-hp.Disconnected:
default:
}
}
func (hp *Hpfeeds) setDisconnected(err error) {
hp.clearDisconnected()
hp.Disconnected <- err
}
// Close closes the hpfeeds connection and signals the Disconnected channel.
func (hp *Hpfeeds) Close() {
hp.close(nil)
}
func (hp *Hpfeeds) close(err error) {
hp.conn.Close()
hp.setDisconnected(err)
select {
case hp.authSent <- false:
default:
}
hp.conn = nil
}
func (hp *Hpfeeds) recvLoop() {
buf := []byte{}
for hp.conn != nil {
readbuf := make([]byte, 1024)
n, err := hp.conn.Read(readbuf)
if err != nil {
hp.log("Read(): %s\n", err)
hp.close(err)
return
}
buf = append(buf, readbuf[:n]...)
for len(buf) > 5 {
hdr := rawMsgHeader{}
hdr.Length = binary.BigEndian.Uint32(buf[0:4])
hdr.Opcode = uint8(buf[4])
if len(buf) < int(hdr.Length) {
break
}
data := buf[5:int(hdr.Length)]
hp.parse(hdr.Opcode, data)
buf = buf[int(hdr.Length):]
}
}
}
func (hp *Hpfeeds) parse(opcode uint8, data []byte) {
switch opcode {
case opcode_info:
hp.sendAuth(data[(1 + uint8(data[0])):])
hp.authSent <- true
case opcode_err:
hp.log("Received error from server: %s\n", string(data))
case opcode_pub:
len1 := uint8(data[0])
name := string(data[1:(1 + len1)])
len2 := uint8(data[1+len1])
channel := string(data[(1 + len1 + 1):(1 + len1 + 1 + len2)])
payload := data[1+len1+1+len2:]
hp.handlePub(name, channel, payload)
default:
hp.log("Received message with unknown type %d\n", opcode)
}
}
func (hp *Hpfeeds) handlePub(name string, channelName string, payload []byte) {
channel, ok := hp.channel[channelName]
if !ok {
hp.log("Received message on unsubscribed channel %s\n", channelName)
return
}
channel <- Message{name, payload}
}
func writeField(buf *bytes.Buffer, data []byte) {
buf.WriteByte(byte(len(data)))
buf.Write(data)
}
func (hp *Hpfeeds) sendRawMsg(opcode uint8, data []byte) {
buf := make([]byte, 5)
binary.BigEndian.PutUint32(buf, uint32(5+len(data)))
buf[4] = byte(opcode)
buf = append(buf, data...)
for len(buf) > 0 {
n, err := hp.conn.Write(buf)
if err != nil {
hp.log("Write(): %s\n", err)
hp.close(err)
return
}
buf = buf[n:]
}
}
func (hp *Hpfeeds) sendAuth(nonce []byte) {
buf := new(bytes.Buffer)
mac := sha1.New()
mac.Write(nonce)
mac.Write([]byte(hp.auth))
writeField(buf, []byte(hp.ident))
buf.Write(mac.Sum(nil))
hp.sendRawMsg(opcode_auth, buf.Bytes())
}
func (hp *Hpfeeds) sendSub(channelName string) {
buf := new(bytes.Buffer)
writeField(buf, []byte(hp.ident))
buf.Write([]byte(channelName))
hp.sendRawMsg(opcode_sub, buf.Bytes())
}
func (hp *Hpfeeds) sendPub(channelName string, payload []byte) {
buf := new(bytes.Buffer)
writeField(buf, []byte(hp.ident))
writeField(buf, []byte(channelName))
buf.Write(payload)
hp.sendRawMsg(opcode_pub, buf.Bytes())
}
func (hp *Hpfeeds) Subscribe(channelName string, channel chan Message) {
hp.channel[channelName] = channel
hp.sendSub(channelName)
}
func (hp *Hpfeeds) Publish(channelName string, channel chan []byte) {
go func() {
for payload := range channel {
if hp.conn == nil {
return
}
hp.sendPub(channelName, payload)
}
}()
}
func (hp *Hpfeeds) log(format string, v ...interface{}) {
if hp.Log {
log.Printf(format, v...)
}
}
func main() {
if len(os.Args) != 6 {
fmt.Printf("Usage: %s <ident> <authkey> <server> <port> <channel> (only one channel supported for now)\n", os.Args[0])
os.Exit(0)
}
ident := os.Args[1]
auth := os.Args[2]
host := os.Args[3]
port, err := strconv.Atoi(os.Args[4])
channel := os.Args[5]
if err != nil {
fmt.Println("Port must be an integer")
os.Exit(1)
}
hp := NewHpfeeds(ident, auth, host, port)
hp.Log = true
hp.Connect()
firehose := make(chan Message)
hp.Subscribe(channel, firehose)
go func() {
for foo := range firehose {
// If you really want the HPFeeds honeypot id, uncomment.
// fmt.Println(foo.Name, string(foo.Payload))
// I just want the json payload so we can do fun stuff with jq etc
fmt.Println(string(foo.Payload))
}
}()
// Wait for disconnect
<-hp.Disconnected
}