-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
145 lines (109 loc) · 2.67 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
// port scanning through syn-flood protections
/*
For ensure if a service exist on a port :
if you receive any additional packets from the target after you've established a connection.
1. Capture the packets
2. Check if they are trasmitted with a TCP flag (CWR,ECE,URG, ACK, PSH, RST, SYN, FIN)
*/
package main
import (
"fmt"
"log"
"net"
"os"
"strings"
"time"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
)
var (
snaplen = int32(320)
promise = true
timeout = pcap.BlockForever
filter = "tcp[13] == 0x11 ot tcp[13] == 0x10 or tcp[13] == 0x18"
/*
only looking for
* ACK && FIN : 0x11
* ACK: 0x10
* ACK and PSH 0x18
*/
devFound = false
results = make(map[string]int)
)
func capture(targetDev, target string) {
handle, err := pcap.OpenLive(targetDev, snaplen, promise, timeout)
if err != nil {
log.Panicln(err)
}
defer handle.Close()
if err := handle.SetBPFFilter(filter); err != nil {
log.Panicln(err)
}
source := gopacket.NewPacketSource(handle, handle.LinkType())
fmt.Println("Capturing packets")
for packet := range source.Packets() {
networkLayer := packet.NetworkLayer()
if networkLayer == nil {
continue
}
transportLayer := packet.TransportLayer()
if transportLayer == nil {
continue
}
sourceHost := networkLayer.NetworkFlow().Src().String()
sourcePort := transportLayer.TransportFlow().Src().String()
if sourceHost != target {
continue
}
results[sourcePort] += 1
}
}
func explode (portString string) ([]string , error){
ret := make([]string, 0)
ports := strings.Split(portString, ",")
for _, port := range ports {
port := strings.TrimSpace(port)
ret = append(ret, port)
}
return ret, nil
}
func main() {
if len(os.Args) != 4 {
log.Fatalln("Usage: main.go <capture_targetDev><target_ip><port1,port2, port3>")
}
devices, err := pcap.FindAllDevs()
if err != nil {
log.Panicln(err)
}
targetDevice := os.Args[1]
for _, device := range devices {
if device.Name == targetDevice {
devFound = true
}
}
if !devFound {
log.Panicf("Device named %s' does not exist\n ", targetDevice)
}
ip := os.Args[2]
go capture(targetDevice, ip)
time.Sleep(1 * time.Second)
ports, err := explode(os.Args[3])
if err != nil {
log.Panicln(err)
}
for _, port := range ports {
target := fmt.Sprintf("%s %s", ip, port)
fmt.Println("Trying ", target)
e, err := net.DialTimeout("tcp", target, 1000*time.Millisecond)
if err != nil {
continue
}
e.Close()
}
time.Sleep(2 * time.Second)
for port, probability := range results {
if probability >= 1 {
fmt.Println("port %s open (probability: %d)\n", port, probability)
}
}
}