GO language realizes port scanning

//GO language implements port scanning 
//defects 
//port cannot be set as a global variable, I don’t know how to set it 
//var l = list.New() This is an array operation and not a message queue, which is similar to the message queue function 

//Implementation function 
//Implement IP segment generation 
//Implement port scanning 
//Implement parameter 
input 
//write file to local 
// main.go 58.215.20.30 58.215.201.30 80 //File name Start IP End IP scan port 
//QQ29295842 Hope 
Get to know more friends Technical exchange //QQ group 367196336 go golang WEB security development 
// blog http://hi.baidu.com/alalmn 
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 {// 
        Endless loop if l.Len() <= 0 { 
            fmt.Println("out Loop") 
            break //#Bounce out 
        } 
        // 
        Dequeue i1 from the previous read := l.Front() 
        l.Remove(i1) 
        IP, _ := i1.Value.(string) 
        if socket_ip(IP, port) { 
            //OK 
            //Get the current date as the file name and write the IP in 
            text_add(file_name+"_ok.txt", IP) 
        } //else { 
        // text_add(file_name+"_no.txt", IP) 
        //} 

        time .Sleep(time.Millisecond * 500) //Nanosecond as a unit 
    } 
} 

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) //纳秒为单位
        }

    }
}

Guess you like

Origin blog.csdn.net/benli8541/article/details/112707214
Recommended