-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.go
180 lines (156 loc) · 4.48 KB
/
util.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
package main
import (
"crypto/sha1"
"encoding/base64"
"github.com/degenerat3/iftpp/pbuf"
"github.com/golang/protobuf/proto"
"golang.org/x/net/icmp"
"golang.org/x/net/ipv4"
"log"
"math/rand"
"net"
"sort"
"time"
)
// icmpConst is usually one, used for protocol marshalling (ipv6 support has a diff one)
const icmpConst int = 1
// timeout is how long the conn read will wait for before timing
const timeout int = 10
// readSize is the amount to read from the listener (always send less than this)
const readSize int = 1600
// calculate the sha1 sum of the data and use (almost) the last 8 chars as the checksum
func calcChecksum(data []byte) []byte {
hasher := sha1.New()
hasher.Write(data)
sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
chk := sha[len(sha)-9 : len(sha)-1]
return []byte(chk)
}
// calculate the shared key by combining keys, sort by descending, then taking sha1
func calcSharedKey(key1 []byte, key2 []byte) []byte {
combined := append(key1, key2...) // put two keys together
sort.Slice(combined, func(i int, j int) bool { return combined[i] > combined[j] }) // sort descending
hasher := sha1.New()
hasher.Write(combined)
sha := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
return []byte(sha)
}
// generate an icmp listener on the specified IP (usually 0.0.0.0)
func getListener(ip string) *icmp.PacketConn {
conn, err := icmp.ListenPacket("ip4:icmp", ip)
if err != nil {
log.Fatal(err)
}
return conn
}
// take in the required fields for an `IFTPP` message and proto-fy the data
func buildProto(sid int32, payld []byte, chksum []byte, typ pbuf.IFTPP_Flag) []byte {
testPro := &pbuf.IFTPP{
SessionId: sid,
Payload: payld,
Checksum: chksum,
TypeFlag: typ,
}
data, err := proto.Marshal(testPro)
if err != nil {
log.Fatal("marshaling error: ", err)
}
return data
}
// marshal the data into an `IFTPP` msg
func decodeProto(data []byte) *pbuf.IFTPP {
iftppMesage := &pbuf.IFTPP{}
if err := proto.Unmarshal(data, iftppMesage); err != nil {
log.Fatalln("Failed to unmarshal IFTPP:", err)
}
return iftppMesage
}
// take a byte slice payload an turn it into an ICMP message packet
func buildICMP(payload []byte) []byte {
m := icmp.Message{
Type: ipv4.ICMPTypeEcho, Code: 0,
Body: &icmp.RawBody{
Data: payload,
},
}
b, err := m.Marshal(nil)
if err != nil {
log.Fatal(err)
}
return b
}
// disassemble ICMP message packet and extract body
func disasICMP(msg []byte, n int) []byte {
parsed, err := icmp.ParseMessage(icmpConst, msg[:n])
if err != nil {
log.Fatal(err)
}
bod, err := parsed.Body.Marshal(icmpConst)
if err != nil {
log.Fatal(err)
}
return bod
}
// take the required fields for an `IFTPP` message and put them into an ICMP packet
func buildPacket(sid int32, payld []byte, chksum []byte, typ pbuf.IFTPP_Flag) []byte {
buf := buildProto(sid, payld, chksum, typ)
packet := buildICMP(buf)
return packet
}
// disassemble ICMP packet, return an `IFTPP` message so we can use field references
func disasPacket(msg []byte, n int) *pbuf.IFTPP {
packetBody := disasICMP(msg, n)
protoMsg := decodeProto(packetBody)
return protoMsg
}
// read a packet from the listener, extract body from the ICMP, unmarshall the body into an `IFTPP` message
func readFromListener(conn *icmp.PacketConn) (*pbuf.IFTPP, net.Addr) {
msg := make([]byte, readSize)
err := conn.SetReadDeadline(time.Now().Add(10 * time.Second))
if err != nil {
log.Fatal(err)
}
n, peer, err := conn.ReadFrom(msg)
if err != nil {
log.Fatal(err)
}
protoMsg := disasPacket(msg, n)
return protoMsg, peer
}
// marshal provided fields into an `IFTPP` message, put that into an ICMP packet body, write the packet to the conn
func writeToListener(conn *icmp.PacketConn, dst net.Addr, sid int32, payld []byte, chksum []byte, typ pbuf.IFTPP_Flag) {
packet := buildPacket(sid, payld, chksum, typ)
_, err := conn.WriteTo(packet, dst)
if err != nil {
log.Fatal(err)
}
return
}
// xor key with the data
func xorData(data []byte, key []byte) []byte {
var output []byte
for i := 0; i < len(data); i++ {
kIndex := i % len(key)
xdata := data[i] ^ key[kIndex]
output = append(output, xdata)
}
return output
}
func compareChks(myChk []byte, chk []byte) bool {
if len(myChk) != len(chk) {
return false
}
for i, j := range myChk {
if j != chk[i] {
return false
}
}
return true
}
func genKey() ([]byte, []byte) {
cliKey := make([]byte, 16)
rand.Read(cliKey)
var pyld = cliKey
var chk = calcChecksum(pyld)
return pyld, chk
}