Simple Port Scanner with Golang

Use Go‘s net package: net.Dial(network, address string)

package main

import (
"fmt"
"net"
) func main() {
_, err := net.Dial("tcp", "scanme.nmap.org:80")
if err == nil {
fmt.Println("Connection successful")
}

Nonconcurrent Scanning

Scan TCP ports range from 1 to  2014 slowly.

package main

import (
"fmt"
"net"
) func main() { for i := 1; i <=1024; i ++ {
address := fmt.Sprintf("scame.nmap.org:%d", i)
conn, err := net.Dial("tcp", address)
if err != nil {
// port is closed or filtered.
continue
} conn.Close()
fmt.Printf("%d open\n", i)
}
}

 Concurrent scanning

Harness the power of goroutines to scan multiple ports concurrently.

package main

import (
"fmt"
"net"
) func main() {
for i := 1; i <= 1024; i++ {
go func(j int) {
address := fmt.Sprintf("scanme.nmap.org:%d", j)
conn, err := net.Dial("tcp", address)
if err != nil {
return
}
conn.Close()
fmt.Printf("%d open\n", j)
}(i)
}
}

But it is too fast to wait for the connection to take place, so you may not get accurate results for ports whose packets were still in-flight.

Method 1:

Synchronized Scanning Using WaitGroup

Use WaitGroup from the sync package to control concurrency in a thread-safe way.

package main

import (
"fmt"
"net"
"sync"
) func main() {
var wg sync.WaitGroup
for i := 1; i <= 1024; i++ {
wg.Add(1)
go func(j int) {
defer wg.Done()
address := fmt.Sprintf("10.0.0.24:%d", j)
conn, err := net.Dial("tcp", address)
if err != nil {
return
}
conn.Close()
fmt.Printf("%d open\n", j)
}(i)
}
wg.Wait()
}

This program is better, but still incorrect.

 Port Scanning Using a Worker Pool

To avoid inconsistencies, use a pool of goroutines to manage the concurrent work being performed.

package main

import (
"fmt"
"sync"
) func worker(ports chan int, wg *sync.WaitGroup) {
for p := range ports {
fmt.Println(p)
wg.Done()
}
} func main() {
ports := make(chan int, 100)
var wg sync.WaitGroup
for i := 0; i < cap(ports); i++ {
go worker(ports, &wg)
}
for i := 1; i <= 1024; i++ {
wg.Add(1)
ports <- i
}
wg.Wait()
close(ports)
}

The numbers printed to the screen in no particular order.

Method 2: 

Multichannel Communication

Remove the dependency of a WaitGroup

package main

import (
"fmt"
"net"
"sort"
) func worker(ports, results chan int) {
for p:= range ports {
address := fmt.Sprintf("10.0.0.24:%d", p)
conn, err := net.Dial("tcp", address)
if err != nil {
results <-0
continue
}
conn.Close()
results <- p
}
} func main() {
ports := make(chan int, 100)
results := make(chan int)
var openports []int for i := 0; i < cap(ports); i++ {
go worker(ports, results)
} go func() {
for i := 1; i <= 1024; i++ {
ports <- i
}
}() for i := 0; i < 1024; i++ {
port := <-results
if port != 0 {
openports = append(openports, port)
}
} close(ports)
close(results)
sort.Ints(openports)
for _, port := range openports {
fmt.Printf("%d open\n", port)
}
}

A highly efficient port scanner.

You can modify the program to allow users to provide the number of workers as an option.

Go Pentester - TCP Scanner的更多相关文章

  1. Go Pentester - TCP Proxy

    Building a TCP Proxy Using io.Reader and io.Writer Essentially all input/output(I/O). package main i ...

  2. Metasploit辅助模块

    msf > show auxiliary Auxiliary ========= Name                                                  Di ...

  3. allVncClients

    VNC Viewer Free Edition 37  RealVNC Ltd.  15,367  Freeware  1021.58 KB VNC is client and server remo ...

  4. metasploit快速入门

    今天没上班,在小黑屋里看了一个一百多页的书<metasploit新手指南>,在此将笔记分享给大家.欢迎大家批评指正,共同学习进步.     metasploit新手指南 笔记 kali 0 ...

  5. 辅助模块应用(auxiliary/scanner/portscan/tcp)

    实验步骤 创建msf所需的数据库 之前我们开启msf时下面总会出现一个红色的小减号,原来是因为没有和数据库键连接,于是首先我们要手动建立一个数据库... 使用命令来实现: service postgr ...

  6. Python3实现TCP端口扫描

    在渗透测试的初步阶段通常我们都需要对攻击目标进行信息搜集,而端口扫描就是信息搜集中至关重要的一个步骤.通过端口扫描我们可以了解到目标主机都开放了哪些服务,甚至能根据服务猜测可能存在某些漏洞. TCP端 ...

  7. Python3实现TCP端口扫描器

    本文来自 高海峰对 玄魂工作室 的投稿 作者:高海峰 QQ:543589796 在渗透测试的初步阶段通常我们都需要对攻击目标进行信息搜集,而端口扫描就是信息搜集中至关重要的一个步骤.通过端口扫描我们可 ...

  8. TCP/UDP端口列表

    http://zh.wikipedia.org/wiki/TCP/UDP%E7%AB%AF%E5%8F%A3%E5%88%97%E8%A1%A8 TCP/UDP端口列表     本条目可通过翻译外语维 ...

  9. [JAVA] 基于TCP的起重机运行模拟器

    1.客户端 package ClientMain; import java.io.DataInputStream; import java.io.DataOutputStream; import ja ...

随机推荐

  1. SQL中游标的使用示例

    declare @email_source varchar(MAX); --1.原始发件人字段 declare @key_name varchar(50); --2.我方卷号或客户代码 declare ...

  2. Java中String创建原理深入分析

    创建String对象的常用方式: 1.  使用new关键字 String s1 = new String(“ab”);  // 2.  使用字符串常量直接赋值 String s2 = “abc”; 3 ...

  3. Apollo配置中心的实战

    31.携程 Apollo 配置中心介绍~1.mp4 32.Apollo核心概念~1.mp4 32.Apollo核心概念~1.mp4 每个应用需要有一个唯一的AppID 要在指定的机器上的server. ...

  4. MongoDB via Dotnet Core数据映射详解

    用好数据映射,MongoDB via Dotnet Core开发变会成一件超级快乐的事.   一.前言 MongoDB这几年已经成为NoSQL的头部数据库. 由于MongoDB free schema ...

  5. pikachu靶场-XSS

    .Tips: 一般查询接口容易出现反射型XSS,留言板容易出现存储型XSS 由于后台可能存在过滤措施,构造的script可能会被过滤掉,而无法生效,或者环境限制了执行(浏览器): 通过变化不同的scr ...

  6. openstack Rocky 社区版部署1.3 安装OpenStack packages

    1 installing the Rocky release on all nodes. yum install centos-release-openstack-rocky 安装之后,会在/etc/ ...

  7. Springboot--元注解及自定义注解(表单验证)

    本文简单说明一下元注解,然后对元注解中的@Retention做深入的讨论,在文章最后使用元注解写一个自定义注解来结尾. 一.结论: @Target:注解的作用目标 @Target(ElementTyp ...

  8. JavaScript手写new方法

    1.看一下正常使用的new方法 function father(name){ this.name=name; this.sayname=function(){ console.log(this.nam ...

  9. 超简单集成HMS Scan Kit扫码SDK,轻松实现扫码购

    前言   在前面的文章中,我们向大家介绍了HMS Scan Kit 的快速集成方法以及HMS Scan Kit和其他开源扫码工具的竞争力对比分析,如果没有看到也没关系,文章下方的往期链接中有文章入口. ...

  10. 记一次解密wireshark抓取的冰蝎通信流量

    一.关于冰蝎 1.1 简单介绍 冰蝎是一款基于Java开发的动态加密通信流量的新型Webshell客户端.老牌 Webshell 管理神器——中国菜刀的攻击流量特征明显,容易被各类安全设备检测,实际场 ...