-
Notifications
You must be signed in to change notification settings - Fork 2
/
pcapeek.go
123 lines (114 loc) · 4.21 KB
/
pcapeek.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
package main
import (
"fmt"
"os"
"strings"
"github.com/0xThiebaut/PCAPeek/application"
"github.com/0xThiebaut/PCAPeek/application/reverse"
"github.com/0xThiebaut/PCAPeek/application/rfb"
"github.com/0xThiebaut/PCAPeek/output/files"
"github.com/0xThiebaut/PCAPeek/output/files/binary"
"github.com/0xThiebaut/PCAPeek/output/files/fork"
"github.com/0xThiebaut/PCAPeek/output/media"
mfork "github.com/0xThiebaut/PCAPeek/output/media/fork"
"github.com/0xThiebaut/PCAPeek/output/media/jpeg"
"github.com/0xThiebaut/PCAPeek/output/media/mjpeg"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"github.com/google/gopacket/tcpassembly"
"github.com/spf13/cobra"
)
var (
// PCAP settings
bpf string
// JPEG output
useJpeg = false
jpegDir = `./`
jpegQuality = 100
jpegFps = 0
// MJPEG
useMjpeg = false
mjpegDir = `./`
mjpegQuality = 100
mjpegFps = 10
// Files
useFiles = false
filesDir = `./`
)
var rootCmd = &cobra.Command{
Use: "PCAPeek PCAP [PCAP ...]",
Short: "PCAPeek peeks into PCAPs",
Long: `PCAPeek is a tool to peek into PCAPs. It doesn't do much besides acting as a proof of concept to reconstruct reverse VNC traffic.`,
Args: cobra.MinimumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Trim any BPF spaces
bpf = strings.TrimSpace(bpf)
// Prepare the media
var mo []media.Factory
if useJpeg {
mo = append(mo, jpeg.New(jpegDir, jpegFps, jpegQuality))
}
if useMjpeg {
mo = append(mo, mjpeg.New(mjpegDir, mjpegFps, mjpegQuality))
}
// Prepare the files
var fo []files.Factory
if useFiles {
fo = append(fo, binary.New(filesDir))
}
// TODO: Abstract VNC extraction
rrfb := reverse.New(rfb.New(mfork.New(mo...), fork.New(fo...)))
pool := tcpassembly.NewStreamPool(application.NewApplicationStreamFactory(false, rrfb))
assembler := tcpassembly.NewAssembler(pool)
// Loop the PCAPs
for _, file := range args {
// Open the file
handle, err := pcap.OpenOffline(file)
if err != nil {
return err
}
// Apply any BPF filters
if len(bpf) > 0 {
if err = handle.SetBPFFilter(bpf); err != nil {
return err
}
}
// Create the PCAP source
decoder := gopacket.DecodersByLayerName[handle.LinkType().String()]
source := gopacket.NewPacketSource(handle, decoder)
source.Lazy = true
source.NoCopy = true
// Pipe the PCAPs
for packet := range source.Packets() {
if packet.NetworkLayer() == nil || packet.TransportLayer() == nil || packet.TransportLayer().LayerType() != layers.LayerTypeTCP {
continue
}
tcp, ok := packet.TransportLayer().(*layers.TCP)
if !ok {
continue
}
// Set factory time here for each packet allowing on confirmed read to get the current timestamp or on first read to set the timestamp
assembler.AssembleWithTimestamp(packet.NetworkLayer().NetworkFlow(), tcp, packet.Metadata().Timestamp)
}
}
return nil
},
}
func main() {
rootCmd.PersistentFlags().StringVar(&bpf, `filter`, bpf, `A BPF filter to apply on the PCAPs`)
rootCmd.PersistentFlags().BoolVar(&useJpeg, `jpeg`, useJpeg, `Output JPEG frames`)
rootCmd.PersistentFlags().StringVar(&jpegDir, `jpeg-dir`, jpegDir, `The output directory for the JPEG frames`)
rootCmd.PersistentFlags().IntVar(&jpegQuality, `jpeg-quality`, jpegQuality, `The JPEG frame quality percentage`)
rootCmd.PersistentFlags().IntVar(&jpegFps, `jpeg-fps`, jpegFps, `The number of JPEG frames to output per second (default 0, outputs all frames)`)
rootCmd.PersistentFlags().BoolVar(&useMjpeg, `mjpeg`, useMjpeg, `Output MJPEG videos`)
rootCmd.PersistentFlags().StringVar(&mjpegDir, `mjpeg-dir`, mjpegDir, `The output directory for the MJPEG videos`)
rootCmd.PersistentFlags().IntVar(&mjpegQuality, `mjpeg-quality`, mjpegQuality, `The MJPEG video quality percentage`)
rootCmd.PersistentFlags().IntVar(&mjpegFps, `mjpeg-fps`, mjpegFps, `The number of MJPEG frames to output per second`)
rootCmd.PersistentFlags().BoolVar(&useFiles, `files`, useFiles, `Output clipboard files`)
rootCmd.PersistentFlags().StringVar(&filesDir, `files-dir`, filesDir, `The output directory for the clipboard files`)
if err := rootCmd.Execute(); err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}