IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 网络协议 -> TCP扫描增强器实现65000端口,10S完成,快准狠(Go语言编程) -> 正文阅读

[网络协议]TCP扫描增强器实现65000端口,10S完成,快准狠(Go语言编程)

?

TCP扫描增强器

TCP扫描增强器实现原理,主要是使用TCP三次握手原理

TCP是比我们介绍的要复杂的多,但是我们只介绍一点基础知识。TCP的握手有三个过程。

首先,客户端发送一个 syn 的包,表示建立回话的开始。如果客户端收到超时,说明端口可能在防火墙后面,或者没有启用服务器

第二,如果服务端应答 syn-ack 包,意味着这个端口是打开的,否则会返回 rst 包。最后,客户端需要另外发送一个 ack 包。从这时起,连接就已经建立。

我们TCP扫描器第一步先实现单个端口的测试。使用标准库中的 net.Dial 函数,该函数接收两个参数:协议和测试地址(带端口号)。

版本一(单端口)

package main

import (
    "fmt"
    "net"
)

func main() {
	_, err := net.Dial("tcp", "www.baidu.com:80")
    if err == nil {
        fmt.Println("Connection successful")
    } else {
        fmt.Println(err)
    }
}

?

版本二(多端口)

为了不一个一个地测试每个端口,我们将添加一个简单的循环来简化整个测试过程。

package main

import (
    "fmt"
    "net"
)

func main() {
    for port := 80; port < 100; port++ {
        conn, err := net.Dial("tcp", fmt.Sprintf("www.baidu.com:%d", port))
        if err == nil {
            conn.Close()
            fmt.Println("Connection successful")
        } else {
            fmt.Println(err)
        }
    }
}

这种处理方式有个很大的问题,极度的慢。我们可以通过两个操作来处理一下:并行的执行及为每个连接添加超时控制。

我们来看下如何实现并行。第一步先把扫描功能拆分为一个独立函数。这样会使我们的代码看起来清晰。

版本三(并发执行)

我们会引入一个新的方法 WaitGroup ,详细用法信息可以参考标准库文档。在主函数中,我们可以拆分为协程去执行,然后等待执行结束

package main

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

func isOpen(host string, port int) bool {
	time.Sleep(time.Millisecond * 3)
	conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", host, port))
	if err == nil {
		_ = conn.Close()
		return true
	}

	return false
}

func main() {
	ports := []int{}
	wg := &sync.WaitGroup{}
	for port := 1; port < 50000; port++ {
		wg.Add(1)
		port := port
		go func() {
			opened := isOpen("www.baidu.com", port)
			if opened {
				ports = append(ports, port)
			}
			wg.Done()
		}()

	}
	wg.Wait()
	fmt.Printf("opened ports: %v\n", ports)
}

版本四(等待超时)

我们的代码已经执行的很快了,但是由于超时的原因,我们需要等待很久才能收到返回的错误信息。我们可以假设如果我们200毫秒内没有收到服务器的回应,就不再继续等待。

package main

import (
	"fmt"
	"net"
	"sync"
	"time"
)
func isOpen(host string, port int, timeout time.Duration) bool {
    time.Sleep(time.Millisecond * 1)
    conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), timeout)
    if err == nil {
        _ = conn.Close()
        return true
    }

    return false
}

func main() {
    ports := []int{}

    wg := &sync.WaitGroup{}
    timeout := time.Millisecond * 200
    for port := 1; port < 100; port++ {
        wg.Add(1)
        go func(p int) {
            opened := isOpen("www.baidu.com", p, timeout)
            if opened {
                ports = append(ports, p)
            }
            wg.Done()
        }(port)
    }

    wg.Wait()
    fmt.Printf("opened ports: %v\n", ports)
}

版本五(添加锁)

为什么要添加锁,因为并发执行的话,在往ports数组写的话,会有影响。

现在这个程序会有竞争条件。在只扫描少数端口时,速度比较慢,可能不会出现,但确实存在这个问题。所以我们需要使用 mutex 来修复它。

package main

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

var wg sync.WaitGroup
var mutex sync.Mutex

func isOpen(host string, port int, timeout time.Duration) bool {

	time.Sleep(time.Millisecond * 1)
	conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), timeout)
	if err == nil {
		_ = conn.Close()
		return true
	}

	return false
}

func main() {
	startTime := time.Now()
	ports := []int{}
	timeout := time.Millisecond * 500
	for port := 1; port <= 65000; port++ {
		go func(p int) {
			opened := isOpen("www.baidu.com", p, timeout)
			if opened {
				mutex.Lock()
				ports = append(ports, p)
				log.Printf("端口: %d 已经开通", p)
				mutex.Unlock()
			}
		}(port)
	}
	time.Since(startTime)
	cost := int(time.Since(startTime) / time.Second)
	fmt.Printf("opened ports: %v\n", ports)

	fmt.Printf("代码运行时长: %d S", cost)
}

版本六(并发控制)

为什么并发控制,不控制的话,在运行时会卡,有时间会导致竞争条件。会影响接口,为了数值的准确性,有必要控制一下并发数量

这里面并发控制,我采用channel,有兴趣可以谷歌一下。

golimit.go

package golimit

type GoLimit struct {
	ch chan int
}

func NewGoLimit(max int) *GoLimit {
	return &GoLimit{ch: make(chan int, max)}
}

func (g *GoLimit) Add() {
	g.ch <- 1
}

func (g *GoLimit) Done() {
	<-g.ch
}

tcp.go

package main

import (
	"code/Projects/tcp_Scanning/golimit"
	"fmt"
	"log"
	"net"
	"sync"
	"time"
)

var wg sync.WaitGroup
var mutex sync.Mutex

func isOpen(host string, port int, timeout time.Duration) bool {

	time.Sleep(time.Millisecond * 1)
	conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), timeout)
	if err == nil {
		_ = conn.Close()
		return true
	}

	return false
}

func main() {
	startTime := time.Now()
	ports := []int{}
	timeout := time.Millisecond * 500
	g := golimit.NewGoLimit(2000)
	for port := 1; port <= 65000; port++ {
		g.Add()
		go func(g *golimit.GoLimit, p int) {
			opened := isOpen("10.10.10.1", p, timeout)
			if opened {
				mutex.Lock()
				ports = append(ports, p)
				log.Printf("端口: %d 已经开通", p)
				mutex.Unlock()
			}
			g.Done()
		}(g, port)
	}
	time.Since(startTime)
	cost := int(time.Since(startTime) / time.Second)
	fmt.Printf("opened ports: %v\n", ports)

	fmt.Printf("代码运行时长: %d S", cost)
}

版本七(参数定制)

我们就得到了一个简单的端口扫描器。但有些不好的是,不能很方便的修改域名地址以及端口号范围,我们必须要重新编译代码才可以。Go还有一个很不错的包叫做 flag 。

flag 包可以帮助我们编写命令行程序。我们可以配置每个字符串或数字。我们为主机名及要测试的端口范围和连接超时添加参数。

package main

import (
	"code/Projects/tcp_Scanning/golimit"
	"flag"
	"fmt"
	"log"
	"net"
	"sync"
	"time"
)

var wg sync.WaitGroup
var mutex sync.Mutex

func isOpen(host string, port int, timeout time.Duration) bool {

	time.Sleep(time.Millisecond * 1)
	conn, err := net.DialTimeout("tcp", fmt.Sprintf("%s:%d", host, port), timeout)
	if err == nil {
		_ = conn.Close()
		return true
	}

	return false
}

func main() {
	startTime := time.Now()
	hostname := flag.String("hostname", "", "hostname to test")
	startPort := flag.Int("start-port", 80, "the port on which the scanning starts")
	endPort := flag.Int("end-port", 100, "the port from which the scanning ends")
	timeout := flag.Duration("timeout", time.Millisecond*200, "timeout")
	golimits := flag.Int("golimit", 1000, "the Program Concurrency")
	flag.Parse()
	ports := []int{}
	//timeout := time.Millisecond * 500
	g := golimit.NewGoLimit(*golimits)
	for port := *startPort; port <= *endPort; port++ {
		g.Add()
		go func(g *golimit.GoLimit, p int) {
			opened := isOpen(*hostname, p, *timeout)
			if opened {
				mutex.Lock()
				ports = append(ports, p)
				log.Printf("端口: %d 已经开通", p)
				mutex.Unlock()
			}
			g.Done()
		}(g, port)
	}
	time.Since(startTime)
	cost := int(time.Since(startTime) / time.Second)
	fmt.Printf("opened ports: %v\n", ports)

	fmt.Printf("代码运行时长: %d S", cost)
}

如果我们想要显示如何使用,我们可以添加一个 -h 参数,来显示使用说明。整个项目不到50行的代码,我们使用到了并行、flag 及 net 包。

速度测试还不错,

并发 2000,扫描65000端口,只需要16s,而且很准确。

如果设置4000的并发,扫描出来的结果可能缺少,只需要8S 。

?

?大家可以多试试。

  网络协议 最新文章
使用Easyswoole 搭建简单的Websoket服务
常见的数据通信方式有哪些?
Openssl 1024bit RSA算法---公私钥获取和处
HTTPS协议的密钥交换流程
《小白WEB安全入门》03. 漏洞篇
HttpRunner4.x 安装与使用
2021-07-04
手写RPC学习笔记
K8S高可用版本部署
mySQL计算IP地址范围
上一篇文章      下一篇文章      查看所有文章
加:2021-12-03 13:23:39  更:2021-12-03 13:24:24 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年7日历 -2024/7/6 8:23:28-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码