Skip to content

Commit

Permalink
ARP cache file option
Browse files Browse the repository at this point in the history
  • Loading branch information
v-byte-cpu committed Mar 28, 2021
1 parent 0b6f42f commit 1493c99
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ or individual ports:
cat arp.cache | ./sx tcp -p 22,443 192.168.0.171
```

It is possible to specify the ARP cache file using the `-a` or `--arp-cache` options:

```
./sx tcp -a arp.cache -p 22,443 192.168.0.171
```

or stdin redirect:

```
./sx tcp -p 22,443 192.168.0.171 < arp.cache
```

You can also use the `tcp syn` subcommand instead of the `tcp`:

```
Expand Down
49 changes: 38 additions & 11 deletions command/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package command

import (
"bufio"
"context"
"errors"
"io"
Expand Down Expand Up @@ -63,13 +64,14 @@ var rootCmd = &cobra.Command{
}

var (
cliJSONFlag bool
cliInterfaceFlag string
cliSrcIPFlag string
cliSrcMACFlag string
cliPortsFlag string
cliRateLimitFlag string
cliExitDelayFlag string
cliJSONFlag bool
cliInterfaceFlag string
cliSrcIPFlag string
cliSrcMACFlag string
cliPortsFlag string
cliRateLimitFlag string
cliExitDelayFlag string
cliARPCacheFileFlag string

cliInterface *net.Interface
cliSrcIP net.IP
Expand All @@ -85,6 +87,7 @@ var (
errSrcMAC = errors.New("invalid source MAC")
errSrcInterface = errors.New("invalid source interface")
errRateLimit = errors.New("invalid ratelimit")
errStdin = errors.New("stdin is from a terminal")
)

func init() {
Expand Down Expand Up @@ -128,10 +131,8 @@ func parseScanConfig(scanName, subnet string) (c *scanConfig, err error) {
return
}

// TODO file argument
// TODO handle pipes
cache := arp.NewCache()
if err = arp.FillCache(cache, os.Stdin); err != nil {
var cache *arp.Cache
if cache, err = parseARPCache(); err != nil {
return
}

Expand All @@ -148,6 +149,32 @@ func parseScanConfig(scanName, subnet string) (c *scanConfig, err error) {
return
}

func parseARPCache() (cache *arp.Cache, err error) {
var r io.Reader
if len(cliARPCacheFileFlag) > 0 {
var f *os.File
if f, err = os.Open(cliARPCacheFileFlag); err != nil {
return
}
defer f.Close()
r = bufio.NewReader(f)
} else {
var info os.FileInfo
if info, err = os.Stdin.Stat(); err != nil {
return
}
// only data being piped to stdin is valid
if (info.Mode() & os.ModeCharDevice) != 0 {
// stdin from terminal is not valid
return nil, errStdin
}
r = os.Stdin
}
cache = arp.NewCache()
err = arp.FillCache(cache, r)
return
}

func parseScanRange(subnet string) (*scan.Range, error) {
dstSubnet, err := ip.ParseIPNet(subnet)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions command/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func init() {
if err := tcpCmd.MarkPersistentFlagRequired("ports"); err != nil {
golog.Fatalln(err)
}
tcpCmd.PersistentFlags().StringVarP(&cliARPCacheFileFlag, "arp-cache", "a", "",
strings.Join([]string{"set ARP cache file", "reads from stdin by default"}, "\n"))
tcpCmd.Flags().StringVar(&cliTCPPacketFlags, "flags", "", "set TCP flags")
rootCmd.AddCommand(tcpCmd)
}
Expand Down
2 changes: 2 additions & 0 deletions command/udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (

func init() {
udpCmd.Flags().StringVarP(&cliPortsFlag, "ports", "p", "", "set ports to scan")
udpCmd.Flags().StringVarP(&cliARPCacheFileFlag, "arp-cache", "a", "",
strings.Join([]string{"set ARP cache file", "reads from stdin by default"}, "\n"))
rootCmd.AddCommand(udpCmd)
}

Expand Down

0 comments on commit 1493c99

Please sign in to comment.