-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
172 lines (167 loc) · 4.5 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
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"path"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
homedir "github.com/mitchellh/go-homedir"
)
var (
credsPath = flag.String("credentialsFile", "", "path to aws credentials file")
outputPath = flag.String("out", "flow-logs.pcap", "path to output pcap file to")
prefix = flag.String("prefix", "", "prefix to use to find flow logs/streams")
profileName = flag.String("profile", "default", "profile to use in your aws credentials file")
region = flag.String("region", "us-east-1", "region to use")
zeroLen = flag.Bool("zero-len", true, "zero out packet lengths")
)
func main() {
flag.Parse()
if *credsPath == "" {
home, err := homedir.Dir()
if err != nil {
panic(err)
}
p := path.Join(home, ".aws", "credentials")
credsPath = &p
}
config := aws.NewConfig().WithCredentials(credentials.NewSharedCredentials(*credsPath, *profileName))
config = config.WithMaxRetries(3)
config = config.WithRegion(*region)
sess := session.Must(session.NewSession(config))
svc := cloudwatchlogs.New(sess, config)
groupsResp, err := svc.DescribeLogGroups(&cloudwatchlogs.DescribeLogGroupsInput{
LogGroupNamePrefix: prefix,
})
if err != nil {
panic(err)
}
lgName := groupsResp.LogGroups[0].LogGroupName
orderBy := "LastEventTime"
truePtr := true
streamsResp, err := svc.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
Descending: &truePtr,
LogGroupName: lgName,
OrderBy: &orderBy,
})
if err != nil {
panic(err)
}
stream := streamsResp.LogStreams[0]
eventsResp, err := svc.GetLogEvents(&cloudwatchlogs.GetLogEventsInput{
LogGroupName: lgName,
LogStreamName: stream.LogStreamName,
StartFromHead: &truePtr,
})
if err != nil {
panic(err)
}
file, err := os.OpenFile(*outputPath, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
defer file.Close()
w := pcapgo.NewWriter(file)
if err := w.WriteFileHeader(65536, layers.LinkTypeEthernet); err != nil {
panic(err)
}
for _, e := range eventsResp.Events {
components := strings.Split(*e.Message, " ")
lengthInt := 6
if !*zeroLen {
var err error
lengthInt, err = strconv.Atoi(components[9])
if err != nil {
log.Println("got err", err)
continue
}
}
tsInt, err := strconv.Atoi(components[10])
if err != nil {
log.Println("got err", err)
continue
}
srcPortInt, err := strconv.Atoi(components[5])
if err != nil {
log.Println("got err", err)
continue
}
dstPortInt, err := strconv.Atoi(components[6])
if err != nil {
log.Println("got err", err)
continue
}
packetsInt, err := strconv.Atoi(components[8])
if err != nil {
log.Println("got err", err)
continue
}
protoInt, err := strconv.Atoi(components[7])
if err != nil {
log.Println("got err", err)
continue
}
fmt.Println("proto:", layers.IPProtocol(protoInt).String())
fmt.Println("num packets:", packetsInt)
for i := 0; i < packetsInt+1; i++ {
length := 0
if lengthInt > 65536 {
lengthInt = lengthInt - 65536 - 54
length = 65536
} else {
length = lengthInt + 54
}
p := gopacket.CaptureInfo{
CaptureLength: length,
InterfaceIndex: 0,
Length: length,
Timestamp: time.Unix(int64(tsInt), 0),
}
eth := &layers.Ethernet{
SrcMAC: net.HardwareAddr([]byte{0, 0, 0, 0, 0, 0}),
DstMAC: net.HardwareAddr([]byte{255, 255, 255, 255, 255, 255}),
EthernetType: layers.EthernetTypeIPv4,
}
ip := &layers.IPv4{
Checksum: 0x4324,
DstIP: net.ParseIP(components[4]),
Flags: layers.IPv4DontFragment,
FragOffset: 0,
Id: uint16(i),
Length: 52,
Protocol: layers.IPProtocol(protoInt),
SrcIP: net.ParseIP(components[3]),
TTL: 42,
Version: 4,
}
tcp := &layers.TCP{
SrcPort: layers.TCPPort(srcPortInt),
DstPort: layers.TCPPort(dstPortInt),
}
payload := gopacket.Payload(make([]byte, length-54))
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{
FixLengths: true,
}
if err := gopacket.SerializeLayers(buf, opts, eth, ip, tcp, payload); err != nil {
log.Println("got err", err)
continue
}
if err := w.WritePacket(p, buf.Bytes()); err != nil {
log.Println("got err", err)
continue
}
}
}
}