-
Notifications
You must be signed in to change notification settings - Fork 1
/
qnana.go
275 lines (241 loc) · 6.56 KB
/
qnana.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
package main
import (
"bufio"
"bytes"
"compress/gzip"
"container/list"
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"runtime"
"strconv"
"sync"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/tcpassembly"
"github.com/google/gopacket/tcpassembly/tcpreader"
)
type Req struct {
port int
url string
}
type Res struct {
port int
ctype []string
cencode []string
body []byte
}
var (
// commandline options
iface = flag.String("i", "eth0", "Listen on interface.")
fname = flag.String("r", "", "Read packets from file.")
snaplen = flag.Int("s", 1600, "Snarf bytes of data from each packet.")
filter = flag.String("f", "", "Selects which packets will be processed.")
reqList = list.New()
parserCh = make(chan Res)
)
// Build a simple HTTP request parser using tcpassembly.StreamFactory and tcpassembly.Stream interfaces
// httpStreamFactory implements tcpassembly.StreamFactory
type httpStreamFactory struct{}
// httpStream will handle the actual decoding of http requests.
type httpStream struct {
net, transport gopacket.Flow
r tcpreader.ReaderStream
}
func (h *httpStreamFactory) New(net, transport gopacket.Flow) tcpassembly.Stream {
hstream := &httpStream{
net: net,
transport: transport,
r: tcpreader.NewReaderStream(),
}
go hstream.run() // Important... we must guarantee that data from the reader stream is read.
// ReaderStream implements tcpassembly.Stream, so we can return a pointer to it.
return &hstream.r
}
func (h *httpStream) run() {
buf := bufio.NewReader(&h.r)
for {
switch h.transport.Dst().String() {
case "80": // request
r, err := http.ReadRequest(buf)
if err == io.EOF {
return
} else if err != nil {
log.Println("Error reading stream", h.net, h.transport, ":", err)
} else {
r.Body.Close()
port, _ := strconv.Atoi(h.transport.Src().String())
reqList.PushBack(Req{port, r.URL.Path})
}
default: // response
r, err := http.ReadResponse(buf, nil)
if err == io.ErrUnexpectedEOF {
return
} else if err != nil {
log.Println("Error reading stream", h.net, h.transport, ":", err)
} else {
var res Res
res.body, _ = ioutil.ReadAll(r.Body)
r.Body.Close()
if r.StatusCode != 200 {
log.Println("[Response]:", r.Status)
} else {
res.port, _ = strconv.Atoi(h.transport.Dst().String())
res.ctype = r.Header["Content-Type"]
res.cencode = r.Header["Content-Encoding"]
parserCh <- res
}
}
}
}
}
func parse(wait *sync.WaitGroup) {
wait.Add(1)
hasKey := func(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
for {
select {
case res := <-parserCh:
if res.port == 0 {
wait.Done()
return
}
var req Req
for e := reqList.Front(); e != nil; e = e.Next() {
if e.Value.(Req).port == res.port {
req = reqList.Remove(e).(Req)
break
}
}
if !hasKey("text/plain", res.ctype) {
continue
}
var b []byte
if hasKey("gzip", res.cencode) {
reader, err := gzip.NewReader(bytes.NewReader(res.body))
if err != nil {
log.Println("Failed to parse gzipped data: ", err)
}
b, err = ioutil.ReadAll(reader)
if err != nil {
log.Println("Failed to read gzipped data: ", err)
}
} else {
b = res.body
}
prefix := []byte("svdata=")
if !bytes.HasPrefix(b, prefix) {
log.Println("This body is not JSON data")
continue
}
b = bytes.TrimPrefix(b, prefix)
var data interface{}
if err := json.Unmarshal(b, &data); err != nil {
log.Println("Failed to parse json: ", err)
log.Printf("%s", b)
continue
}
switch req.url {
case "/kcsapi/api_port/port":
m := data.(map[string]interface{})
api_data := m["api_data"].(map[string]interface{})
api_deck_port := api_data["api_deck_port"].([]interface{})
deck1 := api_deck_port[0].(map[string]interface{})
deck1_list := deck1["api_ship"].([]interface{})
api_ship := api_data["api_ship"].([]interface{})
fmt.Printf("Deck1:\n")
for k, v := range deck1_list {
var ship map[string]interface{}
if v.(float64) < 0 {
continue
}
for _, s := range api_ship {
ship = s.(map[string]interface{})
if ship["api_id"] == v {
break
}
}
fmt.Printf(" %d: cond = %3.0f\n", k, ship["api_cond"])
}
default:
log.Println("Unknown API:", req.url)
}
}
}
}
func main() {
var handle *pcap.Handle
var err error
flag.Parse()
if *fname != "" {
log.Printf("Reading from pcap file %q", *fname)
handle, err = pcap.OpenOffline(*fname)
} else {
log.Printf("Starting capture on interface %q", *iface)
handle, err = pcap.OpenLive(*iface, int32(*snaplen), false, pcap.BlockForever)
}
if err != nil {
log.Fatal(err)
}
if *filter == "" {
// from kcs_const.js
*filter = "tcp port 80 and host"
*filter += " (125.6.184.15 or 125.6.184.16 or 125.6.187.205 or"
*filter += " 125.6.187.229 or 125.6.187.253 or 125.6.188.25 or"
*filter += " 125.6.189.7 or 125.6.189.39 or 125.6.189.71 or"
*filter += " 125.6.189.103 or 125.6.189.135 or 125.6.189.167 or"
*filter += " 125.6.189.215 or 125.6.189.247 or 203.104.209.7 or"
*filter += " 203.104.209.23 or 203.104.209.39 or 203.104.209.55 or"
*filter += " 203.104.209.71 or 203.104.209.102 or 203.104.248.135)"
}
if err := handle.SetBPFFilter(*filter); err != nil {
log.Fatal(err)
}
// Set up assembly
streamFactory := &httpStreamFactory{}
streamPool := tcpassembly.NewStreamPool(streamFactory)
assembler := tcpassembly.NewAssembler(streamPool)
// Set up paser goroutine
cpus := runtime.NumCPU()
runtime.GOMAXPROCS(cpus)
wait := new(sync.WaitGroup)
go parse(wait)
// Read in packets, pass to assembler.
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
packets := packetSource.Packets()
ticker := time.Tick(time.Minute)
for {
select {
case packet := <-packets:
// A nil packet indicates the end of a pcap file.
if packet == nil {
return
}
if packet.NetworkLayer() == nil || packet.TransportLayer() == nil {
continue
}
if packet.TransportLayer().LayerType() != layers.LayerTypeTCP {
continue
}
tcp := packet.TransportLayer().(*layers.TCP)
assembler.AssembleWithTimestamp(packet.NetworkLayer().NetworkFlow(), tcp, packet.Metadata().Timestamp)
case <-ticker:
// Every minute, flush connections that haven't seen activity in the past 2 minutes.
assembler.FlushOlderThan(time.Now().Add(time.Minute * -2))
}
}
parserCh <- Res{port:0}
wait.Wait()
}