From 4474c802b5b5bb9b9a420170f565559d425a10ab Mon Sep 17 00:00:00 2001 From: boy-hack <34109680@qq.com> Date: Wed, 9 Sep 2020 15:14:29 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=8A=A0=E5=85=A5=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E7=BD=91=E5=8D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/device.go | 97 +++++++++++++++++++++++++++++++++++++++++++++++--- core/start.go | 7 +++- 2 files changed, 98 insertions(+), 6 deletions(-) diff --git a/core/device.go b/core/device.go index cf89202..31436b7 100644 --- a/core/device.go +++ b/core/device.go @@ -1,6 +1,7 @@ package core import ( + "context" "fmt" "github.com/google/gopacket" "github.com/google/gopacket/layers" @@ -10,11 +11,97 @@ import ( "time" ) -//SrcIp string = "10.13.20.60" -//device string = "en0" -//SrcMac net.HardwareAddr = net.HardwareAddr{0xf0, 0x18, 0x98, 0x1a, 0x56, 0xe8} -//DstMac net.HardwareAddr = net.HardwareAddr{0x5c, 0xc9, 0x99, 0x33, 0x34, 0x80 - +func AutoGetDevices() EthTable { + domain := RandomStr(4) + "paper.seebug.org" + signal := make(chan EthTable) + devices, err := pcap.FindAllDevs() + if err != nil { + gologger.Fatalf("获取网络设备失败:%s\n", err.Error()) + } + data := make(map[string]net.IP) + keys := []string{} + for _, d := range devices { + for _, address := range d.Addresses { + ip := address.IP + if ip.To4() != nil && !ip.IsLoopback() { + data[d.Name] = ip + keys = append(keys, d.Name) + } + } + } + ctx := context.Background() + // 在初始上下文的基础上创建一个有取消功能的上下文 + ctx, cancel := context.WithCancel(ctx) + for _, drviceName := range keys { + go func(drviceName string, domain string, signal chan EthTable, ctx context.Context) { + var ( + snapshot_len int32 = 1024 + promiscuous bool = false + timeout time.Duration = -1 * time.Second + handle *pcap.Handle + ) + var err error + handle, err = pcap.OpenLive( + drviceName, + snapshot_len, + promiscuous, + timeout, + ) + if err != nil { + gologger.Fatalf("pcap打开失败:%s\n", err.Error()) + } + defer handle.Close() + // Use the handle as a packet source to process all packets + packetSource := gopacket.NewPacketSource(handle, handle.LinkType()) + for { + select { + case <-ctx.Done(): + return + default: + packet, err := packetSource.NextPacket() + gologger.Printf(".") + if err != nil { + continue + } + if dnsLayer := packet.Layer(layers.LayerTypeDNS); dnsLayer != nil { + dns, _ := dnsLayer.(*layers.DNS) + if !dns.QR { + continue + } + for _, v := range dns.Questions { + if string(v.Name) == domain { + ethLayer := packet.Layer(layers.LayerTypeEthernet) + if ethLayer != nil { + eth := ethLayer.(*layers.Ethernet) + signal <- EthTable{SrcIp: data[drviceName], Device: drviceName, SrcMac: eth.SrcMAC, DstMac: eth.DstMAC} + // 网关mac 和 本地mac + return + } + } + } + } + } + } + }(drviceName, domain, signal, ctx) + } + var c EthTable + for { + select { + case c = <-signal: + cancel() + goto END + default: + _, _ = net.LookupHost(domain) + time.Sleep(time.Millisecond * 20) + } + } +END: + gologger.Infof("Use Device: %s\n", c.Device) + gologger.Infof("Use IP:%s\n", c.SrcIp.String()) + gologger.Infof("Local Mac:%s\n", c.SrcMac.String()) + gologger.Infof("GateWay Mac:%s\n", c.DstMac.String()) + return c +} func GetDevices(options *Options) EthTable { // Find all devices defaultSelect := options.NetworkId diff --git a/core/start.go b/core/start.go index c480b47..0800a2e 100644 --- a/core/start.go +++ b/core/start.go @@ -20,7 +20,12 @@ func PrintStatus() { func Start(options *Options) { version := pcap.Version() gologger.Infof(version + "\n") - ether := GetDevices(options) + var ether EthTable + if options.NetworkId >= 0 { + ether = AutoGetDevices() + } else { + ether = GetDevices(options) + } LocalStack = NewStack() // 设定接收的ID flagID := uint16(RandInt64(400, 654)) From 5335c048ef6d0674cca2e9a9565d619864e600d4 Mon Sep 17 00:00:00 2001 From: boy-hack <34109680@qq.com> Date: Wed, 9 Sep 2020 16:16:24 +0800 Subject: [PATCH 2/4] fix a bug --- core/device.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/device.go b/core/device.go index 31436b7..e1e65b2 100644 --- a/core/device.go +++ b/core/device.go @@ -73,7 +73,7 @@ func AutoGetDevices() EthTable { ethLayer := packet.Layer(layers.LayerTypeEthernet) if ethLayer != nil { eth := ethLayer.(*layers.Ethernet) - signal <- EthTable{SrcIp: data[drviceName], Device: drviceName, SrcMac: eth.SrcMAC, DstMac: eth.DstMAC} + signal <- EthTable{SrcIp: data[drviceName], Device: drviceName, SrcMac: eth.DstMAC, DstMac: eth.SrcMAC} // 网关mac 和 本地mac return } From 4f67f5f4f2e0e2f79bb529aee6648b267a764a44 Mon Sep 17 00:00:00 2001 From: boy-hack <34109680@qq.com> Date: Wed, 9 Sep 2020 16:24:24 +0800 Subject: [PATCH 3/4] update version to 0.5 --- core/banner.go | 2 +- core/device.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/core/banner.go b/core/banner.go index c5e8532..4e6212f 100644 --- a/core/banner.go +++ b/core/banner.go @@ -4,7 +4,7 @@ import ( "ksubdomain/gologger" ) -const Version = "0.4" +const Version = "0.5" const banner = ` _ __ _____ _ _ _ | |/ / / ____| | | | | (_) diff --git a/core/device.go b/core/device.go index e1e65b2..55c4048 100644 --- a/core/device.go +++ b/core/device.go @@ -96,7 +96,7 @@ func AutoGetDevices() EthTable { } } END: - gologger.Infof("Use Device: %s\n", c.Device) + gologger.Infof("\nUse Device: %s\n", c.Device) gologger.Infof("Use IP:%s\n", c.SrcIp.String()) gologger.Infof("Local Mac:%s\n", c.SrcMac.String()) gologger.Infof("GateWay Mac:%s\n", c.DstMac.String()) From 6d156ccddfaf9a3deb19dfdf49b42f06ddbdc140 Mon Sep 17 00:00:00 2001 From: boy-hack <34109680@qq.com> Date: Wed, 9 Sep 2020 16:27:54 +0800 Subject: [PATCH 4/4] fix a bug --- core/device.go | 3 ++- core/start.go | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/core/device.go b/core/device.go index 55c4048..ffcacc6 100644 --- a/core/device.go +++ b/core/device.go @@ -96,7 +96,8 @@ func AutoGetDevices() EthTable { } } END: - gologger.Infof("\nUse Device: %s\n", c.Device) + gologger.Printf("\n") + gologger.Infof("Use Device: %s\n", c.Device) gologger.Infof("Use IP:%s\n", c.SrcIp.String()) gologger.Infof("Local Mac:%s\n", c.SrcMac.String()) gologger.Infof("GateWay Mac:%s\n", c.DstMac.String()) diff --git a/core/start.go b/core/start.go index 0800a2e..c0aab1f 100644 --- a/core/start.go +++ b/core/start.go @@ -21,7 +21,7 @@ func Start(options *Options) { version := pcap.Version() gologger.Infof(version + "\n") var ether EthTable - if options.NetworkId >= 0 { + if options.NetworkId == -1 { ether = AutoGetDevices() } else { ether = GetDevices(options)