frack it why not dont ping anything
This commit is contained in:
parent
98cfb706cf
commit
4b95f5f25f
1 changed files with 55 additions and 155 deletions
130
main.go
130
main.go
|
@ -1,13 +1,11 @@
|
||||||
package main
|
package main
|
||||||
// sorry in advance for anyone who has to read this
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
|
||||||
"runtime"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
@ -20,18 +18,17 @@ type OpenPort struct {
|
||||||
Hostname string
|
Hostname string
|
||||||
Fingerprint string
|
Fingerprint string
|
||||||
}
|
}
|
||||||
|
|
||||||
const batchSize = 255
|
const batchSize = 255
|
||||||
var printMutex sync.Mutex
|
|
||||||
var (
|
var (
|
||||||
summaryFile *os.File
|
printMutex sync.Mutex
|
||||||
writer *bufio.Writer
|
|
||||||
fileLock sync.Mutex
|
fileLock sync.Mutex
|
||||||
)
|
|
||||||
var (
|
|
||||||
openPorts []OpenPort
|
openPorts []OpenPort
|
||||||
openPortsLock sync.Mutex
|
openPortsLock sync.Mutex
|
||||||
ipLineMap = make(map[string]int)
|
ipLineMap = make(map[string]int)
|
||||||
openPortMsgs []string
|
openPortMsgs []string
|
||||||
|
summaryFile *os.File
|
||||||
)
|
)
|
||||||
|
|
||||||
func clearScreen() {
|
func clearScreen() {
|
||||||
|
@ -75,33 +72,6 @@ func printOpenPortLine(msg string) {
|
||||||
fmt.Println(msg)
|
fmt.Println(msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func isPrivateIP(ipStr string) bool {
|
|
||||||
ip := net.ParseIP(ipStr)
|
|
||||||
if ip == nil {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
privateBlocks := []string{
|
|
||||||
"10.0.0.0/8",
|
|
||||||
"172.16.0.0/12",
|
|
||||||
"192.168.0.0/16",
|
|
||||||
"127.0.0.0/8",
|
|
||||||
}
|
|
||||||
for _, cidr := range privateBlocks {
|
|
||||||
_, block, _ := net.ParseCIDR(cidr)
|
|
||||||
if block.Contains(ip) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
func pingIP(ip string, pingCmd []string) bool {
|
|
||||||
cmd := exec.Command(pingCmd[0], append(pingCmd[1:], ip)...)
|
|
||||||
err := cmd.Run()
|
|
||||||
return err == nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func grabBanner(ip string, port int) string {
|
func grabBanner(ip string, port int) string {
|
||||||
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), 1*time.Second)
|
conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", ip, port), 1*time.Second)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -144,6 +114,7 @@ func scanPort(ip string, port int, wg *sync.WaitGroup) {
|
||||||
hostname = strings.TrimSuffix(host[0], ".")
|
hostname = strings.TrimSuffix(host[0], ".")
|
||||||
}
|
}
|
||||||
fingerprint := identifyServiceAndOS(ip, port)
|
fingerprint := identifyServiceAndOS(ip, port)
|
||||||
|
|
||||||
openPortsLock.Lock()
|
openPortsLock.Lock()
|
||||||
openPorts = append(openPorts, OpenPort{ip, port, hostname, fingerprint})
|
openPorts = append(openPorts, OpenPort{ip, port, hostname, fingerprint})
|
||||||
line := fmt.Sprintf(" - %s : port %d open - hostname: %s", ip, port, hostname)
|
line := fmt.Sprintf(" - %s : port %d open - hostname: %s", ip, port, hostname)
|
||||||
|
@ -155,6 +126,7 @@ func scanPort(ip string, port int, wg *sync.WaitGroup) {
|
||||||
summaryFile.WriteString(line + "\n")
|
summaryFile.WriteString(line + "\n")
|
||||||
fileLock.Unlock()
|
fileLock.Unlock()
|
||||||
openPortsLock.Unlock()
|
openPortsLock.Unlock()
|
||||||
|
|
||||||
msg := fmt.Sprintf("[%s] port %d is OPEN (%s)", ip, port, hostname)
|
msg := fmt.Sprintf("[%s] port %d is OPEN (%s)", ip, port, hostname)
|
||||||
if fingerprint != "" {
|
if fingerprint != "" {
|
||||||
msg += " | " + fingerprint
|
msg += " | " + fingerprint
|
||||||
|
@ -164,7 +136,6 @@ func scanPort(ip string, port int, wg *sync.WaitGroup) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
func scanPortsForIP(ip string, portStart, portEnd int) {
|
func scanPortsForIP(ip string, portStart, portEnd int) {
|
||||||
const maxGoroutines = 50
|
const maxGoroutines = 50
|
||||||
guard := make(chan struct{}, maxGoroutines)
|
guard := make(chan struct{}, maxGoroutines)
|
||||||
|
@ -183,37 +154,6 @@ func scanPortsForIP(ip string, portStart, portEnd int) {
|
||||||
printStatusLine(ip, "scan done.")
|
printStatusLine(ip, "scan done.")
|
||||||
}
|
}
|
||||||
|
|
||||||
func pingIPsConcurrently(ips []string, pingCmd []string) []string {
|
|
||||||
var alive []string
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
var lock sync.Mutex
|
|
||||||
for _, ip := range ips {
|
|
||||||
wg.Add(1)
|
|
||||||
go func(ip string) {
|
|
||||||
defer wg.Done()
|
|
||||||
if !isPrivateIP(ip) {
|
|
||||||
fmt.Printf("[+] skipping ping for public IP %s\n", ip)
|
|
||||||
lock.Lock()
|
|
||||||
alive = append(alive, ip)
|
|
||||||
lock.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if pingIP(ip, pingCmd) {
|
|
||||||
fmt.Printf("[+] found %s\n", ip)
|
|
||||||
lock.Lock()
|
|
||||||
alive = append(alive, ip)
|
|
||||||
lock.Unlock()
|
|
||||||
} else {
|
|
||||||
fmt.Printf("[~] no response from %s\n", ip)
|
|
||||||
}
|
|
||||||
|
|
||||||
}(ip)
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
return alive
|
|
||||||
}
|
|
||||||
|
|
||||||
func ipRange(start, end string) ([]string, error) {
|
func ipRange(start, end string) ([]string, error) {
|
||||||
startIP := net.ParseIP(start).To4()
|
startIP := net.ParseIP(start).To4()
|
||||||
endIP := net.ParseIP(end).To4()
|
endIP := net.ParseIP(end).To4()
|
||||||
|
@ -260,6 +200,7 @@ func main() {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
defer summaryFile.Close()
|
defer summaryFile.Close()
|
||||||
|
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
fmt.Print("enter IP range (e.g., 192.168.1.1-192.168.1.20): ")
|
fmt.Print("enter IP range (e.g., 192.168.1.1-192.168.1.20): ")
|
||||||
ipInput, _ := reader.ReadString('\n')
|
ipInput, _ := reader.ReadString('\n')
|
||||||
|
@ -293,46 +234,19 @@ func main() {
|
||||||
fmt.Println("started at:", time.Now())
|
fmt.Println("started at:", time.Now())
|
||||||
fmt.Println("--------------------------------------------------")
|
fmt.Println("--------------------------------------------------")
|
||||||
|
|
||||||
pingCmd := []string{}
|
clearScreen()
|
||||||
switch runtime.GOOS {
|
for idx, ip := range ipList {
|
||||||
case "windows":
|
ipLineMap[ip] = idx + 1
|
||||||
pingCmd = []string{"ping", "-n", "1", "-w", "1000"}
|
printStatusLine(ip, "waiting to scan...")
|
||||||
case "darwin":
|
|
||||||
pingCmd = []string{"ping", "-c", "1"}
|
|
||||||
default:
|
|
||||||
pingCmd = []string{"ping", "-c", "1", "-W", "1"}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("[~] pinging IPs...")
|
var wg sync.WaitGroup
|
||||||
var alive []string
|
|
||||||
for i := 0; i < len(ipList); i += batchSize {
|
for i := 0; i < len(ipList); i += batchSize {
|
||||||
end := i + batchSize
|
end := i + batchSize
|
||||||
if end > len(ipList) {
|
if end > len(ipList) {
|
||||||
end = len(ipList)
|
end = len(ipList)
|
||||||
}
|
}
|
||||||
batch := ipList[i:end]
|
batch := ipList[i:end]
|
||||||
fmt.Printf("[~] pinging IPs %d to %d...\n", i+1, end)
|
|
||||||
batchAlive := pingIPsConcurrently(batch, pingCmd)
|
|
||||||
alive = append(alive, batchAlive...)
|
|
||||||
}
|
|
||||||
if len(alive) == 0 {
|
|
||||||
fmt.Println("[-] no IPs responded to ping. exiting.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
clearScreen()
|
|
||||||
for idx, ip := range alive {
|
|
||||||
ipLineMap[ip] = idx + 1
|
|
||||||
printStatusLine(ip, "waiting to scan...")
|
|
||||||
}
|
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for i := 0; i < len(alive); i += batchSize {
|
|
||||||
end := i + batchSize
|
|
||||||
if end > len(alive) {
|
|
||||||
end = len(alive)
|
|
||||||
}
|
|
||||||
batch := alive[i:end]
|
|
||||||
for _, ip := range batch {
|
for _, ip := range batch {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func(ip string) {
|
go func(ip string) {
|
||||||
|
@ -349,24 +263,10 @@ func main() {
|
||||||
fmt.Println("scan done at:", time.Now())
|
fmt.Println("scan done at:", time.Now())
|
||||||
fmt.Println("--------------------------------------------------")
|
fmt.Println("--------------------------------------------------")
|
||||||
|
|
||||||
file, err := os.Create("summary.txt")
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("[!] failed to write summary:", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
if len(openPorts) == 0 {
|
if len(openPorts) == 0 {
|
||||||
file.WriteString("no open ports found.\n")
|
summaryFile.WriteString("no open ports found.\n")
|
||||||
} else {
|
} else {
|
||||||
file.WriteString("[+] scan summary: open ports found with fingerprints\n")
|
summaryFile.WriteString("[+] scan summary: open ports found with fingerprints\n")
|
||||||
for _, p := range openPorts {
|
|
||||||
line := fmt.Sprintf(" - %s : port %d open - hostname: %s", p.IP, p.Port, p.Hostname)
|
|
||||||
if p.Fingerprint != "" {
|
|
||||||
line += " | " + p.Fingerprint
|
|
||||||
}
|
|
||||||
file.WriteString(line + "\n")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fmt.Println("[+] scan summary written to summary.txt")
|
fmt.Println("[+] scan summary written to summary.txt")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue