|
| 1 | +package command |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "os" |
| 7 | + "os/signal" |
| 8 | + "runtime" |
| 9 | + "strconv" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/v-byte-cpu/sx/pkg/scan" |
| 14 | + "github.com/v-byte-cpu/sx/pkg/scan/arp" |
| 15 | + "github.com/v-byte-cpu/sx/pkg/scan/icmp" |
| 16 | +) |
| 17 | + |
| 18 | +var ( |
| 19 | + cliICMPTypeFlag string |
| 20 | + cliICMPCodeFlag string |
| 21 | + cliICMPPayloadFlag string |
| 22 | + |
| 23 | + cliICMPType uint8 |
| 24 | + cliICMPCode uint8 |
| 25 | + cliICMPPayload []byte |
| 26 | +) |
| 27 | + |
| 28 | +func init() { |
| 29 | + icmpCmd.Flags().StringVar(&cliIPTTLFlag, "ttl", "", |
| 30 | + strings.Join([]string{"set IP TTL field of generated packet", "64 by default"}, "\n")) |
| 31 | + icmpCmd.Flags().StringVar(&cliIPTotalLenFlag, "iplen", "", |
| 32 | + strings.Join([]string{"set IP Total Length field of generated packet", "calculated by default"}, "\n")) |
| 33 | + icmpCmd.Flags().StringVar(&cliIPProtocolFlag, "ipproto", "", |
| 34 | + strings.Join([]string{"set IP Protocol field of generated packet", "1 (ICMP) by default"}, "\n")) |
| 35 | + icmpCmd.Flags().StringVar(&cliIPFlagsFlag, "ipflags", "", |
| 36 | + strings.Join([]string{"set IP Flags field of generated packet", "DF by default"}, "\n")) |
| 37 | + |
| 38 | + icmpCmd.Flags().StringVarP(&cliICMPTypeFlag, "type", "t", "", |
| 39 | + strings.Join([]string{"set ICMP type of generated packet", "ICMP Echo (Type 8) by default"}, "\n")) |
| 40 | + icmpCmd.Flags().StringVarP(&cliICMPCodeFlag, "code", "c", "", |
| 41 | + strings.Join([]string{"set ICMP code of generated packet", "0 by default"}, "\n")) |
| 42 | + icmpCmd.Flags().StringVarP(&cliICMPPayloadFlag, "payload", "p", "", |
| 43 | + strings.Join([]string{"set byte payload of generated packet", "48 random bytes by default"}, "\n")) |
| 44 | + |
| 45 | + icmpCmd.Flags().StringVarP(&cliARPCacheFileFlag, "arp-cache", "a", "", |
| 46 | + strings.Join([]string{"set ARP cache file", "reads from stdin by default"}, "\n")) |
| 47 | + rootCmd.AddCommand(icmpCmd) |
| 48 | +} |
| 49 | + |
| 50 | +var icmpCmd = &cobra.Command{ |
| 51 | + Use: "icmp [flags] subnet", |
| 52 | + Example: strings.Join([]string{ |
| 53 | + "icmp 192.168.0.1/24", |
| 54 | + "icmp --ttl 37 192.168.0.1/24", |
| 55 | + "icmp --ipproto 157 192.168.0.1/24", |
| 56 | + `icmp --type 13 --code 0 --payload '\x01\x02\x03' 10.0.0.1`}, "\n"), |
| 57 | + Short: "Perform ICMP scan", |
| 58 | + PreRunE: func(cmd *cobra.Command, args []string) (err error) { |
| 59 | + if len(args) != 1 { |
| 60 | + return errors.New("requires one ip subnet argument") |
| 61 | + } |
| 62 | + var icmpType uint64 |
| 63 | + if len(cliICMPTypeFlag) > 0 { |
| 64 | + if icmpType, err = strconv.ParseUint(cliICMPTypeFlag, 10, 8); err != nil { |
| 65 | + return |
| 66 | + } |
| 67 | + cliICMPType = uint8(icmpType) |
| 68 | + } |
| 69 | + var icmpCode uint64 |
| 70 | + if len(cliICMPCodeFlag) > 0 { |
| 71 | + if icmpCode, err = strconv.ParseUint(cliICMPCodeFlag, 10, 8); err != nil { |
| 72 | + return |
| 73 | + } |
| 74 | + cliICMPCode = uint8(icmpCode) |
| 75 | + } |
| 76 | + if len(cliICMPPayloadFlag) > 0 { |
| 77 | + if cliICMPPayload, err = parsePacketPayload(cliICMPPayloadFlag); err != nil { |
| 78 | + return |
| 79 | + } |
| 80 | + } |
| 81 | + return |
| 82 | + }, |
| 83 | + RunE: func(cmd *cobra.Command, args []string) (err error) { |
| 84 | + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) |
| 85 | + defer cancel() |
| 86 | + |
| 87 | + var conf *scanConfig |
| 88 | + if conf, err = parseScanConfig(icmp.ScanType, args[0]); err != nil { |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + m := newICMPScanMethod(ctx, conf) |
| 93 | + |
| 94 | + return startPacketScanEngine(ctx, newPacketScanConfig( |
| 95 | + withPacketScanMethod(m), |
| 96 | + withPacketBPFFilter(icmp.BPFFilter), |
| 97 | + withPacketEngineConfig(newEngineConfig( |
| 98 | + withLogger(conf.logger), |
| 99 | + withScanRange(conf.scanRange), |
| 100 | + )), |
| 101 | + )) |
| 102 | + }, |
| 103 | +} |
| 104 | + |
| 105 | +func newICMPScanMethod(ctx context.Context, conf *scanConfig) *icmp.ScanMethod { |
| 106 | + reqgen := arp.NewCacheRequestGenerator( |
| 107 | + scan.NewIPRequestGenerator(scan.NewIPGenerator()), conf.gatewayIP, conf.cache) |
| 108 | + pktgen := scan.NewPacketMultiGenerator(icmp.NewPacketFiller(getICMPOptions()...), runtime.NumCPU()) |
| 109 | + psrc := scan.NewPacketSource(reqgen, pktgen) |
| 110 | + results := scan.NewResultChan(ctx, 1000) |
| 111 | + return icmp.NewScanMethod(psrc, results) |
| 112 | +} |
| 113 | + |
| 114 | +func getICMPOptions() (opts []icmp.PacketFillerOption) { |
| 115 | + if len(cliIPTTLFlag) > 0 { |
| 116 | + opts = append(opts, icmp.WithTTL(cliTTL)) |
| 117 | + } |
| 118 | + if len(cliIPTotalLenFlag) > 0 { |
| 119 | + opts = append(opts, icmp.WithIPTotalLength(cliIPTotalLen)) |
| 120 | + } |
| 121 | + if len(cliIPProtocolFlag) > 0 { |
| 122 | + opts = append(opts, icmp.WithIPProtocol(cliIPProtocol)) |
| 123 | + } |
| 124 | + if len(cliIPFlagsFlag) > 0 { |
| 125 | + opts = append(opts, icmp.WithIPFlags(cliIPFlags)) |
| 126 | + } |
| 127 | + if len(cliICMPTypeFlag) > 0 { |
| 128 | + opts = append(opts, icmp.WithType(cliICMPType)) |
| 129 | + } |
| 130 | + if len(cliICMPCodeFlag) > 0 { |
| 131 | + opts = append(opts, icmp.WithCode(cliICMPCode)) |
| 132 | + } |
| 133 | + if len(cliICMPPayloadFlag) > 0 { |
| 134 | + opts = append(opts, icmp.WithPayload(cliICMPPayload)) |
| 135 | + } |
| 136 | + return |
| 137 | +} |
0 commit comments