Skip to content

Commit

Permalink
兼容Windows,Linux,Mac三系统
Browse files Browse the repository at this point in the history
  • Loading branch information
boy-hack committed Aug 21, 2020
1 parent 6baafae commit 8940920
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 35 deletions.
4 changes: 3 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ var flag_filename = flag.String("f", "", "爆破字典路径")
var flag_resolvers = flag.String("s", "", "resolvers文件路径")
var flag_output = flag.String("o", "", "输出文件路径")
var flag_test = flag.Bool("test", false, "测试本地最大发包数")
var flag_default = flag.Int("e", -1, "默认网络设备ID,默认-1,如果有多个网络设备会在命令行中选择")

func main() {
flag.Parse()
core.ShowBanner()
bandwith := *flag_bandwith // K M G
domain := *flag_domain
filename := *flag_filename
if *flag_test {
sendog := core.SendDog{}
ether := core.GetDevices()
ether := core.GetDevices(*flag_default)
ether.DstMac = net.HardwareAddr{0x5c, 0xc9, 0x09, 0x33, 0x34, 0x80}
sendog.Init(ether, []string{"8.8.8.8"})
defer sendog.Close()
Expand Down
98 changes: 66 additions & 32 deletions core/device.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package core

import (
"bufio"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/google/gopacket/pcap"
"log"
"net"
"os"
"strconv"
"strings"
"time"
)

Expand All @@ -15,59 +19,83 @@ import (
//SrcMac net.HardwareAddr = net.HardwareAddr{0xf0, 0x18, 0x98, 0x1a, 0x56, 0xe8}
//DstMac net.HardwareAddr = net.HardwareAddr{0x5c, 0xc9, 0x99, 0x33, 0x34, 0x80

func GetDevices() EthTable {
netInterfaces, err := net.Interfaces()
func GetDevices(defaultSelect int) EthTable {
// Find all devices
devices, err := pcap.FindAllDevs()
if err != nil {
fmt.Printf("fail to get net interfaces: %v", err)
log.Fatal(err)
}
data := make(map[string]net.IP)
keys := []string{}

for _, netInterface := range netInterfaces {
interfaceName := netInterface.Name
inter, err := net.InterfaceByName(interfaceName)
if err != nil {
log.Fatalf("无法获取信息: %v", err)
for _, d := range devices {
for _, address := range d.Addresses {
ip := address.IP
if ip.To4() != nil && !ip.IsLoopback() {
fmt.Printf("\n[%d] Name: %s\n", len(keys), d.Name)
fmt.Println("Description: ", d.Description)
fmt.Println("Devices addresses: ", d.Description)
fmt.Println("IP address: ", ip)
fmt.Println("Subnet mask: ", address.Netmask)
data[d.Name] = ip
keys = append(keys, d.Name)
}
}

addrs, err := inter.Addrs()

}
if len(keys) == 0 {
panic("获取不到可用的设备名称")
} else if len(keys) == 1 {
defaultSelect = 0
}
if defaultSelect == -1 {
var i int
fmt.Println("选择一个可用网卡ID:")
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
if err != nil {
log.Fatalln(err)
panic("There were errors reading, exiting program.")
}
// 获取IP地址,子网掩码
for _, addr := range addrs {
if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() {
if ip.IP.To4() != nil {
fmt.Println("Name:", interfaceName)
fmt.Println("IP:", ip.IP)
fmt.Println("Mask:", ip.Mask)
fmt.Println("Mac:", inter.HardwareAddr.String())
c := GetGateMacAddress(interfaceName)
fmt.Println("GateWay Mac:", c)
return EthTable{ip.IP, interfaceName, inter.HardwareAddr, c}
}
}
i, err2 := strconv.Atoi(strings.TrimSpace(input))
if err2 != nil {
panic("There were errors reading, exiting program.")
}

if i < 0 || i >= len(keys) {
panic("ID超出了范围")
}
defaultSelect = i
}
panic("获取不到可用的IP或网关搜索失败")
deviceName := keys[defaultSelect]
ip := data[deviceName]
fmt.Println("Use Device:", deviceName)
fmt.Println("Use IP:", ip)
c := GetGateMacAddress(deviceName)
fmt.Println("Local Mac:", c[1])
fmt.Println("GateWay Mac:", c[0])
fmt.Println("")
return EthTable{ip, deviceName, c[1], c[0]}
}

func GetGateMacAddress(dvice string) net.HardwareAddr {
func GetGateMacAddress(dvice string) [2]net.HardwareAddr {
// 获取网关mac地址
domain := RandomStr(4) + "paper.seebug.org"
_signal := make(chan net.HardwareAddr)
go func(device string, domain string, signal chan net.HardwareAddr) {
_signal := make(chan [2]net.HardwareAddr)
go func(device string, domain string, signal chan [2]net.HardwareAddr) {
var (
snapshot_len int32 = 1024
promiscuous bool = false
timeout time.Duration = -1 * time.Second
handle *pcap.Handle
)
handle, _ = pcap.OpenLive(
var err error
handle, err = pcap.OpenLive(
device,
snapshot_len,
promiscuous,
timeout,
)
if err != nil {
panic(err)
}
defer handle.Close()
// Use the handle as a packet source to process all packets
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
Expand All @@ -87,7 +115,11 @@ func GetGateMacAddress(dvice string) net.HardwareAddr {
ethLayer := packet.Layer(layers.LayerTypeEthernet)
if ethLayer != nil {
eth := ethLayer.(*layers.Ethernet)
signal <- eth.SrcMAC
srcMac := eth.SrcMAC
dstMac := eth.DstMAC

signal <- [2]net.HardwareAddr{srcMac, dstMac}
// 网关mac 和 本地mac
return
}
}
Expand All @@ -96,7 +128,7 @@ func GetGateMacAddress(dvice string) net.HardwareAddr {

}
}(dvice, domain, _signal)
var c net.HardwareAddr
var c [2]net.HardwareAddr
for {
select {
case c = <-_signal:
Expand All @@ -107,5 +139,7 @@ func GetGateMacAddress(dvice string) net.HardwareAddr {
}
}
END:

fmt.Print("\n")
return c
}
3 changes: 1 addition & 2 deletions core/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
)

func Start(domain string, filename string, bandwith string, reslovers []string, output string) {
ShowBanner()
if filename != "" && string(domain[0]) != "." {
domain = "." + domain
}
Expand Down Expand Up @@ -40,7 +39,7 @@ func Start(domain string, filename string, bandwith string, reslovers []string,

version := pcap.Version()
fmt.Println(version)
ether := GetDevices()
ether := GetDevices(-1)
LocalStack = NewStack()
go Recv(ether.Device, output)
fmt.Println("启动接收模块,设置rate:", rate, "pps")
Expand Down
39 changes: 39 additions & 0 deletions test2/test_net_interface/net_interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"fmt"
"log"
"net"
)

func main() {
netInterfaces, err := net.Interfaces()
if err != nil {
fmt.Printf("fail to get net interfaces: %v", err)
}

for _, netInterface := range netInterfaces {
interfaceName := netInterface.Name
inter, err := net.InterfaceByName(interfaceName)
if err != nil {
log.Fatalf("无法获取信息: %v", err)
}

addrs, err := inter.Addrs()

if err != nil {
log.Fatalln(err)
}
// 获取IP地址,子网掩码
for _, addr := range addrs {
if ip, ok := addr.(*net.IPNet); ok && !ip.IP.IsLoopback() {
if ip.IP.To4() != nil {
fmt.Println("Name:", interfaceName)
fmt.Println("IP:", ip.IP)
fmt.Println("Mask:", ip.Mask)
fmt.Println("Mac:", inter.HardwareAddr.String())
}
}
}
}
}
30 changes: 30 additions & 0 deletions test2/test_pcap_interface/pcap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package main

import (
"fmt"
"github.com/google/gopacket/pcap"
"log"
)

func main() {
// Find all devices
devices, err := pcap.FindAllDevs()
if err != nil {
log.Fatal(err)
}

// Print device information
fmt.Println("Devices found:")
for _, d := range devices {
for _, address := range d.Addresses {
ip := address.IP
if ip.To4() != nil && !ip.IsLoopback() {
fmt.Println("Name: ", d.Name)
fmt.Println("Description: ", d.Description)
fmt.Println("Devices addresses: ", d.Description)
fmt.Println("- IP address: ", address.IP)
fmt.Println("- Subnet mask: ", address.Netmask)
}
}
}
}

0 comments on commit 8940920

Please sign in to comment.