【安全开发】内网扫描器


前言

为什么要写这个?

  1. fscna被杀的概率太高(哪天二开一下免杀)。
  2. go还在学习的阶段正是写项目的时候,边写边学习go项目。
  3. 自己写的项目改起来更加方便。
  4. 实现功能暂时定为网段扫描和暴力破解和输出文档。

现实现的功能较少后序开发会逐步加入简单漏洞探探测和代理功能。

一、开发过程

项目已经打包上传至github:https://github.com/19xinan/Scanl

1.项目结构

项目结构非常简单,现在开发的基本功能包括主机存活探测、端口扫描、暴力破解功能其他功能后序开发。

scanl/
|-- main.go //程序入口,主函数包括命令格式和网段格式限制
|-- core/
|   |-- scanner.go// 扫描器,包括线程控制
|   |-- services.go//服务识别
|-- bruteforce/
|   |-- bruteforce.go//暴力破解模版
|-- pass.txt//暴力破解使用的密码文件

2.main.go

1.实现网段限制
2.实现网段存活探测
3.实现命令行参数限制
4.实现输出扫描结果文档

package main

import (
	"flag"
	"fmt"
	"net"
	"os"
	"sync"
	"time"

	"scanl/bruteforce"
	"scanl/core"
)

func main() {
	fmt.Println(`
  ██████  ▄████▄   ▄▄▄       ███▄    █  ██▓    
▒██    ▒ ▒██▀ ▀█  ▒████▄     ██ ▀█   █ ▓██▒    
░ ▓██▄   ▒▓█    ▄ ▒██  ▀█▄  ▓██  ▀█ ██▒▒██░    
  ▒   ██▒▒▓▓▄ ▄██▒░██▄▄▄▄██ ▓██▒  ▐▌██▒▒██░    
▒██████▒▒▒ ▓███▀ ░ ▓█   ▓██▒▒██░   ▓██░░██████▒
▒ ▒▓▒ ▒ ░░ ░▒ ▒  ░ ▒▒   ▓▒█░░ ▒░   ▒ ▒ ░ ▒░▓  ░
░ ░▒  ░ ░  ░  ▒     ▒   ▒▒ ░░ ░░   ░ ▒░░ ░ ▒  ░
░  ░  ░  ░          ░   ▒      ░   ░ ░   ░ ░   
      ░  ░ ░            ░  ░         ░     ░  ░
         ░
	`)

	// 解析命令行参数:-h网段、-all全端口、-t线程数、-pwd指定密码文件、-output指定输出文件名(不指定默认输出)
	subnet := flag.String("h", "", "Target subnet for scanning (e.g., 192.168.10.0/24)")
	allPorts := flag.Bool("all", false, "Scan all ports (0-65535)")
	threads := flag.Int("t", 100, "Number of concurrent threads")
	passwordFile := flag.String("pwd", "pass.txt", "Password file for bruteforce")
	outputFile := flag.String("output", "scan_results.txt", "Output file for scan results")
	flag.Parse()
	//检查网段
	if *subnet == "" {
		fmt.Println("Usage: ScanL.exe -h <target_subnet> [-all] [-t N] [-pwd pass.txt] [-output scan_results.txt]")
		os.Exit(1)
	}

	// 打开输出文件
	outputFileHandle, err := os.OpenFile(*outputFile, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
	if err != nil {
		fmt.Printf("Error opening output file: %v\n", err)
		os.Exit(1)
	}
	defer outputFileHandle.Close()

	// 解析网段
	ips, err := expandCIDR(*subnet)
	if err != nil {
		fmt.Fprintf(outputFileHandle, "Error parsing subnet: %v\n", err)
		os.Exit(1)
	}

	var wg sync.WaitGroup
	var mutex sync.Mutex
	var aliveHosts []string

	// 检测存活主机并输出到终端和文件
	for _, ip := range ips {
		wg.Add(1)
		go func(ip string) {
			defer wg.Done()
			if isHostAlive(ip) {
				mutex.Lock()
				aliveHosts = append(aliveHosts, ip)
				mutex.Unlock()
				fmt.Printf("Host %s is alive\n", ip)
				fmt.Fprintf(outputFileHandle, "Host %s is alive\n", ip)
			} else {
				fmt.Printf("Host %s is not alive\n", ip)
				fmt.Fprintf(outputFileHandle, "Host %s is not alive\n", ip)
			}
		}(ip)
	}

	wg.Wait()

	// 输出存活主机到文件
	fmt.Fprintln(outputFileHandle, "Alive hosts in subnet:")
	for _, ip := range aliveHosts {
		fmt.Fprintln(outputFileHandle, ip)
	}

	var ports []int
	if *allPorts {
		ports = make([]int, 65536)
		for i := 0; i <= 65535; i++ {
			ports[i] = i
		}
	} else {
		ports = []int{21, 22, 23, 25, 53, 80, 110, 119, 123, 143, 161, 194, 443, 445, 465, 587, 993, 995, 1433, 1521, 1723, 3306, 3389, 5900, 8080, 8443, 8888, 9090, 7001, 9999, 6379, 9200, 9300, 27017} // 精简端口列表
	}

	// 扫描主机并输出结果到终端和文件
	for _, ip := range aliveHosts {
		fmt.Fprintf(outputFileHandle, "Scanning host: %s\n", ip)
		fmt.Printf("Scanning host: %s\n", ip)
		results := core.ScanPorts(ip, ports, *threads)

		fmt.Fprintf(outputFileHandle, "Open ports on host %s:\n", ip)
		fmt.Printf("Open ports on host %s:\n", ip)
		for port, service := range results {
			if service != "Closed" {
				fmt.Fprintf(outputFileHandle, "Port %d: %s\n", port, service)
				fmt.Printf("Port %d: %s\n", port, service)
			}
		}

		// 默认启用暴力破解模块,针对开启了SSH或RDP的端口
		if service, found := results[22]; found && service == "SSH" {
			fmt.Fprintln(outputFileHandle, "Starting bruteforce attack on SSH...")
			fmt.Println("Starting bruteforce attack on SSH...")
			bruteforce.Bruteforce(ip, 22, *passwordFile)
		}
		//RDP实现有问题暂存
		//if service, found := results[3389]; found && service == "RDP" {
		//	fmt.Fprintln(outputFileHandle, "Starting bruteforce attack on RDP...")
		//	fmt.Println("Starting bruteforce attack on RDP...")
		//	bruteforce.Bruteforce(ip, 3389, *passwordFile)
		//}

		fmt.Fprintln(outputFileHandle, "---------------------------------------------")
		fmt.Println("---------------------------------------------")
	}

	fmt.Printf("Scan results saved to %s\n", *outputFile)
}

// expandCIDR 解析网段,生成所有 IP 地址
func expandCIDR(cidr string) ([]string, error) {
	ip, ipNet, err := net.ParseCIDR(cidr)
	if err != nil {
		return nil, err
	}

	var ips []string
	for ip := ip.Mask(ipNet.Mask); ipNet.Contains(ip); inc(ip) {
		ips = append(ips, ip.String())
	}

	// 排除网络地址和广播地址
	lenIPs := len(ips)
	switch {
	case lenIPs < 2:
		break
	case lenIPs > 2:
		ips = ips[1 : len(ips)-1]
	}

	return ips, nil
}

// IP地址递增
func inc(ip net.IP) {
	for j := len(ip) - 1; j >= 0; j-- {
		ip[j]++
		if ip[j] > 0 {
			break
		}
	}
}

// isHostAlive 检测主机是否存活
func isHostAlive(ip string) bool {
	timeout := 2 * time.Second
	conn, err := net.DialTimeout("ip4:icmp", ip, timeout)
	if err != nil {
		return false
	}

	defer conn.Close()
	return true
}

3.core模块

3.1 scanner.go

package core

import (
	"fmt"
	"net"
	"sync"
	"time"
)

// ScanPorts 扫描指定主机的指定端口,使用指定数量的并发线程
func ScanPorts(host string, ports []int, threads int) map[int]string {
	results := make(map[int]string)
	var mu sync.Mutex
	var wg sync.WaitGroup
	portChan := make(chan int, len(ports))

	// 启动指定数量的goroutines
	for i := 0; i < threads; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			for port := range portChan {
				service := scanPort(host, port)
				mu.Lock()
				results[port] = service
				mu.Unlock()
			}
		}()
	}

	// 将所有端口放入通道
	for _, port := range ports {
		portChan <- port
	}
	close(portChan)

	wg.Wait()
	return results
}

// scanPort 扫描单个端口
func scanPort(host string, port int) string {
	address := fmt.Sprintf("%s:%d", host, port)
	conn, err := net.DialTimeout("tcp", address, 1*time.Second)
	if err != nil {
		return "Closed"
	}
	defer func(conn net.Conn) {
		err := conn.Close()
		if err != nil {

		}
	}(conn)
	return identifyService(port)
}

3.2 service.go

package core

// identifyService 根据端口号识别服务
func identifyService(port int) string {
	services := map[int]string{
		21:    "FTP",
		22:    "SSH",
		23:    "Telnet",
		25:    "SMTP",
		53:    "DNS",
		80:    "HTTP",
		110:   "POP3",
		119:   "NNTP",
		123:   "NTP",
		143:   "IMAP",
		161:   "SNMP",
		194:   "IRC",
		443:   "HTTPS",
		445:   "SMB",
		465:   "SMTPS",
		587:   "Submission",
		993:   "IMAPS",
		995:   "POP3S",
		1433:  "MSSQL",
		1521:  "Oracle DB",
		1723:  "PPTP",
		3306:  "MySQL",
		3389:  "RDP",
		5900:  "VNC",
		8080:  "HTTP-Proxy",
		8443:  "HTTPS-Alt",
		8888:  "HTTP-Alt",
		9090:  "Weblogic",
		7001:  "Weblogic-Alt",
		9999:  "HTTP-Alt2",
		6379:  "Redis",
		9200:  "Elasticsearch",
		9300:  "Elasticsearch-Transport",
		27017: "MongoDB",
	}

	if service, found := services[port]; found {
		return service
	}
	return "Unknown"
}

4.bruteforc

这里少了rdp的爆破

4.1 bruteforce.go

package bruteforce

import (
	"bufio"
	"fmt"
	"os"
	"sync"
	"time"

	"golang.org/x/crypto/ssh"
)

// 默认账号列表
var defaultAccounts = []string{"root", "admin", "administrator"}

// Bruteforce 执行暴力破解攻击
func Bruteforce(host string, port int, passwordFile string) {
	passwords, err := readPasswords(passwordFile)
	if err != nil {
		fmt.Printf("Error reading password file: %v\n", err)
		return
	}

	var wg sync.WaitGroup
	wg.Add(len(defaultAccounts) * len(passwords))

	// 并发尝试不同的账号和密码组合
	for _, account := range defaultAccounts {
		for _, password := range passwords {
			go func(host string, port int, account string, password string) {
				defer wg.Done()
				fmt.Printf("Trying account: %s, password: %s\n", account, password)
				if sshLogin(host, port, account, password) {
					fmt.Printf("SSH login successful: %s:%s@%s\n", account, password, host)
				}
			}(host, port, account, password)
		}
	}

	wg.Wait()
}

// readPasswords 读取密码文件
func readPasswords(filePath string) ([]string, error) {
	file, err := os.Open(filePath)
	if err != nil {
		return nil, err
	}
	defer func(file *os.File) {
		err := file.Close()
		if err != nil {

		}
	}(file)

	var passwords []string
	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		passwords = append(passwords, scanner.Text())
	}

	if err := scanner.Err(); err != nil {
		return nil, err
	}

	return passwords, nil
}

// sshLogin 尝试使用SSH登录
func sshLogin(host string, port int, username, password string) bool {
	config := &ssh.ClientConfig{
		User: username,
		Auth: []ssh.AuthMethod{
			ssh.Password(password),
		},
		HostKeyCallback: ssh.InsecureIgnoreHostKey(),
		Timeout:         5 * time.Second,
	}

	conn, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, port), config)
	if err != nil {
		return false
	}
	defer func(conn *ssh.Client) {
		err := conn.Close()
		if err != nil {

		}
	}(conn)
	return true
}

二、使用步骤

ScanL.exe -h 192.168.10.1/24
其他参数
ScanL.exe -h <target_subnet> [-all] [-t N] [-pwd pass.txt] [-output scan_results.txt]

请添加图片描述

#网络资产信息扫描 在渗透测试(特别是内网)中经常需要对目标进行网络资产收集,即对方服务器都有哪些IP,IP上开了哪些端口,端口上运行着哪些服务,此脚本即为实现此过程,相比其他探测脚本有以下优点:1、轻巧简洁,只需python环境,无需安装额外外库。2、扫描完成后生成独立页面报告。 此脚本的大概流程为 ICMP存活探测-->端口开放探测-->端口指纹服务识别-->提取快照(若为WEB)-->生成结果报表 运行环境:python 2.6 + 参数说明 -h 必须输入的参数,支持ip(192.168.1.1),ip段(192.168.1),ip范围指定(192.168.1.1-192.168.1.254),ip列表文件(ip.ini),最多限制一次可扫描65535个IP。 -p 指定要扫描端口列表,多个端口使用,隔开 例如:22,23,80,3306。未指定即使用内置默认端口进行扫描(21,22,23,25,53,80,110,139,143,389,443,445,465,873,993,995,1080,1723,1433,1521,3306,3389,3690,5432,5800,5900,6379,7001,8000,8001,8080,8081,8888,9200,9300,9080,9999,11211,27017) -m 指定线程数量 默认100线程 -t 指定HTTP请求超时时间,默认为10秒,端口扫描超时为值的1/2。 -n 不进行存活探测(ICMP)直接进行扫描。 结果报告保存在当前目录(扫描IP-时间戳.html)。 例子: python NAScan.py -h 10.111.1 python NAScan.py -h 192.168.1.1-192.168.2.111 python NAScan.py -h 10.111.1.22 -p 80,7001,8080 -m 200 -t 6 python NAScan.py -h ip.ini -p port.ini -n 服务识别在server_info.ini文件中配置 格式为:服务名|默认端口|正则 例 ftp|21|^220.*?ftp|^220- 正则为空时则使用端口进行匹配,否则以正则匹配结果为准。 项目地址 https://github.com/ywolf/ 欢迎大家反馈建议和BUG
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

0x717866

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值