【发布时间】:2018-04-28 23:20:27
【问题描述】:
我一直在学习 Golang,以便将我所有的渗透测试工具迁移到它。因为我喜欢编写自己的工具,所以这是学习一门新语言的完美方式。在这种特殊情况下,我认为我使用频道的方式有问题。我知道一个没有完成端口映射的事实,因为我在 ruby 上编写的其他工具正在查找所有打开的端口,但我的 golang 工具没有。有人可以帮我理解我做错了什么吗?渠道是正确的做法吗?
package main
import (
"fmt"
"log"
"net"
"strconv"
"time"
)
func portScan(TargetToScan string, PortStart int, PortEnd int, openPorts []int) []int {
activeThreads := 0
doneChannel := make(chan bool)
for port := PortStart; port <= PortEnd; port++ {
go grabBanner(TargetToScan, port, doneChannel)
activeThreads++
}
// Wait for all threads to finish
for activeThreads > 0 {
<-doneChannel
activeThreads--
}
return openPorts
}
func grabBanner(ip string, port int, doneChannel chan bool) {
connection, err := net.DialTimeout(
"tcp",
ip+":"+strconv.Itoa(port),
time.Second*10)
if err != nil {
doneChannel <- true
return
}
// append open port to slice
openPorts = append(openPorts, port)
fmt.Printf("+ Port %d: Open\n", port)
// See if server offers anything to read
buffer := make([]byte, 4096)
connection.SetReadDeadline(time.Now().Add(time.Second * 5))
// Set timeout
numBytesRead, err := connection.Read(buffer)
if err != nil {
doneChannel <- true
return
}
log.Printf("+ Banner of port %d\n%s\n", port,
buffer[0:numBytesRead])
// here we add to map port and banner
targetPorts[port] = string(buffer[0:numBytesRead])
doneChannel <- true
return
}
注意:似乎找到了第一批端口,但没有找到高于高数示例 8080 的端口,但它通常会得到 80 和 443... 所以我怀疑有些事情正在超时,或者发生了一些奇怪的事情。
有很多糟糕的代码 hack,主要是因为我正在学习和搜索很多关于如何做事的知识,所以请随时提供提示甚至更改/拉取请求。谢谢
【问题讨论】:
-
链接可以使用,但不是唯一的。链接可能不可靠。请将您的代码直接发布到问题中。
标签: go