-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
215 lines (198 loc) · 4.83 KB
/
main.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
// Copyright 2017 github.com/DirkDuesentrieb
// license that can be found in the LICENSE file.
// a converter for FortiGate session logs to pcap files
package main
import (
"bufio"
"encoding/hex"
"fmt"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
"time"
)
const (
globalHeader string = "d4c3b2a1020004000000000000000000ee05000001000000"
info string = `fgsniffer [-fixvpn]
Convert text captures to pcap files.
On the fortigate run
diagnose sniffer packet <interface> '<filter>' <3|6> <count> a
to create a parsable dump.
-fixvpn adds virtual MAC addresses to avoid unparsable dumps on VPN/tunnel interfaces.`
unsafe string = "[]{}/\\*"
)
type (
pcaps struct {
pcap map[string]int
}
packet struct {
data string // raw hex data
size int64
secs, ms int64 // the packets timestamp
port string // the network port (verbose=6)
}
)
var fixvpn bool
func main() {
var scanner *bufio.Scanner
var p packet
now := time.Now()
fname := os.Args[1]
if len(os.Args) > 1 {
switch os.Args[1] {
case "-?", "-h":
fmt.Println(info)
os.Exit(0)
case "-fixvpn":
fixvpn = true
fname = os.Args[2]
fallthrough
default:
f, err := os.Open(fname)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
scanner = bufio.NewScanner(f)
}
} else {
scanner = bufio.NewScanner(os.Stdin)
}
hexLine := regexp.MustCompile("^0x([0-9a-f]+)[ |\t]+(.*$)")
// absolute time
headLineA := regexp.MustCompile("^([0-9-]+ [0-9][0-9]:[0-9][0-9]:[0-9][0-9])\\.([0-9]+) .*$")
// relative time
headLineR := regexp.MustCompile("^([0-9]+)\\.([0-9]+) .*$")
// verbose mode 6
headLine6 := regexp.MustCompile("\\.[0-9]+ ([^ ]+) (in|out|--) ")
pcps := pcaps{make(map[string]int)}
for scanner.Scan() {
date := ""
mseconds := ""
iface := ""
match := false
line := scanner.Text()
hexData := hexLine.FindStringSubmatch(line)
// packet header with absolute time
header := headLineA.FindStringSubmatch(line)
if len(header) == 3 {
match = true
date = header[1]
mseconds = header[2]
}
// packet header with relative time
header = headLineR.FindStringSubmatch(line)
if len(header) == 3 {
match = true
sec, err := time.ParseDuration(header[1] + "s")
if err != nil {
fmt.Println("time.ParseDuration("+header[1]+")", err)
}
date = now.Add(sec).In(time.UTC).Format("2006-01-02 15:04:05")
mseconds = header[2]
}
// verbose mode 6
header = headLine6.FindStringSubmatch(line)
if match && len(header) == 3 {
iface = header[1]
}
if match {
pcps.addPacket(p)
p = newPacket(date, mseconds, iface)
if fixvpn {
p.addData("00090f09010100090f0902020800")
}
}
// packet hex data
if len(hexData) == 3 {
p.addData(strings.Replace(hexData[2][:39], " ", "", -1))
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, err)
return
}
// clean up
pcps.addPacket(p)
for name, packets := range pcps.pcap {
fmt.Println("created output file", name, "with", packets, "packets.")
}
}
// create file, add global header
func (pcps *pcaps) newPcap(name string) (err error) {
err = ioutil.WriteFile(name, nil, 0644)
if err != nil {
fmt.Println(err)
}
err = appendStringToFile(name, globalHeader)
if err != nil {
fmt.Println(err)
}
return err
}
// create a new packet. We need some data from the header
func newPacket(date, mseconds, iface string) packet {
t, _ := time.Parse("2006-01-02 15:04:05", date)
ms, _ := strconv.ParseInt(mseconds, 10, 64)
return packet{"", 0, t.Unix(), ms, iface}
}
// add a data line to the packet
func (p *packet) addData(data string) {
p.size += int64(len(data) / 2)
p.data += data
}
// all hex lines complete, write the packet to the pcap
func (pcps *pcaps) addPacket(p packet) {
if p.size == 0 {
return
}
fname := "fgsniffer"
if p.port != "" {
for i := 0; i < len(unsafe); i++ {
p.port = strings.Replace(p.port, string(unsafe[i]), "_", -1)
}
fname += "-" + p.port
}
fname += ".pcap"
_, found := pcps.pcap[fname]
if !found {
pcps.pcap[fname] = 0
_ = pcps.newPcap(fname)
}
header := switchEndian(p.secs) + switchEndian(p.ms) + switchEndian(p.size) + switchEndian(p.size)
err := appendStringToFile(fname, header+p.data)
if err != nil {
fmt.Println(err)
}
pcps.pcap[fname]++
}
// 11259375 -> 00abcdef -> efcdab00
func switchEndian(n int64) (r string) {
b := fmt.Sprintf("%08x", n)
for i := 0; i < 4; i++ {
start := 6 - 2*i
r = r + b[start:start+2]
}
return
}
// convert the hex data in string to binary and write it to file
func appendStringToFile(file, text string) error {
f, err := os.OpenFile(file, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if err != nil {
return err
}
defer f.Close()
src := []byte(text)
dst := make([]byte, hex.DecodedLen(len(src)))
_, err = hex.Decode(dst, src)
if err != nil {
return err
}
_, err = f.Write(dst)
if err != nil {
return err
}
return nil
}