Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
GobiasSomeCoffeeCo committed Aug 12, 2023
1 parent 489f7d2 commit 4bd2454
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 73 deletions.
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ go 1.20

require github.com/google/gopacket v1.1.19

require golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
require (
golang.org/x/sys v0.0.0-20190412213103-97732733099d // indirect
)
17 changes: 17 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
github.com/google/gopacket v1.1.19/go.mod h1:iJ8V8n6KS+z2U1A8pUwu8bW5SyEMkXJB8Yo/Vo+TKTo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20200302205851-738671d3881b/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY=
Expand All @@ -14,3 +27,7 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7w
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
1 change: 1 addition & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ func ParseCLI() *silentscan.ScanOptions {
flag.BoolVar(&options.UseACK, "sA", false, "Set ACK flag for TCP")
flag.BoolVar(&options.UseURG, "sU", false, "Set URG flag for TCP")
flag.BoolVar(&options.UseXMas, "sX", false, "Set 'XMas Flag' (URG PSH FIN) for TCP")
flag.BoolVar(&options.UseXMas, "b", false, "Attempt to grab the banner from the service")
flag.StringVar(&options.InterfaceName, "i", "", "Network interface to use. (e.g., sudo ./gosilent -t 192.168.1.1 -i eno2) If empty, will fallback to system defaults.")
//flag.StringVar(&options.Ports, "ports", "22,80,135,139,400-10000", "Ports to scan (e.g., 22,80,139,400-500). Use commas and hyphens for ranges.")
flag.StringVar(&options.Target, "t", "", "The target IP you'd like to scan (e.g., sudo ./gosilent -t 192.168.1.1)")
Expand Down
10 changes: 0 additions & 10 deletions pkg/helpers/pazzaz.go

This file was deleted.

3 changes: 0 additions & 3 deletions pkg/helpers/version.go

This file was deleted.

9 changes: 3 additions & 6 deletions pkg/syn/syn_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
)

func handleFlags(opts *ScanOptions, tcp *layers.TCP) {
// Since SYN is always set to true, if users start utilizing flags we'll
// turn it off and check at the end if they still want it turned on
// Since SYN is by default set to true, if users start utilizing flags we'll
// assume they dont want it automatically turned on. We'll set it to off
// and check at the end if they still want it on
if opts.UseACK {
tcp.ACK = true
tcp.SYN = false
Expand All @@ -19,10 +20,6 @@ func handleFlags(opts *ScanOptions, tcp *layers.TCP) {
tcp.RST = true
tcp.SYN = false
}
if opts.UseNS {
tcp.NS = true
tcp.SYN = false
}
if opts.UsePSH {
tcp.PSH = true
tcp.SYN = false
Expand Down
110 changes: 57 additions & 53 deletions pkg/syn/syn_scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"os/user"
"time"

"github.com/GobiasSomeCoffeeCo/gosilent/pkg/helpers"
"github.com/GobiasSomeCoffeeCo/gosilent/pkg/display"

Check failure on line 12 in pkg/syn/syn_scan.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/GobiasSomeCoffeeCo/gosilent/pkg/display; to add it:

"github.com/google/gopacket"
"github.com/google/gopacket/examples/util"
Expand All @@ -32,9 +32,15 @@ type ScanOptions struct {
InterfaceName string
Ports string
Target string
Banner bool
PortRanges []int
}

type ScanResults struct {
OpenPorts []int
Banner []string
}

// scanner handles scanning a single IP address.
type scanner struct {
// iface is the interface to send packets on.
Expand Down Expand Up @@ -66,7 +72,6 @@ func newScanner(ip net.IP, router routing.Router, opts *ScanOptions) (*scanner,
if err != nil {
return nil, err
}

s.gw, s.src, s.iface = gw, src, iface

// Open the handle for reading/writing.
Expand All @@ -78,23 +83,17 @@ func newScanner(ip net.IP, router routing.Router, opts *ScanOptions) (*scanner,
if opts.InterfaceName != "" {
s.iface, err = net.InterfaceByName(opts.InterfaceName)
if err != nil {
fmt.Println("Cannot get interface by name")
fmt.Printf("%s Cannot get interface by name. error: %v", display.BAD, err)
}
s.getHandle(opts.InterfaceName)

log.Printf("scanning ip %v with interface %v, gateway %v, src %v", ip, iface.Name, gw, src)
} else {

s.getHandle(iface.Name)
log.Printf("scanning ip %v with interface %v, gateway %v, src %v", ip, iface.Name, gw, src)

}

return s, nil
}

// close cleans up the handle.

func (s *scanner) getHandle(ifaceName string) (*pcap.Handle, error) {
if handle, ok := s.handleMap[ifaceName]; ok {
return handle, nil
Expand All @@ -112,47 +111,19 @@ func (s *scanner) getHandle(ifaceName string) (*pcap.Handle, error) {
func (s *scanner) close() {
handle, ok := s.handleMap[s.iface.Name]
if !ok {
log.Printf("%s Unable to get PCAP handle in close()", helpers.BAD)
log.Printf("%s Unable to get PCAP handle in close()", display.BAD)
}
handle.Close()
}

// scan scans the dst IP address of this scanner.
func (s *scanner) scan(opts *ScanOptions) error {
// First off, get the MAC address we should be sending packets to.
hwaddrChan := make(chan net.HardwareAddr)
defer close(hwaddrChan)
eth, ip4, tcp := s.setUpLayers()

eth := layers.Ethernet{
SrcMAC: s.iface.HardwareAddr,
DstMAC: net.HardwareAddr{0xf0, 0x81, 0x75, 0x03, 0x50, 0x92},
EthernetType: layers.EthernetTypeIPv4,
}
ip4 := layers.IPv4{
SrcIP: s.src,
DstIP: s.dst,
Id: 23757,
Version: 4,
TTL: 42,
Protocol: layers.IPProtocolTCP,
}
tcp := layers.TCP{
SrcPort: 54321,
DstPort: 0, // will be incremented during the scan
SYN: true,
Window: 1024,
Seq: 35476,
Options: []layers.TCPOption{
{
OptionType: layers.TCPOptionKindMSS,
OptionLength: 4,
OptionData: []byte{0x05, 0xb4}},
},
}
tcp.SetNetworkLayerForChecksum(&ip4)
handle, ok := s.handleMap[s.iface.Name]
if !ok {
log.Println("Unable to get PCAP handle in scan()")
log.Printf("%s Unable to get PCAP handle in scan()", display.BAD)
}
// Create channels for communication between goroutines
done := make(chan bool)
Expand All @@ -172,20 +143,24 @@ func (s *scanner) scan(opts *ScanOptions) error {
for tcp.DstPort < 65535 {
start = time.Now()

// Give time for the read
time.Sleep(time.Microsecond * 1)

port := randomizer()
id := randomizer()

ip4.Id = uint16(id)

tcp.DstPort++
tcp.SrcPort = layers.TCPPort(port)
if err := s.send(&eth, &ip4, &tcp); err != nil {
log.Printf("%s error sending to port %v: %v", helpers.BAD, tcp.DstPort, err)
log.Printf("%s error sending to port %v: %v", display.BAD, tcp.DstPort, err)
}
}

// Timeout if no packets sent for 5 seconds
if time.Since(start) > time.Second*5 {
log.Printf("%s timed out for %v, assuming we've seen all we can", helpers.INFO, s.dst)
log.Printf("%s timed out for %v, assuming we've seen all we can", display.INFO, s.dst)
}
}()

Expand All @@ -196,7 +171,7 @@ func (s *scanner) scan(opts *ScanOptions) error {
if err == pcap.NextErrorTimeoutExpired {
continue
} else if err != nil {
log.Printf("%s error reading packet: %v", helpers.BAD, err)
log.Printf("%s error reading packet: %v", display.BAD, err)
continue
}

Expand All @@ -217,7 +192,7 @@ func (s *scanner) scan(opts *ScanOptions) error {
if net := packet.NetworkLayer(); net == nil {
//log.Printf("packet has no network layer") //
} else if net.NetworkFlow() != ipFlow {
//log.Printf("packet does not match our ip src/dst") //
log.Printf("packet does not match our ip src/dst") //
} else if tcpLayer := packet.Layer(layers.LayerTypeTCP); tcpLayer == nil {
//log.Printf("packet has not tcp layer") //
} else if tcp, ok := tcpLayer.(*layers.TCP); !ok {
Expand All @@ -227,9 +202,9 @@ func (s *scanner) scan(opts *ScanOptions) error {
//} else if tcp.DstPort != 54321 {
//log.Printf("dst port %v does not match", tcp.DstPort) //
} else if tcp.RST {
//log.Printf(" port %v closed", tcp.SrcPort)
//fmt.Printf("%s closed %v\n", display.BAD, tcp.SrcPort)
} else if tcp.SYN && tcp.ACK {
fmt.Printf("%s open %v\n", helpers.GOOD, tcp.SrcPort)
fmt.Printf("%s open %v\n", display.GOOD, tcp.SrcPort)
} else {
log.Printf("ignoring useless packet") //
}
Expand All @@ -238,36 +213,65 @@ func (s *scanner) scan(opts *ScanOptions) error {
return nil // Exit if done sending packets
}
}

}

// send sends the given layers as a single packet on the network.
func (s *scanner) send(l ...gopacket.SerializableLayer) error {
if err := gopacket.SerializeLayers(s.buf, s.opts, l...); err != nil {
return err
}

handle, ok := s.handleMap[s.iface.Name]
if !ok {
log.Println("Unable to access PCAP handle in send()")
log.Printf("%v Unable to access PCAP handle in send()", display.BAD)
}
return handle.WritePacketData(s.buf.Bytes())
}

func randomizer() int {
rand.Seed(time.Now().UnixNano())
//rand.Seed(time.Now().UnixNano())
rand.New(rand.NewSource(time.Now().UnixNano()))
min := 49152
max := 65535
port := rand.Intn(max-min+1) + min

return port
}

func (s *scanner) setUpLayers() (layers.Ethernet, layers.IPv4, layers.TCP) {
eth := layers.Ethernet{
SrcMAC: s.iface.HardwareAddr,
DstMAC: net.HardwareAddr{0xf0, 0x81, 0x75, 0x03, 0x50, 0x92},
EthernetType: layers.EthernetTypeIPv4,
}
ip4 := layers.IPv4{
SrcIP: s.src,
DstIP: s.dst,
Id: 23757,
Version: 4,
TTL: 42,
Protocol: layers.IPProtocolTCP,
}
tcp := layers.TCP{
SrcPort: 54321,
DstPort: 0, // will be incremented during the scan
SYN: true,
Window: 1024,
Seq: 35476,
Options: []layers.TCPOption{
{
OptionType: layers.TCPOptionKindMSS,
OptionLength: 4,
OptionData: []byte{0x05, 0xb4}},
},
}
return eth, ip4, tcp
}

func SynScan(opts *ScanOptions) {
defer util.Run()()
currentUser, err := user.Current()
if err != nil {
fmt.Println("Failed to get the current user:", err)
fmt.Printf("%v Failed to get the current user: %v\n", display.BAD, err)
os.Exit(1)
}

Expand All @@ -290,11 +294,11 @@ func SynScan(opts *ScanOptions) {

s, err := newScanner(ip, router, opts)
if err != nil {
log.Printf("unable to create scanner for %v: %v", ip, err)
log.Printf("%v unable to create scanner for %v: %v", display.BAD, ip, err)
return
}
if err := s.scan(opts); err != nil {
log.Printf("unable to scan %v: %v", ip, err)
log.Printf("%v unable to scan %v: %v", display.BAD, ip, err)
}
s.close()
}

0 comments on commit 4bd2454

Please sign in to comment.