-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
175 lines (133 loc) · 3.9 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
package main
import (
"fmt"
"github.com/fatih/color"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"flag"
"strings"
"encoding/binary"
)
type Options struct {
captureInterface string
capturePort int
verbose bool
captureFile string
}
var options Options
func main() {
flag.Parse()
//FIXME introduce logger instead of writing directly anf commandline option to be silent
//TODO dump files
//TODO Colorize Traffic --> Show short overview on packages
//TODO Verbose mode
//FIXME Pinning of 3rd party dependencies
var (
handle *pcap.Handle
err error
)
if options.captureFile != "" {
handle, err = pcap.OpenOffline(options.captureFile)
if err != nil {
color.Red("[e]\tCould not capture file %s", options.captureFile)
color.Red("\t%s",err)
}
} else {
handle, err = pcap.OpenLive(options.captureInterface, 64000, true, pcap.BlockForever)
if err != nil {
color.Red("[e]\tCould not open device %s", options.captureInterface)
color.Red("\t%s",err)
} else {
color.HiGreen("[i]\tADBDump will start capturing on device: %s and port %d", options.captureInterface, options.capturePort)
}
}
if handle != nil {
defer handle.Close()
filter := fmt.Sprintf("port %d", options.capturePort)
logD(fmt.Sprintf("Using \"%s\" as BPF Filter", filter))
e := handle.SetBPFFilter(filter)
if e != nil {
//FIXME Better error handling needed
panic(e)
}
// Loop through packets in file
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
for packet := range packetSource.Packets() {
tcpLayer := packet.Layer(layers.LayerTypeTCP)
if tcpLayer != nil {
tcp, _ := tcpLayer.(*layers.TCP)
payload := tcp.Payload
handlePayload(payload, tcp)
}
}
}
}
var (
syncInProgress bool = false
dataLen uint32 = 0
)
func handlePayload(payload []byte, tcp *layers.TCP) {
// fmt.Println(len(payload))
if len(payload) > 0 {
line :=string(payload)
line = strings.TrimSpace(line)
if tcp.DstPort == 5037 { //Snip first 4 bytes
payload = payload[4:]
if strings.LastIndex(line, "SEND") == 0{
dataLen =binary.LittleEndian.Uint32(payload[4:8])
color.Red("[ADB]%d\t%s", dataLen,line)
} else if strings.LastIndex(line, "DATA") == 0{
dataLen =binary.LittleEndian.Uint32(payload[4:8])
color.Red("[ADB]:%d\t", dataLen)
} else if strings.LastIndex(line, "shell") == 4{
color.Red("[ADB][%d]\t%s",tcp.SrcPort,line)
} else if strings.LastIndex(line, "host") == 4{
color.Red("[ADB]\t%s",line)
} else if strings.LastIndex(line, "sync") == 4{
color.Red("[ADB]\t%s",line)
syncInProgress = true
} else if len(payload) > 20 && !syncInProgress {
color.Blue("[ADB]\t%s",line[0:20])
} else if !syncInProgress {
color.Blue("[ADB]\t%s",line[0:8])
}
} else {
max := 20
if len(payload) < max{
max = len(payload)
}
if strings.HasPrefix(line, "INSTRUMENTATION"){
color.Green("[ANDR][%d]\t%s", tcp.DstPort, string(line))
} else if strings.HasPrefix(line, "["){
color.HiBlack("[ANDR][%d]\t%s", tcp.DstPort, string(line))
} else {
color.Yellow("[ANDR][%d]\t%s", tcp.DstPort, string(line))
if syncInProgress {
color.Yellow(string(payload))
if len(payload) == 8 {
color.Yellow("%d", binary.LittleEndian.Uint32(payload[4:8]))
syncInProgress = false
}
}
}
}
//max := 20
//if len(payload) < max{
// max = len(payload)
//}
if strings.Contains(line, "Exception"){
//fmt.Println(line)
}
}
}
func logD(s string) {
color.HiGreen("[i]\t%s", s )
}
func init() {
options = Options{}
flag.StringVar(&options.captureInterface, "d", "lo", "Device to capture on")
flag.IntVar(&options.capturePort, "p", 5037, "Port to capture on")
flag.BoolVar(&options.verbose, "v", false, "Log not only commands, also content of the communication")
flag.StringVar(&options.captureFile, "r", "" , "Read from file instead of ")
}