-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
354 lines (319 loc) · 9.94 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package main
import (
"bytes"
"encoding/binary"
"flag"
"fmt"
"log"
"math/rand"
"net"
"strconv"
"time"
"github.com/krolaw/dhcp4"
"github.com/songgao/packets/ethernet"
"github.com/songgao/water"
"golang.org/x/net/ipv4"
)
var (
remoteIP = flag.String("remote", "", "Destination Server:Destination Port")
localPort = flag.Int("port", 1900, "Local Port")
mtu = 1500
)
func main() {
//remoteIPP := "10.0.0.1"
//remoteIP := &remoteIPP
channel := make(chan int)
flag.Parse()
if "" == *remoteIP {
flag.Usage()
log.Fatalln("No Destination IP found")
}
ifce, err := water.New(water.Config{
DeviceType: water.TAP,
PlatformSpecificParams: water.PlatformSpecificParams{
ComponentID: "tap0901",
Network: "192.168.1.10/24",
},
})
if err != nil {
log.Fatal(err)
}
log.Println("V3 Interface allocated: ", ifce.Name())
log.Println("MTU: ", mtu)
// DHCP
rand.Seed(time.Now().UnixNano())
// Generate a random number between 1 and 255
//log.Println("It maybe laggy if you keep getting a new IP, use ipconfig /renew to renew ur ip")
/*
//C
ipEnding := rand.Intn(255) + 1
assignedIP := net.ParseIP("192.168.1." + strconv.Itoa(ipEnding))
subnetMask := net.ParseIP("255.255.255.0")
serverIP := net.ParseIP("192.168.1.0") // DHCP Server IP, must not duplicate
*/
//B
ipEnding := rand.Intn(255) + 1
ipEnding2 := rand.Intn(255) + 1
assignedIP := net.ParseIP("172.19." + strconv.Itoa(ipEnding2) + "." + strconv.Itoa(ipEnding))
subnetMask := net.ParseIP("255.240.0.0")
serverIP := net.ParseIP("172.17.0.0") // DHCP Server IP, must not duplicate
/*
//A
ipEnding := rand.Intn(255) + 1
ipEnding2 := rand.Intn(255) + 1
ipEnding3 := rand.Intn(255) + 1
assignedIP := net.ParseIP("10." + strconv.Itoa(ipEnding3) + "." + strconv.Itoa(ipEnding2) + "." + strconv.Itoa(ipEnding))
subnetMask := net.ParseIP("255.0.0.0")
serverIP := net.ParseIP("10.0.0.0") // DHCP Server IP, must not duplicate
*/
log.Println("Interface IP address: ", assignedIP)
assignIP(ifce, assignedIP, subnetMask, serverIP)
log.Println("Prep Stage completed")
//ip := net.ParseIP("192.168.1.1")
//fmt.Println(ip.String(), []byte(ip)[12:])
// Create remote connection
remoteAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s", *remoteIP))
if nil != err {
log.Fatalln("Unable to resolve remote addr:", err)
}
// listen to local socket
lstnAddr, err := net.ResolveUDPAddr("udp", fmt.Sprintf(":%v", *localPort))
if nil != err {
log.Fatalln("Unable to get UDP socket:", err)
}
lstnConn, err := net.ListenUDP("udp", lstnAddr)
if nil != err {
log.Fatalln("Unable to listen on UDP socket:", err)
}
defer lstnConn.Close()
go send(lstnConn, ifce, remoteAddr)
go receive(lstnConn, ifce)
//to stuck the thread
<-channel
}
func send(lstnConn *net.UDPConn, ifce *water.Interface, remoteAddr *net.UDPAddr) {
var frame ethernet.Frame
count := 0
for {
//log.Println(len(frame))
frame.Resize(1500)
_, err := ifce.Read([]byte(frame))
if err != nil {
log.Fatal(err)
}
lstnConn.WriteToUDP(frame, remoteAddr)
count++
if count%1000 == 0 {
log.Println("Sent", count, " packets")
}
}
}
func receive(lstnConn *net.UDPConn, ifce *water.Interface) {
buf := make([]byte, 1514)
count := 0
for {
n, _, err := lstnConn.ReadFromUDP(buf)
ifce.Write(buf[:n])
if err != nil || n == 0 {
//fmt.Println("Error: ", err)
continue
}
count++
if count%1000 == 0 {
log.Println("Received", count, " packets")
}
}
}
const (
UDP int = 17
)
func assignIP(ifce *water.Interface, assignedIP net.IP, subnetMask net.IP, serverIP net.IP) {
log.Println("Assigning IP", assignedIP.String())
log.Println("Subnet Mask", subnetMask.String())
log.Println("DHCP Server Address", serverIP.String())
if !assignedIP.IsPrivate() {
log.Println("WARNING: Assigning IP that maybe is not in private")
}
//frame := make([]byte, 1508)
//a := dhcp4.NewPacket(dhcp4.BootRequest)
//fmt.Println(a.)
assignedIPByte := []byte(assignedIP)[12:]
var frame ethernet.Frame
for {
frame.Resize(1500)
n, err := ifce.Read([]byte(frame))
if err != nil {
log.Fatal(err)
}
frame = frame[:n]
if frame.Ethertype() == ethernet.IPv4 {
header, _ := ipv4.ParseHeader(frame.Payload())
if header.Dst.Equal(net.ParseIP("255.255.255.255")) && header.Src.Equal(net.ParseIP("0.0.0.0")) && header.Protocol == UDP {
udpHeader := frame.Payload()[34-14 : 42-12]
srcPort := binary.BigEndian.Uint16(udpHeader[0:2])
dstPort := binary.BigEndian.Uint16(udpHeader[2:4])
// DHCP Packet
if srcPort == 68 && dstPort == 67 {
dhcpPacket := frame.Payload()[42-14:]
req := dhcp4.Packet(dhcpPacket)
options := req.ParseOptions()
//fmt.Println(options)
if t := options[dhcp4.OptionDHCPMessageType]; len(t) != 1 {
// not a DHCP packet
continue
} else {
// ok this is a DHCP packet, parse it
reqType := dhcp4.MessageType(t[0])
// parse the type
//fmt.Println(reqType)
if reqType == dhcp4.Discover {
ifce.Write(genDHCP(req, dhcp4.Offer, assignedIP, subnetMask, serverIP))
} else if reqType == dhcp4.Request {
// in case that Request IP is different from what we expect
// decline it
nak := false
// loop thru options
for i, opt := range options {
// if it is a requested ip field
if i == dhcp4.OptionRequestedIPAddress {
// check if requested != our assigned
if !bytes.Equal(opt, assignedIPByte) {
// decline and break
ifce.Write(genDHCP(req, dhcp4.NAK, net.IP{opt[0], opt[1], opt[2], opt[3]}, subnetMask, serverIP))
nak = true
break
}
}
}
// request packet LGTM
if !nak {
// gen a ACK request and tell client success
ifce.Write(genDHCP(req, dhcp4.ACK, assignedIP, subnetMask, serverIP))
log.Println("DHCP Configuration Success")
break
}
}
}
}
}
}
}
}
func genDHCP(req dhcp4.Packet, response dhcp4.MessageType, assignedIP net.IP, subnetMask net.IP, serverIP net.IP) []byte {
//assignedIPByte := []byte(assignedIP)[12:]
subnetMaskByte := []byte(subnetMask)[12:]
serverIPByte := []byte(serverIP)[12:]
//fmt.Println(subnetMaskByte, serverIPByte)
// DHCP Packet
option := []dhcp4.Option{{
Code: dhcp4.OptionSubnetMask,
Value: subnetMaskByte,
},
}
//fmt.Println(req.Secs())
// build a DHCP packet
//42947295 = As Good as forever, no expiry
reply := dhcp4.ReplyPacket(req, response, serverIPByte, assignedIP, 4294967295*time.Second, option)
reply[8] = req.Secs()[0]
reply[9] = req.Secs()[1]
// reply
//reply = reply[:len(reply)-10]
// Calculate checksum
udp := udphdr{
src: uint16(67),
dst: uint16(68),
ulen: uint16(8 + len(reply)),
}
udp.udpchecksum([4]byte(serverIPByte), [4]byte{255, 255, 255, 255}, uint8(UDP), reply)
//fmt.Println(udp.csum)
// UDP Header
udpHeaderReply := []byte{}
b := []byte{0x00, 0x00}
binary.BigEndian.PutUint16(b, 67) // Src
udpHeaderReply = append(udpHeaderReply[:], b[:]...)
binary.BigEndian.PutUint16(b, 68) // Dst
udpHeaderReply = append(udpHeaderReply[:], b[:]...)
binary.BigEndian.PutUint16(b, uint16(8+len(reply))) // DHCP Packet + 8 Byte UDP Header length
udpHeaderReply = append(udpHeaderReply[:], b[:]...)
binary.BigEndian.PutUint16(b, udp.csum) // Checksum
udpHeaderReply = append(udpHeaderReply[:], b[:]...)
// IPv4 Header
iph := &ipv4.Header{
Version: ipv4.Version,
Len: ipv4.HeaderLen,
TOS: 0x00, // DSCP CS6
ID: 0x00,
TotalLen: ipv4.HeaderLen + len(reply) + len(udpHeaderReply),
TTL: 128,
Protocol: UDP,
Src: serverIP,
Dst: net.ParseIP("255.255.255.255"),
}
// Claculating checksum
ipHeader, _ := iph.Marshal()
iph.Checksum = int(checksum(ipHeader))
ipHeader, _ = iph.Marshal()
//fmt.Println(iph)
// Ethernet frame
finalPacket := []byte{}
finalPacket = append([]byte{0x00, 0xfa, 0x80, 0xce, 0x96, 0x3e}, finalPacket...) // Src MAC Address, MUST NOT DUPLICATE WITH NIC ADDRESS
finalPacket = append([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, finalPacket...) // Dst BROADCAST ADDRESS
finalPacket = append(finalPacket, []byte{0x08, 0x00}...) // ETH Header
finalPacket = append(finalPacket, ipHeader...) // IP Header
finalPacket = append(finalPacket, udpHeaderReply...) // UDP Header
finalPacket = append(finalPacket, reply...) // DHCP Payload
//ifce.Write(finalPacket)
return finalPacket
}
func checksum(buf []byte) uint16 {
sum := uint32(0)
for ; len(buf) >= 2; buf = buf[2:] {
sum += uint32(buf[0])<<8 | uint32(buf[1])
}
if len(buf) > 0 {
sum += uint32(buf[0]) << 8
}
for sum > 0xffff {
sum = (sum >> 16) + (sum & 0xffff)
}
csum := ^uint16(sum)
/*
* From RFC 768:
* If the computed checksum is zero, it is transmitted as all ones (the
* equivalent in one's complement arithmetic). An all zero transmitted
* checksum value means that the transmitter generated no checksum (for
* debugging or for higher level protocols that don't care).
*/
if csum == 0 {
csum = 0xffff
}
return csum
}
type pseudohdr struct {
ipsrc [4]byte
ipdst [4]byte
zero uint8
ipproto uint8
plen uint16
}
type udphdr struct {
src uint16
dst uint16
ulen uint16
csum uint16
}
func (u *udphdr) udpchecksum(src [4]byte, dst [4]byte, proto uint8, payload []byte) {
u.csum = 0
phdr := pseudohdr{
ipsrc: src,
ipdst: dst,
zero: 0,
ipproto: proto,
plen: u.ulen,
}
var b bytes.Buffer
binary.Write(&b, binary.BigEndian, &phdr)
binary.Write(&b, binary.BigEndian, u)
binary.Write(&b, binary.BigEndian, &payload)
u.csum = checksum(b.Bytes())
}