-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a much faster and better implementation of a pure go scanner. It uses gopacket to perform arp scanning to find devices on the network along with their mac addresses. It then performs a SYN scan on those found devices to detect when port 22 is open. Caveats are that this requires ops to be run with root privileges and has a dependency on libpcap.
- Loading branch information
1 parent
7fe6305
commit 75871ed
Showing
14 changed files
with
344 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,282 @@ | ||
package discovery | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"net" | ||
"regexp" | ||
"sync" | ||
|
||
"github.com/google/gopacket" | ||
"github.com/google/gopacket/layers" | ||
"github.com/google/gopacket/pcap" | ||
"github.com/projectdiscovery/mapcidr" | ||
"github.com/robgonnella/ops/internal/logger" | ||
"github.com/robgonnella/ops/internal/server" | ||
"github.com/robgonnella/ops/internal/util" | ||
"github.com/rs/zerolog/log" | ||
) | ||
|
||
var cidrSuffix = regexp.MustCompile(`\/\d{2}$`) | ||
|
||
type ARPScanner struct { | ||
ctx context.Context | ||
cancel context.CancelFunc | ||
targets []string | ||
networkInfo *util.NetworkInfo | ||
handle *pcap.Handle | ||
packetSource *gopacket.PacketSource | ||
arpMap map[string]net.HardwareAddr | ||
resultChan chan *DiscoveryResult | ||
mux sync.RWMutex | ||
log logger.Logger | ||
} | ||
|
||
func NewARPScanner( | ||
networkInfo *util.NetworkInfo, | ||
targets []string, | ||
resultChan chan *DiscoveryResult, | ||
) (*ARPScanner, error) { | ||
ipList := []string{} | ||
|
||
for _, t := range targets { | ||
if cidrSuffix.MatchString(t) { | ||
ips, err := mapcidr.IPAddresses(t) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
ipList = append(ipList, ips...) | ||
} else { | ||
ipList = append(ipList, t) | ||
} | ||
} | ||
|
||
// Open up a pcap handle for packet reads/writes. | ||
handle, err := pcap.OpenLive( | ||
networkInfo.Interface.Name, | ||
65536, | ||
true, | ||
pcap.BlockForever, | ||
) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
src := gopacket.NewPacketSource(handle, layers.LayerTypeEthernet) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
|
||
scanner := &ARPScanner{ | ||
ctx: ctx, | ||
cancel: cancel, | ||
targets: ipList, | ||
handle: handle, | ||
packetSource: src, | ||
networkInfo: networkInfo, | ||
arpMap: map[string]net.HardwareAddr{}, | ||
resultChan: resultChan, | ||
mux: sync.RWMutex{}, | ||
log: logger.New(), | ||
} | ||
|
||
go scanner.readPackets() | ||
|
||
return scanner, nil | ||
} | ||
|
||
func (s *ARPScanner) Scan() error { | ||
return s.writeARP() | ||
} | ||
|
||
func (s *ARPScanner) Stop() { | ||
s.cancel() | ||
s.handle.Close() | ||
} | ||
|
||
func (s *ARPScanner) readPackets() { | ||
for { | ||
select { | ||
case <-s.ctx.Done(): | ||
return | ||
case packet := <-s.packetSource.Packets(): | ||
arpLayer := packet.Layer(layers.LayerTypeARP) | ||
|
||
if arpLayer != nil { | ||
s.handleARPLayer(arpLayer.(*layers.ARP)) | ||
continue | ||
} | ||
|
||
s.handleNonARPPacket(packet) | ||
} | ||
} | ||
} | ||
|
||
func (s *ARPScanner) handleARPLayer(arp *layers.ARP) { | ||
if arp.Operation != layers.ARPReply { | ||
// not an arp reply | ||
return | ||
} | ||
|
||
if bytes.Equal([]byte(s.networkInfo.Interface.HardwareAddr), arp.SourceHwAddress) { | ||
// This is a packet I sent. | ||
return | ||
} | ||
|
||
ip := net.IP(arp.SourceProtAddress) | ||
mac := net.HardwareAddr(arp.SourceHwAddress) | ||
|
||
s.mux.Lock() | ||
s.arpMap[ip.String()] = mac | ||
s.mux.Unlock() | ||
|
||
s.writeSyn(ip, mac) | ||
} | ||
|
||
func (s *ARPScanner) handleNonARPPacket(packet gopacket.Packet) { | ||
// Find the packets we care about, and print out logging | ||
// information about them. All others are ignored. | ||
net := packet.NetworkLayer() | ||
|
||
if net == nil { | ||
return | ||
} | ||
|
||
srcIP := net.NetworkFlow().Src().String() | ||
|
||
s.mux.RLock() | ||
mac, ok := s.arpMap[srcIP] | ||
s.mux.RUnlock() | ||
|
||
if !ok { | ||
return | ||
} | ||
|
||
tcpLayer := packet.Layer(layers.LayerTypeTCP) | ||
|
||
if tcpLayer == nil { | ||
return | ||
} | ||
|
||
tcp, ok := tcpLayer.(*layers.TCP) | ||
|
||
if !ok { | ||
// this should never happen. | ||
return | ||
} | ||
|
||
if tcp.DstPort != 54321 { | ||
return | ||
} | ||
|
||
if tcp.SrcPort != 22 { | ||
return | ||
} | ||
|
||
res := &DiscoveryResult{ | ||
ID: mac.String(), | ||
IP: srcIP, | ||
Hostname: "", | ||
OS: "", | ||
Status: server.StatusOnline, | ||
} | ||
|
||
port := Port{ | ||
ID: 22, | ||
Status: PortClosed, | ||
} | ||
|
||
if tcp.SYN && tcp.ACK { | ||
port.Status = PortOpen | ||
} | ||
|
||
res.Ports = []Port{port} | ||
|
||
s.resultChan <- res | ||
} | ||
|
||
func (s *ARPScanner) writeARP() error { | ||
// Set up all the layers' fields we can. | ||
eth := layers.Ethernet{ | ||
SrcMAC: s.networkInfo.Interface.HardwareAddr, | ||
DstMAC: net.HardwareAddr{0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, | ||
EthernetType: layers.EthernetTypeARP, | ||
} | ||
|
||
arp := layers.ARP{ | ||
AddrType: layers.LinkTypeEthernet, | ||
Protocol: layers.EthernetTypeIPv4, | ||
HwAddressSize: 6, | ||
ProtAddressSize: 4, | ||
Operation: layers.ARPRequest, | ||
SourceHwAddress: []byte(s.networkInfo.Interface.HardwareAddr), | ||
SourceProtAddress: []byte(s.networkInfo.UserIP.To4()), | ||
DstHwAddress: []byte{0, 0, 0, 0, 0, 0}, | ||
} | ||
|
||
// Set up buffer and options for serialization. | ||
buf := gopacket.NewSerializeBuffer() | ||
|
||
opts := gopacket.SerializeOptions{ | ||
FixLengths: true, | ||
ComputeChecksums: true, | ||
} | ||
|
||
// Send one packet for every address. | ||
for _, ip := range s.targets { | ||
arp.DstProtAddress = []byte(net.ParseIP(ip).To4()) | ||
|
||
if err := gopacket.SerializeLayers(buf, opts, ð, &arp); err != nil { | ||
return err | ||
} | ||
|
||
if err := s.handle.WritePacketData(buf.Bytes()); err != nil { | ||
log.Error().Err(err).Msg("") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *ARPScanner) writeSyn(ip net.IP, mac net.HardwareAddr) { | ||
// Construct all the network layers we need. | ||
eth := layers.Ethernet{ | ||
SrcMAC: s.networkInfo.Interface.HardwareAddr, | ||
DstMAC: mac, | ||
EthernetType: layers.EthernetTypeIPv4, | ||
} | ||
|
||
ip4 := layers.IPv4{ | ||
SrcIP: s.networkInfo.UserIP.To4(), | ||
DstIP: ip.To4(), | ||
Version: 4, | ||
TTL: 64, | ||
Protocol: layers.IPProtocolTCP, | ||
} | ||
|
||
tcp := layers.TCP{ | ||
SrcPort: 54321, | ||
DstPort: 22, | ||
SYN: true, | ||
} | ||
|
||
tcp.SetNetworkLayerForChecksum(&ip4) | ||
|
||
buf := gopacket.NewSerializeBuffer() | ||
|
||
opts := gopacket.SerializeOptions{ | ||
FixLengths: true, | ||
ComputeChecksums: true, | ||
} | ||
|
||
if err := gopacket.SerializeLayers(buf, opts, ð, &ip4, &tcp); err != nil { | ||
s.log.Error().Err(err).Msg("failed to serialize syn layers") | ||
return | ||
} | ||
|
||
if err := s.handle.WritePacketData(buf.Bytes()); err != nil { | ||
s.log.Error().Err(err).Msg("failed to send syn packet") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.