GO语言实现的端口扫描器分享
//go语言 实现端口扫描
//缺陷
//port 无法设置成全局变量不知道怎么设置的
//var l = list.new() 这个是数组操作并不是消息队列 跟消息队列功能类似
//实现功能
//实现生成 ip段
//实现端口扫描
//实现参数传入
//写入文件到本地
//main.go 58.215.20.30 58.215.201.30 80
//文件名 开始ip 结束ip 扫描端口
//qq29295842 希望认识更多的朋友技术交流
//qq群367196336 go golang web安全开发
package main
import (
"container/list"
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
)
func ip2num(ip string) int {
cansplit := func(c rune) bool { return c == '.' }
lisit := strings.fieldsfunc(ip, cansplit) //[58 215 20 30]
//fmt.println(lisit)
ip1_str_int, _ := strconv.atoi(lisit[0])
ip2_str_int, _ := strconv.atoi(lisit[1])
ip3_str_int, _ := strconv.atoi(lisit[2])
ip4_str_int, _ := strconv.atoi(lisit[3])
return ip1_str_int<<24 | ip2_str_int<<16 | ip3_str_int<<8 | ip4_str_int
}
func num2ip(num int) string {
ip1_int := (num & 0xff000000) >> 24
ip2_int := (num & 0x00ff0000) >> 16
ip3_int := (num & 0x0000ff00) >> 8
ip4_int := num & 0x000000ff
//fmt.println(ip1_int)
data := fmt.sprintf("%d.%d.%d.%d", ip1_int, ip2_int, ip3_int, ip4_int)
return data
}
func gen_ip(aip1 int, aip2 int) {
index := aip1
for index < aip2 {
//fmt.println(num2ip(index))
// 入队, 压栈
ip_data := num2ip(index)
//fmt.println(ip_data)
l.pushback(ip_data)
index++
}
}
func text_add(name string, data string) { //向文件中写入数据 text_add("file2.txt", "qqqqqqqqqqqqqqqqqqqqqqq")
f, err := os.openfile(name, os.o_rdwr|os.o_create|os.o_append, 0x644)
if err != nil {
panic(err)
}
defer f.close()
_, err = f.writestring(data)
_, err = f.writestring("\r\n")
if err != nil {
panic(err)
}
}
//text_add("file2.txt", "qqqqqqqqqqqqqqqqqqqqqqq")
var l = list.new()
func socket_ip(host string, port string) bool {
var (
remote = host + ":" + port
)
tcpaddr, _ := net.resolvetcpaddr("tcp4", remote) //转换ip格式
//fmt.printf("%s", tcpaddr)
conn, err := net.dialtcp("tcp", nil, tcpaddr) //查看是否连接成功
if err != nil {
fmt.printf("no==%s:%s\r\n", host, port)
return false
}
defer conn.close()
fmt.printf("ok==%s:%s\r\n", host, port)
return true
}
func for_ip(port string) {
now := time.now()
year, mon, day := now.utc().date()
file_name := fmt.sprintf("%d-%d-%d_%s", year, mon, day, port)
for { //死循环
if l.len() <= 0 {
fmt.println("跳出循环")
break //#跳出
}
// 出队 从前读取
i1 := l.front()
l.remove(i1)
ip, _ := i1.value.(string)
if socket_ip(ip, port) {
//ok
//获取当前 日期作为文件名 在把ip写入进去
text_add(file_name+"_ok.txt", ip)
} //else {
// text_add(file_name+"_no.txt", ip)
// }
time.sleep(time.millisecond * 500) //纳秒为单位
}
}
func main() {
argslen := len(os.args)
//fmt.println(argslen)
if argslen != 4 {
fmt.println("main.go 58.215.20.30 58.215.201.30 80")
} else {
gen_ip(ip2num(os.args[1]), ip2num(os.args[2]))
for index := 0; index < 200; index++ {
go for_ip(os.args[3])
}
for {
time.sleep(1 * time.second) //纳秒为单位
}
}
}