diff --git a/modules/net_sniff.go b/modules/net_sniff.go index e77e2bef..49ab2902 100644 --- a/modules/net_sniff.go +++ b/modules/net_sniff.go @@ -4,6 +4,7 @@ import ( "fmt" "time" + "github.com/bettercap/bettercap/log" "github.com/bettercap/bettercap/session" "github.com/google/gopacket" @@ -170,6 +171,7 @@ func (s *Sniffer) Start() error { s.pktSourceChan = src.Packets() for packet := range s.pktSourceChan { if !s.Running() { + log.Debug("end pkt loop (pkt=%v filter='%s')", packet, s.Ctx.Filter) break } @@ -209,9 +211,14 @@ func (s *Sniffer) Start() error { func (s *Sniffer) Stop() error { return s.SetRunning(false, func() { + log.Debug("stopping sniffer") if s.pktSourceChan != nil { + log.Debug("sending nil") s.pktSourceChan <- nil + log.Debug("nil sent") } + log.Debug("closing ctx") s.Ctx.Close() + log.Debug("ctx closed") }) } diff --git a/modules/net_sniff_context.go b/modules/net_sniff_context.go index df943c2a..0a30ad8b 100644 --- a/modules/net_sniff_context.go +++ b/modules/net_sniff_context.go @@ -3,6 +3,7 @@ package modules import ( "os" "regexp" + "time" "github.com/bettercap/bettercap/log" "github.com/bettercap/bettercap/session" @@ -36,7 +37,12 @@ func (s *Sniffer) GetContext() (error, *SnifferContext) { } if ctx.Source == "" { - if ctx.Handle, err = pcap.OpenLive(s.Session.Interface.Name(), 65536, true, pcap.BlockForever); err != nil { + /* + * We don't want to pcap.BlockForever otherwise pcap_close(handle) + * could hang waiting for a timeout to expire ... + */ + readTimeout := 500 * time.Millisecond + if ctx.Handle, err = pcap.OpenLive(s.Session.Interface.Name(), 65536, true, readTimeout); err != nil { return err, ctx } } else { @@ -117,12 +123,16 @@ func (c *SnifferContext) Log(sess *session.Session) { func (c *SnifferContext) Close() { if c.Handle != nil { + log.Debug("closing handle") c.Handle.Close() + log.Debug("handle closed") c.Handle = nil } if c.OutputFile != nil { + log.Debug("closing output") c.OutputFile.Close() + log.Debug("output closed") c.OutputFile = nil } } diff --git a/modules/wifi.go b/modules/wifi.go index a8211bae..881ada2d 100644 --- a/modules/wifi.go +++ b/modules/wifi.go @@ -236,7 +236,13 @@ func (w *WiFiModule) Configure() error { return fmt.Errorf("Error while setting interface %s in monitor mode: %s", tui.Bold(w.Session.Interface.Name()), err) } else if err = ihandle.SetSnapLen(65536); err != nil { return err - } else if err = ihandle.SetTimeout(pcap.BlockForever); err != nil { + } + /* + * We don't want to pcap.BlockForever otherwise pcap_close(handle) + * could hang waiting for a timeout to expire ... + */ + readTimeout := 500 * time.Millisecond + if err = ihandle.SetTimeout(readTimeout); err != nil { return err } else if w.handle, err = ihandle.Activate(); err != nil { return err