|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "flag" |
| 5 | + "fmt" |
| 6 | + "net/netip" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "regexp" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/rs/zerolog" |
| 13 | + "github.com/shadowy-pycoder/colors" |
| 14 | + "github.com/shadowy-pycoder/mshark/arpspoof" |
| 15 | + "github.com/shadowy-pycoder/mshark/network" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + app = "marpspoof" |
| 20 | + ipPortPattern = regexp.MustCompile( |
| 21 | + `\b(?:\d{1,3}\.){3}\d{1,3}(?::(6553[0-5]|655[0-2]\d|65[0-4]\d{2}|6[0-4]\d{3}|[1-5]?\d{1,4}))?\b`, |
| 22 | + ) |
| 23 | + macPattern = regexp.MustCompile(`(?i)([a-z0-9_]+_[0-9a-f]{2}(?::[0-9a-f]{2}){2}|(?:[0-9a-f]{2}[:-]){5}[0-9a-f]{2})`) |
| 24 | +) |
| 25 | + |
| 26 | +func root(args []string) error { |
| 27 | + conf := &arpspoof.ARPSpoofConfig{} |
| 28 | + flags := flag.NewFlagSet(app, flag.ExitOnError) |
| 29 | + flags.StringVar( |
| 30 | + &conf.Targets, |
| 31 | + "t", |
| 32 | + "", |
| 33 | + "Targets for ARP spoofing. Example: \"targets 10.0.0.1,10.0.0.5-10,192.168.1.*,192.168.10.0/24\" (Default: entire subnet)", |
| 34 | + ) |
| 35 | + gw := flags.String("g", "", "IPv4 address of custom gateway (Default: default gateway)") |
| 36 | + flags.StringVar(&conf.Interface, "i", "", "The name of the network interface. Example: eth0 (Default: default interface)") |
| 37 | + flags.BoolVar(&conf.FullDuplex, "f", false, "Run ARP spoofing in fullduplex mode") |
| 38 | + flags.BoolVar(&conf.Debug, "d", false, "Enable debug logging") |
| 39 | + nocolor := flags.Bool("nocolor", false, "Disable colored output") |
| 40 | + flags.BoolFunc("I", "Display list of interfaces and exit.", func(flagValue string) error { |
| 41 | + if err := network.DisplayInterfaces(); err != nil { |
| 42 | + fmt.Fprintf(os.Stderr, "%s: %v\n", app, err) |
| 43 | + os.Exit(2) |
| 44 | + } |
| 45 | + os.Exit(0) |
| 46 | + return nil |
| 47 | + }) |
| 48 | + if err := flags.Parse(args); err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + if *gw != "" { |
| 52 | + ip, err := netip.ParseAddr(*gw) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + conf.Gateway = &ip |
| 57 | + } |
| 58 | + output := zerolog.ConsoleWriter{Out: os.Stdout, NoColor: *nocolor} |
| 59 | + output.FormatTimestamp = func(i any) string { |
| 60 | + ts, _ := time.Parse(time.RFC3339, i.(string)) |
| 61 | + if *nocolor { |
| 62 | + return colors.WrapBrackets(ts.Format(time.TimeOnly)) |
| 63 | + } |
| 64 | + return colors.Gray(colors.WrapBrackets(ts.Format(time.TimeOnly))).String() |
| 65 | + } |
| 66 | + output.FormatMessage = func(i any) string { |
| 67 | + if i == nil || i == "" { |
| 68 | + return "" |
| 69 | + } |
| 70 | + s := i.(string) |
| 71 | + if *nocolor { |
| 72 | + return s |
| 73 | + } |
| 74 | + result := ipPortPattern.ReplaceAllStringFunc(s, func(match string) string { |
| 75 | + return colors.Gray(match).String() |
| 76 | + }) |
| 77 | + result = macPattern.ReplaceAllStringFunc(result, func(match string) string { |
| 78 | + return colors.Yellow(match).String() |
| 79 | + }) |
| 80 | + return result |
| 81 | + } |
| 82 | + logger := zerolog.New(output).With().Timestamp().Logger() |
| 83 | + conf.Logger = &logger |
| 84 | + arpspoofer, err := arpspoof.NewARPSpoofer(conf) |
| 85 | + if err != nil { |
| 86 | + return err |
| 87 | + } |
| 88 | + go arpspoofer.Start() |
| 89 | + quit := make(chan os.Signal, 1) |
| 90 | + signal.Notify(quit, os.Interrupt) |
| 91 | + <-quit |
| 92 | + return arpspoofer.Stop() |
| 93 | +} |
| 94 | + |
| 95 | +func main() { |
| 96 | + if err := root(os.Args[1:]); err != nil { |
| 97 | + fmt.Fprintf(os.Stderr, "%s: %v\n", app, err) |
| 98 | + os.Exit(2) |
| 99 | + } |
| 100 | +} |
0 commit comments