-
Notifications
You must be signed in to change notification settings - Fork 158
/
server.go
253 lines (213 loc) · 6.22 KB
/
server.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
/*
* NETCAP - Traffic Analysis Framework
* Copyright (c) 2017 Philipp Mieden <dreadl0ck [at] protonmail [dot] ch>
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package main
import (
"bytes"
"compress/gzip"
"context"
"encoding/hex"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"os"
"path/filepath"
"strings"
"github.com/dreadl0ck/cryptoutils"
"github.com/dreadl0ck/netcap/types"
"github.com/gogo/protobuf/proto"
)
var (
flagGenKeypair = flag.Bool("gen-keypair", false, "generate keypair")
flagPrivKey = flag.String("privkey", "", "path to the hex encoded server private key")
flagAddr = flag.String("addr", "127.0.0.1:1335", "specify an adress and port to listen for incoming traffic")
files = make(map[string]*AuditRecordHandle)
// not configurable at the moment
// flagCompress = flag.Bool("comp", true, "compress data when writing to disk")
// flagBuffer = flag.Bool("buf", true, "buffer data before writing to disk")
)
// maxBufferSize specifies the size of the buffers that
// are used to temporarily hold data from the UDP packets
// that we receive.
const (
maxBufferSize = 10 * 1024
)
func main() {
flag.Parse()
if *flagGenKeypair {
// generate a new keypair
pub, priv, err := cryptoutils.GenerateKeypair()
if err != nil {
panic(err)
}
// write public key to file on disk
pubFile, err := os.Create("pub.key")
if err != nil {
panic(err)
}
pubFile.WriteString(hex.EncodeToString(pub[:]))
// close file handle
err = pubFile.Close()
if err != nil {
panic(err)
}
// write private key to file on disk
privFile, err := os.Create("priv.key")
if err != nil {
panic(err)
}
privFile.WriteString(hex.EncodeToString(priv[:]))
// close file handle
err = privFile.Close()
if err != nil {
panic(err)
}
fmt.Println("wrote keys")
return
}
// serve
ctx := context.Background()
log.Fatal(udpServer(ctx, *flagAddr))
}
// udpServer implements a simple UDP server
func udpServer(ctx context.Context, address string) (err error) {
// ListenPacket provides us a wrapper around ListenUDP so that
// we don't need to call `net.ResolveUDPAddr` and then subsequentially
// perform a `ListenUDP` with the UDP address.
//
// The returned value (PacketConn) is pretty much the same as the one
// from ListenUDP (UDPConn) - the only difference is that `Packet*`
// methods and interfaces are more broad, also covering `ip`.
pc, err := net.ListenPacket("udp", address)
if err != nil {
return
}
// `Close`ing the packet "connection" means cleaning the data structures
// allocated for holding information about the listening socket.
defer pc.Close()
var (
doneChan = make(chan error, 1)
buffer = make([]byte, maxBufferSize)
)
// run cleanup on signals
handleSignals()
// read private key file contents
privKeyContents, err := ioutil.ReadFile(*flagPrivKey)
if err != nil {
panic(err)
}
// hex decode private key
var serverPrivKey [cryptoutils.KeySize]byte
_, err = hex.Decode(serverPrivKey[:], privKeyContents)
if err != nil {
panic(err)
}
// Given that waiting for packets to arrive is blocking by nature and we want
// to be able of canceling such action if desired, we do that in a separate
// go routine.
go func() {
for {
// By reading from the connection into the buffer, we block until there's
// new content in the socket that we're listening for new packets.
//
// Whenever new packets arrive, `buffer` gets filled and we can continue
// the execution.
//
// note.: `buffer` is not being reset between runs.
// It's expected that only `n` reads are read from it whenever
// inspecting its contents.
n, addr, err := pc.ReadFrom(buffer)
if err != nil {
doneChan <- err
return
}
fmt.Printf("packet-received: bytes=%d from=%s\n", n, addr.String())
// create a copy of the data to allow reusing the buffer for the next incoming packet
var copyBuf = make([]byte, n)
copy(copyBuf, buffer[:n])
buf := bytes.NewBuffer(copyBuf)
// spawn a new goroutine to handle packet data
go func() {
// trim off the public key of the peer
var pubKeyClient = [32]byte{}
for i, b := range buf.Bytes() {
if i == 32 {
break
}
pubKeyClient[i] = b
}
// decrypt
decrypted, ok := cryptoutils.AsymmetricDecrypt(buf.Bytes()[32:], &pubKeyClient, &serverPrivKey)
if !ok {
panic("decryption failed")
}
var decryptedBuf = bytes.NewBuffer(decrypted)
// create a new gzipped reader
gr, err := gzip.NewReader(decryptedBuf)
if err != nil {
fmt.Println(hex.Dump(decryptedBuf.Bytes()))
fmt.Println("gzip error", err)
return
}
// read data
c, err := ioutil.ReadAll(gr)
if err == io.EOF || err == io.ErrUnexpectedEOF {
fmt.Println("failed to decompress batch", err)
return
} else if err != nil {
fmt.Println(hex.Dump(buf.Bytes()))
fmt.Println("gzip error", err)
return
}
// close reader
err = gr.Close()
if err != nil {
panic(err)
}
// init new batch
b := new(types.Batch)
// unmarshal batch data
err = proto.Unmarshal(c, b)
if err == io.EOF || err == io.ErrUnexpectedEOF {
fmt.Println("failed to unmarshal batch", err)
return
} else if err != nil {
panic(err)
}
fmt.Println("decoded batch", b.MessageType, "from client", b.ClientID)
var (
protocol = strings.TrimPrefix(b.MessageType.String(), "NC_")
path = filepath.Join(b.ClientID, protocol+".ncap.gz")
)
if a, ok := files[path]; ok {
_, err := a.gWriter.Write(b.Data)
if err != nil {
panic(err)
}
} else {
files[path] = NewAuditRecordHandle(b, path)
}
}()
}
}()
select {
case <-ctx.Done():
fmt.Println("cancelled")
err = ctx.Err()
case err = <-doneChan:
cleanup()
}
return
}