cmd

package
v0.0.0-...-e20221a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 29, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Copyright © 2023 SecOpsBear bear@secopsbear.com

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright © 2023 SecOpsBear bear@secopsbear.com

Index

Constants

This section is empty.

Variables

View Source
var ScanCmd = &cobra.Command{
	Use:   "scan [IP address]",
	Short: "Port scanner to probe for all open ports on a target IP.",
	Long:  `Port scanner to probe for all open ports on a target IP.`,

	PreRunE: func(cmd *cobra.Command, args []string) error {
		if len(args) < 1 {
			return fmt.Errorf("requires argument IP address to scan")
		}
		if _, err := checkIPAddress(args[0]); err != nil {
			return err
		}
		ipAddress = args[0]
		rangeData := strings.Split(portRange, "-")

		if len(rangeData) == 2 {
			var err error
			startPort, err = strconv.Atoi(rangeData[0])
			if err != nil {
				return fmt.Errorf("enter range in the format 15-2000")
			}
			endPort, err = strconv.Atoi(rangeData[1])
			if err != nil {
				return fmt.Errorf("enter range in the format 15-2000")
			}
			if !(startPort >= 1 && startPort <= 65535) {
				return fmt.Errorf("%d port is not valid. It should be between 1-65535", startPort)
			}
			if !(endPort >= 1 && endPort <= 65535) {
				return fmt.Errorf("%d port is not valid. It should be between 1-65535", endPort)
			}
		} else {
			if strings.ToLower(portRange) == "all" {
				startPort = 1
				endPort = 65535
			} else {

				return fmt.Errorf("enter range in the format 15-2000")
			}
		}
		proto := [2]string{"tcp", "udp"}

		if !contains(proto, strings.ToLower(scanProtocol)) {
			return fmt.Errorf("protocol: select either tcp or udp")
		}

		if len(proxyURL) == 0 {
			proxyURL = ""
		} else {
			s := strings.Split(proxyURL, ":")
			if len(s) == 2 {
				ps, err := strconv.Atoi(s[1])
				if err != nil {
					return fmt.Errorf("port should be an integer between 1-65535")
				}
				if !(ps >= 1 && ps <= 65535) {
					return fmt.Errorf("%d port is not valid. It should be between 1-65535", startPort)
				}

				_, err = checkIPAddress(s[0])
				if err != nil {
					return err
				}
			} else {
				return fmt.Errorf("proxy should in the format as 127.0.0.1:9050")
			}

		}

		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {

		fmt.Printf("Scanning %s for open ports between %d-%d\n", ipAddress, startPort, endPort)

		ports := make(chan int, threadCount)
		results := make(chan int)

		var openPorts []int
		for i := 0; i <= cap(ports); i++ {
			go worker(ports, results)
		}

		go func() {
			for i := startPort; i <= endPort; i++ {
				ports <- i
			}
		}()

		for i := startPort; i <= endPort; i++ {

			port := <-results
			if port != 0 {
				fmt.Printf("%d port is open\n", port)
				openPorts = append(openPorts, port)
			}

		}

		close(ports)
		close(results)
		sort.Ints(openPorts)
		op := strings.Join(strings.Fields(fmt.Sprint(openPorts)), ",")
		opData := op[1 : len(op)-1]
		fmt.Printf("List of open ports : %s", opData)
		if outFile != "" {
			outFilefile, err := os.OpenFile(outFile, os.O_RDWR|os.O_CREATE, 0666)
			printFatalError(err)
			defer outFilefile.Close()
			outFilefile.WriteString("Scan IP address: " + ipAddress + "\n")
			outFilefile.WriteString("List of open ports : " + opData + "\n")

		}
	},
}

ScanCmd represents the scan command

Functions

func Execute

func Execute()

Execute adds all child commands to the root command and sets flags appropriately. This is called by main.main(). It only needs to happen once to the rootCmd.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL