本公众号所发布的文章及工具只限交流学习,本公众号不承担任何责任!如有侵权,请告知我们立即删除。
往后会坚持更新这个系列的文章,写简明扼要的文章聊聊如何使用go
语言来开发安全工具,为自己所用。在看这些之前,需要对go
语言有一些了解。go
语言的语法和python
以及其他高级语言的语法有非常相似之处,因此如果有其他语言基础的话将会非常快地就能上手。关于基础语法的学习可以参考runoob
上面的教程:
https://www.runoob.com/go/go-tutorial.html
package mainimport (
"fmt"
"net"
)
func tcp_scanner() {
for port := 0; port < 65535; port++ {
address := fmt.Sprintf("192.168.44.132:%d", port)
connect, connect_error := net.Dial("tcp", address)
if connect_error != nil {
fmt.Printf("[×] %d --> 端口已关闭。\n", port)
continue
} else {
connect.Close()
fmt.Printf("[+] %d --> 端口开启!!!!!!\n", port)
}
}
}
func main() {
tcp_scanner()
}
运行结果为:
package mainimport (
"fmt"
"net"
"sync"
"time"
)
func tcp_scanner() {
var tcp_scanner_wait_group sync.WaitGroup
time_start := time.Now()
for port := 1; port < 65535; port++ {
tcp_scanner_wait_group.Add(1)
go func(fast_port int) {
defer tcp_scanner_wait_group.Done()
address := fmt.Sprintf("192.168.44.132:%d", fast_port)
connect, connect_error := net.Dial("tcp", address)
if connect_error != nil {
//fmt.Printf("[×] %s --> 关闭。\n", address)
return
} else {
connect.Close()
fmt.Printf("[+] %s --> 开启!!!!!!\n", address)
}
}(port)
}
tcp_scanner_wait_group.Wait()
time_stop := time.Now()
time_def := time_stop.Sub(time_start)
fmt.Printf("[*] 1-65535端口扫描完毕,共用时:%v。\n", time_def)
}
func main() {
tcp_scanner()
}
运行结果:
简单说就是找100个tcp_worker过来,并为他们分配工作。但是如果我们添加过多的worker,那么运行结果可能就不是那么可靠,因此我们需要寻求一个合理的值。
package mainimport (
"fmt"
"net"
"sort"
"time"
)
func tcp_worker(ports chan int, results chan int) {
for po := range ports {
address := fmt.Sprintf("192.168.44.132:%d", po)
connect, connect_error := net.Dial("tcp", address)
if connect_error != nil {
//fmt.Printf("[×] %s --> 关闭。\n", address)
results <- 0
return
} else {
connect.Close()
results <- po
}
}
}
func tcp_scanner() {
ports := make(chan int, 100)
results := make(chan int)
var open_ports []int
var close_ports []int
for worker := 0; worker < cap(ports); worker++ {
go tcp_worker(ports, results)
}
time_start := time.Now()
go func() {
//给这100个tcp_worker分配任务
for port := 1; port < 65535; port++ {
ports <- port
}
}()
for num := 1; num < 65535; num++ {
port := <-results
if port != 0 {
open_ports = append(open_ports, port)
} else {
close_ports = append(close_ports, port)
}
ports <- port
}
close(ports)
close(results)
time_stop := time.Now()
time_def := time_stop.Sub(time_start)
fmt.Printf("[*] 1-65535端口扫描完毕,共用时:%v。\n", time_def)
//端口排序
sort.Ints(open_ports)
//展示结果
for _, port := range open_ports {
fmt.Printf("[-] 192.168.44.132:%d端口开启!!!", port)
}
}
func main() {
tcp_scanner()
}