/
dump.go
116 lines (109 loc) · 2.7 KB
/
dump.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
package cmd
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/desertbit/grumble"
"github.com/fatih/color"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcapgo"
"github.com/lesnuages/yuki/parser"
)
func dumpToFile(c *grumble.Context, isPcap bool) (err error) {
var (
file *os.File
s parser.Session
)
if s, err = Parser.GetSession(); err != nil {
return err
}
filepath := c.Args.String("filepath")
if filepath == "" {
return fmt.Errorf("You must provide a filepath")
}
if file, err = os.Create(filepath); err != nil {
return err
}
defer file.Close()
if isPcap {
writer := pcapgo.NewWriter(file)
writer.WriteFileHeader((uint32)(len(s.Packets)), layers.LinkTypeEthernet)
for _, p := range s.Packets {
writer.WritePacket(p.Metadata().CaptureInfo, p.Data())
}
} else {
for _, p := range s.Packets {
file.Write(p.TransportLayer().LayerPayload())
}
}
headline := color.New(color.FgGreen, color.Bold)
headline.Println("[*] File written to", filepath)
return nil
}
func writeToPcap(c *grumble.Context) (err error) {
return dumpToFile(c, true)
}
func writeToFile(c *grumble.Context) (err error) {
return dumpToFile(c, false)
}
func dirCompleter(prefix string, args []string) []string {
var (
res []string
currentDir string
)
idx := strings.LastIndexAny(prefix, "/")
if idx == -1 {
currentDir = "./"
} else {
if len(prefix) > 0 {
currentDir = prefix[:idx+1]
}
}
if files, err := ioutil.ReadDir(currentDir); err == nil {
for _, file := range files {
if strings.HasPrefix(currentDir+file.Name(), prefix) {
index := strings.LastIndex(currentDir+file.Name(), prefix)
if index == -1 {
res = append(res, file.Name())
} else if index < len(currentDir+file.Name()) {
filename := currentDir + file.Name()[index:]
res = append(res, filename)
}
}
}
} else {
fmt.Println(err)
}
return res
}
func init() {
writePcap := &grumble.Command{
Name: "writepcap",
Help: "Export the current session to a pcap file",
LongHelp: `Usage: writepcap FILEPATH
Write the selected session packets to a new pcap file,
located at the provided FILEPATH.
`,
Run: writeToPcap,
Completer: dirCompleter,
Args: func(a *grumble.Args) {
a.String("filepath", "path to a PCAP file", grumble.Default(""))
},
}
dump := &grumble.Command{
Name: "dump",
Help: "Dump transport layer data to a file",
LongHelp: `Usage: dump FILEPATH
Write the transport layer data to a new file,
located at the provided FILEPATH.
`,
Run: writeToFile,
Completer: dirCompleter,
Args: func(a *grumble.Args) {
a.String("filepath", "path to a PCAP file", grumble.Default(""))
},
}
App.AddCommand(writePcap)
App.AddCommand(dump)
}