mirror of
https://github.com/bettercap/bettercap
synced 2025-08-19 13:09:49 -07:00
misc: small fix or general refactoring i did not bother commenting
This commit is contained in:
parent
b8d9179def
commit
30d9415d8c
5 changed files with 25 additions and 24 deletions
|
@ -2,15 +2,17 @@ package syn_scan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/bettercap/bettercap/network"
|
"time"
|
||||||
|
|
||||||
"github.com/evilsocket/islazy/async"
|
"github.com/evilsocket/islazy/async"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const bannerGrabTimeout = time.Duration(5) * time.Second
|
||||||
|
|
||||||
type bannerGrabberFn func(mod *SynScanner, ip string, port int) string
|
type bannerGrabberFn func(mod *SynScanner, ip string, port int) string
|
||||||
|
|
||||||
type grabberJob struct {
|
type grabberJob struct {
|
||||||
Host *network.Endpoint
|
IP string
|
||||||
Port *OpenPort
|
Port *OpenPort
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,7 +22,7 @@ func (mod *SynScanner) bannerGrabber(arg async.Job) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ip := job.Host.IpAddress
|
ip := job.IP
|
||||||
port := job.Port.Port
|
port := job.Port.Port
|
||||||
sport := fmt.Sprintf("%d", port)
|
sport := fmt.Sprintf("%d", port)
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,6 @@ import (
|
||||||
"golang.org/x/net/html"
|
"golang.org/x/net/html"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func isTitleElement(n *html.Node) bool {
|
func isTitleElement(n *html.Node) bool {
|
||||||
|
@ -30,9 +29,8 @@ func searchForTitle(n *html.Node) string {
|
||||||
|
|
||||||
func httpGrabber(mod *SynScanner, ip string, port int) string {
|
func httpGrabber(mod *SynScanner, ip string, port int) string {
|
||||||
schema := "http"
|
schema := "http"
|
||||||
timeout := time.Duration(10 * time.Second)
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: timeout,
|
Timeout: bannerGrabTimeout,
|
||||||
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
@ -42,7 +40,7 @@ func httpGrabber(mod *SynScanner, ip string, port int) string {
|
||||||
if strings.Contains(sport, "443") {
|
if strings.Contains(sport, "443") {
|
||||||
schema = "https"
|
schema = "https"
|
||||||
client = &http.Client{
|
client = &http.Client{
|
||||||
Timeout: timeout,
|
Timeout: bannerGrabTimeout,
|
||||||
Transport: &http.Transport{
|
Transport: &http.Transport{
|
||||||
TLSClientConfig: &tls.Config{
|
TLSClientConfig: &tls.Config{
|
||||||
InsecureSkipVerify: true,
|
InsecureSkipVerify: true,
|
||||||
|
|
|
@ -50,7 +50,7 @@ func NewSynScanner(s *session.Session) *SynScanner {
|
||||||
}
|
}
|
||||||
|
|
||||||
mod.scanQueue = async.NewQueue(0, mod.scanWorker)
|
mod.scanQueue = async.NewQueue(0, mod.scanWorker)
|
||||||
mod.bannerQueue = async.NewQueue(4, mod.bannerGrabber)
|
mod.bannerQueue = async.NewQueue(0, mod.bannerGrabber)
|
||||||
|
|
||||||
mod.State.Store("scanning", &mod.addresses)
|
mod.State.Store("scanning", &mod.addresses)
|
||||||
mod.State.Store("progress", 0.0)
|
mod.State.Store("progress", 0.0)
|
||||||
|
@ -120,6 +120,7 @@ func (mod *SynScanner) Configure() (err error) {
|
||||||
} else if err = mod.handle.SetBPFFilter(fmt.Sprintf("tcp dst port %d", synSourcePort)); err != nil {
|
} else if err = mod.handle.SetBPFFilter(fmt.Sprintf("tcp dst port %d", synSourcePort)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
mod.packets = gopacket.NewPacketSource(mod.handle, mod.handle.LinkType()).Packets()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -155,10 +156,6 @@ func (mod *SynScanner) Stop() error {
|
||||||
return mod.SetRunning(false, func() {
|
return mod.SetRunning(false, func() {
|
||||||
mod.packets <- nil
|
mod.packets <- nil
|
||||||
mod.waitGroup.Wait()
|
mod.waitGroup.Wait()
|
||||||
mod.showProgress()
|
|
||||||
mod.addresses = []net.IP{}
|
|
||||||
mod.State.Store("progress", 0.0)
|
|
||||||
mod.State.Store("scanning", &mod.addresses)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -203,6 +200,7 @@ func (mod *SynScanner) synScan() error {
|
||||||
defer mod.waitGroup.Done()
|
defer mod.waitGroup.Done()
|
||||||
|
|
||||||
defer mod.SetRunning(false, func() {
|
defer mod.SetRunning(false, func() {
|
||||||
|
mod.showProgress()
|
||||||
mod.addresses = []net.IP{}
|
mod.addresses = []net.IP{}
|
||||||
mod.State.Store("progress", 0.0)
|
mod.State.Store("progress", 0.0)
|
||||||
mod.State.Store("scanning", &mod.addresses)
|
mod.State.Store("scanning", &mod.addresses)
|
||||||
|
@ -233,8 +231,6 @@ func (mod *SynScanner) synScan() error {
|
||||||
mod.waitGroup.Add(1)
|
mod.waitGroup.Add(1)
|
||||||
defer mod.waitGroup.Done()
|
defer mod.waitGroup.Done()
|
||||||
|
|
||||||
src := gopacket.NewPacketSource(mod.handle, mod.handle.LinkType())
|
|
||||||
mod.packets = src.Packets()
|
|
||||||
for packet := range mod.packets {
|
for packet := range mod.packets {
|
||||||
if !mod.Running() {
|
if !mod.Running() {
|
||||||
break
|
break
|
||||||
|
|
|
@ -46,6 +46,12 @@ func (mod *SynScanner) onPacket(pkt gopacket.Packet) {
|
||||||
from := ip.SrcIP.String()
|
from := ip.SrcIP.String()
|
||||||
port := int(tcp.SrcPort)
|
port := int(tcp.SrcPort)
|
||||||
|
|
||||||
|
openPort := &OpenPort{
|
||||||
|
Proto: "tcp",
|
||||||
|
Port: port,
|
||||||
|
Service: network.GetServiceByPort(port, "tcp"),
|
||||||
|
}
|
||||||
|
|
||||||
var host *network.Endpoint
|
var host *network.Endpoint
|
||||||
if ip.SrcIP.Equal(mod.Session.Interface.IP) {
|
if ip.SrcIP.Equal(mod.Session.Interface.IP) {
|
||||||
host = mod.Session.Interface
|
host = mod.Session.Interface
|
||||||
|
@ -58,20 +64,13 @@ func (mod *SynScanner) onPacket(pkt gopacket.Packet) {
|
||||||
if host != nil {
|
if host != nil {
|
||||||
ports := host.Meta.GetOr("ports", map[int]*OpenPort{}).(map[int]*OpenPort)
|
ports := host.Meta.GetOr("ports", map[int]*OpenPort{}).(map[int]*OpenPort)
|
||||||
if _, found := ports[port]; !found {
|
if _, found := ports[port]; !found {
|
||||||
openPort := &OpenPort{
|
|
||||||
Proto: "tcp",
|
|
||||||
Port: port,
|
|
||||||
Service: network.GetServiceByPort(port, "tcp"),
|
|
||||||
}
|
|
||||||
|
|
||||||
ports[port] = openPort
|
ports[port] = openPort
|
||||||
|
|
||||||
mod.bannerQueue.Add(async.Job(grabberJob{host, openPort}))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
host.Meta.Set("ports", ports)
|
host.Meta.Set("ports", ports)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mod.bannerQueue.Add(async.Job(grabberJob{from, openPort}))
|
||||||
|
|
||||||
NewSynScanEvent(from, host, port).Push()
|
NewSynScanEvent(from, host, port).Push()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,10 +19,16 @@ func cleanBanner(banner string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func tcpGrabber(mod *SynScanner, ip string, port int) string {
|
func tcpGrabber(mod *SynScanner, ip string, port int) string {
|
||||||
if conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", ip, port)); err == nil {
|
dialer := net.Dialer{
|
||||||
|
Timeout: bannerGrabTimeout,
|
||||||
|
}
|
||||||
|
|
||||||
|
if conn, err := dialer.Dial("tcp", fmt.Sprintf("%s:%d", ip, port)); err == nil {
|
||||||
defer conn.Close()
|
defer conn.Close()
|
||||||
msg, _ := bufio.NewReader(conn).ReadString('\n')
|
msg, _ := bufio.NewReader(conn).ReadString('\n')
|
||||||
return cleanBanner(strings.Trim(msg, "\r\n\t "))
|
return cleanBanner(strings.Trim(msg, "\r\n\t "))
|
||||||
|
} else {
|
||||||
|
mod.Debug("%s:%d : %v", ip, port, err)
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue