//GO語(yǔ)言 實(shí)現(xiàn)端口掃描
//缺陷
//port 無(wú)法設(shè)置成全局變量不知道怎么設(shè)置的
//var l = list.New() 這個(gè)是數(shù)組操作并不是消息隊(duì)列 跟消息隊(duì)列功能類似
//實(shí)現(xiàn)功能
//實(shí)現(xiàn)生成 IP段
//實(shí)現(xiàn)端口掃描
//實(shí)現(xiàn)參數(shù)傳入
//寫(xiě)入文件到本地
//main.go 58.215.20.30 58.215.201.30 80
//文件名 開(kāi)始IP 結(jié)束IP 掃描端口
//QQ29295842 希望認(rèn)識(shí)更多的朋友技術(shù)交流
//QQ群367196336 go golang WEB安全開(kāi)發(fā)
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_int24 | ip2_str_int16 | ip3_str_int8 | 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))
// 入隊(duì), 壓棧
ip_data := num2ip(index)
//fmt.Println(ip_data)
l.PushBack(ip_data)
index++
}
}
func text_add(name string, data string) { //向文件中寫(xiě)入數(shù)據(jù) 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) //轉(zhuǎn)換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 { //死循環(huán)
if l.Len() = 0 {
fmt.Println("跳出循環(huán)")
break //#跳出
}
// 出隊(duì) 從前讀取
i1 := l.Front()
l.Remove(i1)
IP, _ := i1.Value.(string)
if socket_ip(IP, port) {
//OK
//獲取當(dāng)前 日期作為文件名 在把IP寫(xiě)入進(jìn)去
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) //納秒為單位
}
}
}