mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
Merge branch 'bikeshack:main' into tidy-code
This commit is contained in:
commit
d7d1265fca
3 changed files with 73 additions and 9 deletions
19
cmd/scan.go
19
cmd/scan.go
|
|
@ -2,6 +2,7 @@ package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
|
@ -16,6 +17,7 @@ var (
|
||||||
begin uint8
|
begin uint8
|
||||||
end uint8
|
end uint8
|
||||||
subnets []string
|
subnets []string
|
||||||
|
subnetMasks []net.IP
|
||||||
disableProbing bool
|
disableProbing bool
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -28,8 +30,16 @@ var scanCmd = &cobra.Command{
|
||||||
if len(hosts) > 0 {
|
if len(hosts) > 0 {
|
||||||
hostsToScan = hosts
|
hostsToScan = hosts
|
||||||
} else {
|
} else {
|
||||||
for _, subnet := range subnets {
|
for i, subnet := range subnets {
|
||||||
hostsToScan = append(hostsToScan, magellan.GenerateHosts(subnet, begin, end)...)
|
if len(subnet) <= 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(subnetMasks) < i + 1 {
|
||||||
|
subnetMasks = append(subnetMasks, net.IP{255, 255, 255, 0})
|
||||||
|
}
|
||||||
|
|
||||||
|
hostsToScan = append(hostsToScan, magellan.GenerateHosts(subnet, &subnetMasks[i])...)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,9 +73,10 @@ var scanCmd = &cobra.Command{
|
||||||
func init() {
|
func init() {
|
||||||
scanCmd.Flags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts to scan")
|
scanCmd.Flags().StringSliceVar(&hosts, "host", []string{}, "set additional hosts to scan")
|
||||||
scanCmd.Flags().IntSliceVar(&ports, "port", []int{}, "set the ports to scan")
|
scanCmd.Flags().IntSliceVar(&ports, "port", []int{}, "set the ports to scan")
|
||||||
scanCmd.Flags().Uint8Var(&begin, "begin", 0, "set the starting point for range of IP addresses")
|
// scanCmd.Flags().Uint8Var(&begin, "begin", 0, "set the starting point for range of IP addresses")
|
||||||
scanCmd.Flags().Uint8Var(&end, "end", 255, "set the ending point for range of IP addresses")
|
// scanCmd.Flags().Uint8Var(&end, "end", 255, "set the ending point for range of IP addresses")
|
||||||
scanCmd.Flags().StringSliceVar(&subnets, "subnet", []string{}, "set additional subnets")
|
scanCmd.Flags().StringSliceVar(&subnets, "subnet", []string{}, "set additional subnets")
|
||||||
|
scanCmd.Flags().IPSliceVar(&subnetMasks, "subnet-mask", []net.IP{}, "set the subnet masks to use for network")
|
||||||
scanCmd.Flags().BoolVar(&disableProbing, "disable-probing", false, "disable probing scanned results for BMC nodes")
|
scanCmd.Flags().BoolVar(&disableProbing, "disable-probing", false, "disable probing scanned results for BMC nodes")
|
||||||
|
|
||||||
rootCmd.AddCommand(scanCmd)
|
rootCmd.AddCommand(scanCmd)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@ package magellan
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
@ -50,12 +51,47 @@ func rawConnect(host string, ports []int, timeout int, keepOpenOnly bool) []Scan
|
||||||
return results
|
return results
|
||||||
}
|
}
|
||||||
|
|
||||||
func GenerateHosts(subnet string, begin uint8, end uint8) []string {
|
|
||||||
|
func GenerateHosts(subnet string, subnetMask *net.IP) []string {
|
||||||
|
if subnet == "" || subnetMask == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// convert subnets from string to net.IP
|
||||||
|
subnetIp := net.ParseIP(subnet)
|
||||||
|
if subnetIp == nil {
|
||||||
|
// try parse CIDR instead
|
||||||
|
ip, network, err := net.ParseCIDR(subnet)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
subnetIp = ip
|
||||||
|
if network != nil {
|
||||||
|
t := net.IP(network.Mask)
|
||||||
|
subnetMask = &t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mask := net.IPMask(subnetMask.To4())
|
||||||
|
|
||||||
|
// if no subnet mask, use a default 24-bit mask (for now)
|
||||||
|
return generateHosts(&subnetIp, &mask)
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateHosts(ip *net.IP, mask *net.IPMask) []string {
|
||||||
|
// get all IP addresses in network
|
||||||
|
ones, _ := mask.Size()
|
||||||
hosts := []string{}
|
hosts := []string{}
|
||||||
ip := net.ParseIP(subnet).To4()
|
end := int(math.Pow(2, float64((32-ones))))-1
|
||||||
for i := begin; i < end; i++ {
|
for i := 0; i < end; i++ {
|
||||||
ip[3] = byte(i)
|
// ip[3] = byte(i)
|
||||||
hosts = append(hosts, fmt.Sprintf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]))
|
ip = util.GetNextIP(ip, 1)
|
||||||
|
if ip == nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
// host := fmt.Sprintf("%v.%v.%v.%v", (*ip)[0], (*ip)[1], (*ip)[2], (*ip)[3])
|
||||||
|
// fmt.Printf("host: %v\n", ip.String())
|
||||||
|
hosts = append(hosts, ip.String())
|
||||||
}
|
}
|
||||||
return hosts
|
return hosts
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
|
|
@ -17,6 +18,22 @@ func PathExists(path string) (bool, error) {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetNextIP(ip *net.IP, inc uint) *net.IP {
|
||||||
|
if ip == nil {
|
||||||
|
return &net.IP{}
|
||||||
|
}
|
||||||
|
i := ip.To4()
|
||||||
|
v := uint(i[0])<<24 + uint(i[1])<<16 + uint(i[2])<<8 + uint(i[3])
|
||||||
|
v += inc
|
||||||
|
v3 := byte(v & 0xFF)
|
||||||
|
v2 := byte((v >> 8) & 0xFF)
|
||||||
|
v1 := byte((v >> 16) & 0xFF)
|
||||||
|
v0 := byte((v >> 24) & 0xFF)
|
||||||
|
// return &net.IP{[]byte{v0, v1, v2, v3}}
|
||||||
|
r := net.IPv4(v0, v1, v2, v3)
|
||||||
|
return &r
|
||||||
|
}
|
||||||
|
|
||||||
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
|
func MakeRequest(url string, httpMethod string, body []byte, headers map[string]string) (*http.Response, []byte, error) {
|
||||||
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
|
||||||
req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
|
req, err := http.NewRequest(httpMethod, url, bytes.NewBuffer(body))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue