-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
114 lines (93 loc) · 2.17 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
package main
import (
"log"
"net"
"time"
)
func main() {
ip := getIP().String()
log.Printf("external ip: %v", ip)
go probe(ip)
log.Printf("listen on %v:9000", "0.0.0.0")
listener, err := net.Listen("tcp", "0.0.0.0:9000")
if err != nil {
panic(err)
}
for {
conn, err := listener.Accept()
if err != nil {
panic(err)
}
go func(conn net.Conn) {
time.Sleep(10 * time.Second)
conn.Close()
}(conn)
}
}
func probe(ip string) {
log.Printf("probing %v", ip)
ipAddr, err := net.ResolveIPAddr("ip4:tcp", ip)
if err != nil {
panic(err)
}
conn, err := net.ListenIP("ip4:tcp", ipAddr)
if err != nil {
panic(err)
}
pending := make(map[string]uint32)
var buffer [4096]byte
for {
n, addr, err := conn.ReadFrom(buffer[:])
if err != nil {
log.Printf("conn.ReadFrom() error: %v", err)
continue
}
pkt := &tcpPacket{}
data, err := pkt.decode(buffer[:n])
if err != nil {
log.Printf("tcp packet parse error: %v", err)
continue
}
if pkt.DestPort != 9000 {
continue
}
log.Printf("tcp packet: %+v, flag: %v, data: %v, addr: %v", pkt, pkt.FlagString(), data, addr)
if pkt.Flags&SYN != 0 {
pending[addr.String()] = pkt.Seq + 1
continue
}
if pkt.Flags&RST != 0 {
panic("RST received")
}
if pkt.Flags&ACK != 0 {
if seq, ok := pending[addr.String()]; ok {
log.Println("connection established")
delete(pending, addr.String())
badPkt := &tcpPacket{
SrcPort: pkt.DestPort,
DestPort: pkt.SrcPort,
Ack: seq,
Seq: pkt.Ack - 100000, // Bad: seq out-of-window
Flags: (5 << 12) | PSH | ACK, // Offset and Flags oooo000F FFFFFFFF (o:offset, F:flags)
WindowSize: pkt.WindowSize,
}
data := []byte("boom!!!")
remoteIP := net.ParseIP(addr.String())
localIP := net.ParseIP(conn.LocalAddr().String())
_, err := conn.WriteTo(badPkt.encode(localIP, remoteIP, data[:]), addr)
if err != nil {
log.Printf("conn.WriteTo() error: %v", err)
}
}
}
}
}
func getIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:53")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}