From e4682168dfb1e0569166e0d580ecfc6c56dc5fe7 Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Thu, 5 Mar 2020 08:34:45 +1000 Subject: [PATCH 01/10] add dns.spoof.ttl env variable --- modules/dns_spoof/dns_spoof.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/modules/dns_spoof/dns_spoof.go b/modules/dns_spoof/dns_spoof.go index 0db15410..8a74344d 100644 --- a/modules/dns_spoof/dns_spoof.go +++ b/modules/dns_spoof/dns_spoof.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "net" + "strconv" "sync" "github.com/bettercap/bettercap/packets" @@ -20,6 +21,7 @@ type DNSSpoofer struct { session.SessionModule Handle *pcap.Handle Hosts Hosts + TTL uint32 All bool waitGroup *sync.WaitGroup pktSourceChan chan gopacket.Packet @@ -31,6 +33,7 @@ func NewDNSSpoofer(s *session.Session) *DNSSpoofer { Handle: nil, All: false, Hosts: Hosts{}, + TTL: 1024, waitGroup: &sync.WaitGroup{}, } @@ -55,6 +58,11 @@ func NewDNSSpoofer(s *session.Session) *DNSSpoofer { "false", "If true the module will reply to every DNS request, otherwise it will only reply to the one targeting the local pc.")) + mod.AddParam(session.NewStringParameter("dns.spoof.ttl", + "1024", + "^[0-9]+$", + "TTL of spoofed DNS replies in seconds.")) + mod.AddHandler(session.NewModuleHandler("dns.spoof on", "", "Start the DNS spoofer in the background.", func(args []string) error { @@ -84,6 +92,7 @@ func (mod DNSSpoofer) Author() string { func (mod *DNSSpoofer) Configure() error { var err error + var ttl string var hostsFile string var domains []string var address net.IP @@ -102,6 +111,8 @@ func (mod *DNSSpoofer) Configure() error { return err } else if err, hostsFile = mod.StringParam("dns.spoof.hosts"); err != nil { return err + } else if err, ttl = mod.StringParam("dns.spoof.ttl"); err != nil { + return err } mod.Hosts = Hosts{} @@ -131,6 +142,12 @@ func (mod *DNSSpoofer) Configure() error { mod.Session.Firewall.EnableForwarding(true) } + ttl_, err := strconv.ParseUint(ttl, 10, 32) + if err != nil { + return fmt.Errorf("dns.spoof.ttl value must be an integer") + } + mod.TTL = uint32(ttl_) + return nil } @@ -184,7 +201,7 @@ func (mod *DNSSpoofer) dnsReply(pkt gopacket.Packet, peth *layers.Ethernet, pudp Name: []byte(q.Name), Type: q.Type, Class: q.Class, - TTL: 1024, + TTL: mod.TTL, IP: address, }) } From 03951d9d0125b43c58fd8860a636ac210be54588 Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Thu, 5 Mar 2020 08:35:42 +1000 Subject: [PATCH 02/10] Update dns_spoof.go --- modules/dns_spoof/dns_spoof.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/dns_spoof/dns_spoof.go b/modules/dns_spoof/dns_spoof.go index 8a74344d..5072c6b7 100644 --- a/modules/dns_spoof/dns_spoof.go +++ b/modules/dns_spoof/dns_spoof.go @@ -144,7 +144,7 @@ func (mod *DNSSpoofer) Configure() error { ttl_, err := strconv.ParseUint(ttl, 10, 32) if err != nil { - return fmt.Errorf("dns.spoof.ttl value must be an integer") + return fmt.Errorf("dns.spoof.ttl value must be an integer") } mod.TTL = uint32(ttl_) From 466105a1af2cc14fd1ca615bfc991358b372993a Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Thu, 5 Mar 2020 08:39:07 +1000 Subject: [PATCH 03/10] Update dns_spoof.go --- modules/dns_spoof/dns_spoof.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/dns_spoof/dns_spoof.go b/modules/dns_spoof/dns_spoof.go index 5072c6b7..49ad906d 100644 --- a/modules/dns_spoof/dns_spoof.go +++ b/modules/dns_spoof/dns_spoof.go @@ -142,11 +142,11 @@ func (mod *DNSSpoofer) Configure() error { mod.Session.Firewall.EnableForwarding(true) } - ttl_, err := strconv.ParseUint(ttl, 10, 32) - if err != nil { - return fmt.Errorf("dns.spoof.ttl value must be an integer") + _ttl, _ := strconv.ParseUint(ttl, 10, 32) + if _ttl <= 0 { + return fmt.Errorf("dns.spoof.ttl value must be 1 or higher") } - mod.TTL = uint32(ttl_) + mod.TTL = uint32(_ttl) return nil } From 2f14254c4c73828b8d2e735c1460eb0671ea34d8 Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Thu, 5 Mar 2020 08:43:21 +1000 Subject: [PATCH 04/10] Update dns_spoof.go --- modules/dns_spoof/dns_spoof.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/modules/dns_spoof/dns_spoof.go b/modules/dns_spoof/dns_spoof.go index 49ad906d..86a31ca8 100644 --- a/modules/dns_spoof/dns_spoof.go +++ b/modules/dns_spoof/dns_spoof.go @@ -142,10 +142,7 @@ func (mod *DNSSpoofer) Configure() error { mod.Session.Firewall.EnableForwarding(true) } - _ttl, _ := strconv.ParseUint(ttl, 10, 32) - if _ttl <= 0 { - return fmt.Errorf("dns.spoof.ttl value must be 1 or higher") - } + _ttl, _ := strconv.Atoi(ttl) mod.TTL = uint32(_ttl) return nil From 51dfd868980e11352e16148bccae7043c2b49ec5 Mon Sep 17 00:00:00 2001 From: buffermet <29265684+buffermet@users.noreply.github.com> Date: Thu, 5 Mar 2020 08:44:12 +1000 Subject: [PATCH 05/10] Update dns_spoof.go --- modules/dns_spoof/dns_spoof.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/dns_spoof/dns_spoof.go b/modules/dns_spoof/dns_spoof.go index 86a31ca8..bd865e68 100644 --- a/modules/dns_spoof/dns_spoof.go +++ b/modules/dns_spoof/dns_spoof.go @@ -61,7 +61,7 @@ func NewDNSSpoofer(s *session.Session) *DNSSpoofer { mod.AddParam(session.NewStringParameter("dns.spoof.ttl", "1024", "^[0-9]+$", - "TTL of spoofed DNS replies in seconds.")) + "TTL of spoofed DNS replies.")) mod.AddHandler(session.NewModuleHandler("dns.spoof on", "", "Start the DNS spoofer in the background.", From 1fee1f718deae6ac4e35481633dc8d97ee5ba471 Mon Sep 17 00:00:00 2001 From: Hasibul Hasan Anik Date: Thu, 5 Mar 2020 12:56:04 +0600 Subject: [PATCH 06/10] Remove unnecessary fmt.Sprintf the tui.Bold function already returning a string. There has not need to convert it ot string by using fmt.Sprintf --- modules/events_stream/events_view_http.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/events_stream/events_view_http.go b/modules/events_stream/events_view_http.go index e7e4a9da..6d08c84a 100644 --- a/modules/events_stream/events_view_http.go +++ b/modules/events_stream/events_view_http.go @@ -75,7 +75,7 @@ func (mod *EventsStream) dumpForm(body []byte) string { if err != nil { value = v } - form = append(form, fmt.Sprintf("%s", tui.Bold(tui.Red(value)))) + form = append(form, tui.Bold(tui.Red(value))) } } return "\n" + strings.Join(form, "&") + "\n" @@ -113,7 +113,7 @@ func (mod *EventsStream) dumpJSON(body []byte) string { if err := json.Indent(&buf, body, "", " "); err != nil { pretty = string(body) } else { - pretty = string(buf.Bytes()) + pretty = buf.String() } return "\n" + reJsonKey.ReplaceAllString(pretty, tui.Green(`$1:`)) + "\n" From 050bd28511ec53149652944dfc9e3713023beb58 Mon Sep 17 00:00:00 2001 From: Hasibul Hasan Anik Date: Thu, 5 Mar 2020 12:58:22 +0600 Subject: [PATCH 07/10] Kepp sync.WorkerGroup.Add() outside of goroutine The workergroup should be added before starting the worker. The worker routine itself should not start the worker. It causes race condition. --- modules/syn_scan/syn_scan.go | 2 +- modules/wifi/wifi_assoc.go | 3 +-- modules/wifi/wifi_deauth.go | 2 +- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/modules/syn_scan/syn_scan.go b/modules/syn_scan/syn_scan.go index 0eaa2dde..afa8bfa0 100644 --- a/modules/syn_scan/syn_scan.go +++ b/modules/syn_scan/syn_scan.go @@ -227,8 +227,8 @@ func (mod *SynScanner) synScan() error { mod.State.Store("progress", 0.0) // start the collector + mod.waitGroup.Add(1) go func() { - mod.waitGroup.Add(1) defer mod.waitGroup.Done() for packet := range mod.packets { diff --git a/modules/wifi/wifi_assoc.go b/modules/wifi/wifi_assoc.go index a2fb66c9..43f69a0c 100644 --- a/modules/wifi/wifi_assoc.go +++ b/modules/wifi/wifi_assoc.go @@ -88,9 +88,8 @@ func (mod *WiFiModule) startAssoc(to net.HardwareAddr) error { } return fmt.Errorf("%s is an unknown BSSID or it is in the association skip list.", to.String()) } - + mod.writes.Add(1) go func() { - mod.writes.Add(1) defer mod.writes.Done() // since we need to change the wifi adapter channel for each diff --git a/modules/wifi/wifi_deauth.go b/modules/wifi/wifi_deauth.go index d4f77427..5f79a8ac 100644 --- a/modules/wifi/wifi_deauth.go +++ b/modules/wifi/wifi_deauth.go @@ -113,8 +113,8 @@ func (mod *WiFiModule) startDeauth(to net.HardwareAddr) error { return fmt.Errorf("%s is an unknown BSSID, is in the deauth skip list, or doesn't have detected clients.", to.String()) } + mod.writes.Add(1) go func() { - mod.writes.Add(1) defer mod.writes.Done() // since we need to change the wifi adapter channel for each From 8c3f60641e544ef1e7b08331776c34039cca35d2 Mon Sep 17 00:00:00 2001 From: Hasibul Hasan Anik Date: Thu, 5 Mar 2020 13:01:17 +0600 Subject: [PATCH 08/10] Remove unnecessary and empty if block. The if block itself is empty. Calling recover is enough to recover from panic --- session/events.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/session/events.go b/session/events.go index c62b6c9f..31564f25 100644 --- a/session/events.go +++ b/session/events.go @@ -65,9 +65,7 @@ func (p *EventPool) Listen() <-chan Event { go func() { for i := len(p.events) - 1; i >= 0; i-- { defer func() { - if recover() != nil { - - } + recover() }() l <- p.events[i] } From 3b57b0cb386fbff257f66eb50421fe1b9d2c92dc Mon Sep 17 00:00:00 2001 From: Hasibul Hasan Anik Date: Thu, 5 Mar 2020 13:01:46 +0600 Subject: [PATCH 09/10] Remove nil check for empty map. --- modules/net_sniff/net_sniff_upnp.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/net_sniff/net_sniff_upnp.go b/modules/net_sniff/net_sniff_upnp.go index f0b7d48b..cf2f8ffd 100644 --- a/modules/net_sniff/net_sniff_upnp.go +++ b/modules/net_sniff/net_sniff_upnp.go @@ -13,7 +13,7 @@ import ( ) func upnpParser(ip *layers.IPv4, pkt gopacket.Packet, udp *layers.UDP) bool { - if data := packets.UPNPGetMeta(pkt); data != nil && len(data) > 0 { + if data := packets.UPNPGetMeta(pkt); len(data) > 0 { s := "" for name, value := range data { s += fmt.Sprintf("%s:%s ", tui.Blue(name), tui.Yellow(value)) From b253e6b4dfa2af83ba8f4af18a8c2b7b95f7014e Mon Sep 17 00:00:00 2001 From: Hasibul Hasan Anik Date: Thu, 5 Mar 2020 13:02:30 +0600 Subject: [PATCH 10/10] Remove unnecessary variable assignment --- modules/mysql_server/mysql_server.go | 2 +- modules/wifi/wifi_hopping.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/mysql_server/mysql_server.go b/modules/mysql_server/mysql_server.go index 3ddb1d97..09167a62 100644 --- a/modules/mysql_server/mysql_server.go +++ b/modules/mysql_server/mysql_server.go @@ -121,7 +121,7 @@ func (mod *MySQLServer) Start() error { if _, err := conn.Write(packets.MySQLGreeting); err != nil { mod.Warning("error while writing server greeting: %s", err) continue - } else if read, err = reader.Read(readBuffer); err != nil { + } else if _, err = reader.Read(readBuffer); err != nil { mod.Warning("error while reading client message: %s", err) continue } diff --git a/modules/wifi/wifi_hopping.go b/modules/wifi/wifi_hopping.go index a89f8b4f..0dfb78db 100644 --- a/modules/wifi/wifi_hopping.go +++ b/modules/wifi/wifi_hopping.go @@ -92,7 +92,7 @@ func (mod *WiFiModule) channelHopper() { } select { - case _ = <-mod.hopChanges: + case <-mod.hopChanges: mod.Debug("hop changed") break loopCurrentChannels case <-time.After(delay):