【原创】使用golang访问windows telnet服务器

本篇博客记录本次使用golang语言tcp方式进行telnet服务器访问

环境:

1、win7系统telnet服务器,使用地址:192.168.8.189

2、python使用telnetlib库对telnet服务器进行访问的客户端例程

3、服务端用户名:admin  密码:123456

本次功能实现过程:

第一步:抓包获取telnet交互流程分析

1、打开cmd。输入:telnet 192.168.8.189      访问 telnet服务器,依次输入用户名密码,登录成功后输入:exit 退出;

2、打开wireshark对上述流程进行抓包;

流查看抓包内容如下:

转储16进制查看:

 

内容分析结果:

1、回车换行内容为:0d 0a     也就是window本身的换行:\r\n;

2、回显内容包含大量屏幕坐标信息,基本无法直接使用;

 

看到该内容后想起以前使用python进行telnet服务器访问时,回显内容无屏幕坐标信息,遂找出以前写的python程序对访问信息进行抓包查看:

第二步:用使用telnetlib库编写的python程序对服务器进行访问,对交互流程抓包

转储16进制:

查看内容发现回显内容无屏幕坐标信息,对比两次交互内容,决定使用golang对方式二进行模拟,注意图中红色框内信息;

golang程序例程:

第一步:创建连接

func telnet_Creat(host string,usr string,pass string)  (conn net.Conn, err error) {
    telnetClientObj := new(TelnetClient)
    telnetClientObj.IP = host
    telnetClientObj.Port = "23"
    telnetClientObj.IsAuthentication = true
    telnetClientObj.UserName = usr
    telnetClientObj.Password = pass
    conn,err =telnetClientObj.Telnet(20)

    return conn,err
}
func (this *TelnetClient) Telnet(timeout int) (conn net.Conn, err error) {
raddr := this.IP + ":" + this.Port
conn, err = net.DialTimeout("tcp", raddr, time.Duration(timeout)*time.Second)
if nil != err {
log.Print("pkg: model, func: Telnet, method: net.DialTimeout, errInfo:", err)
return
}
if false == this.telnetProtocolHandshake(conn) {
log.Print("pkg: model, func: Telnet, method: this.telnetProtocolHandshake, errInfo: telnet protocol handshake failed!!!")
return
}
return
}
 

第二步:模拟登录(telnetProtocolHandshake)

func (this *TelnetClient) telnetProtocolHandshake(conn net.Conn) bool {
    var buf [4096]byte
    log.Print("telnetProtocolHandshake")
    n, err := conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    log.Print("1====",string(buf[0:n]))
    log.Printf("%x",(buf[0:n]))
    buf[0] = 0xff
    buf[1] = 0xfc
    buf[2] = 0x25
    buf[3] = 0xff
    buf[4] = 0xfe
    buf[5] = 0x01
    n, err = conn.Write(buf[0:6])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }

    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    log.Print("2====",string(buf[0:n]))
    buf[0] = 0xff
    buf[1] = 0xfe
    buf[2] = 0x03
    buf[3] = 0xff
    buf[4] = 0xfc
    buf[5] = 0x27
    n, err = conn.Write(buf[0:6])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }

    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    log.Print("3====",string(buf[0:n]))
    n, err = conn.Write([]byte(this.UserName + "\r\n"))
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }
    time.Sleep(time.Millisecond * 500)

    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    log.Print("4====",string(buf[0:n]))
    n, err = conn.Write([]byte(this.Password+ "\r\n"))
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }
    time.Sleep(time.Millisecond * 2000)
    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }

    log.Print("5====",string(buf[0:n]))

    buf[0] = 0xff
    buf[1] = 0xfc
    buf[2] = 0x18
    n, err = conn.Write(buf[0:3])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }
    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    log.Print("6====",string(buf[0:n]))

    return true
}

 

 

完整程序:

package tool

import (
    "log"
    "net"
    "time"
)

type TelnetClient struct {
    IP               string
    Port             string
    IsAuthentication bool
    UserName         string
    Password         string
}

func main() {
    conn,err:= telnet_Creat(host,usr,pass)
    if nil != err {
        return err

    }
    defer conn.Close()  
//后续对conn进行操作即可
}

func telnet_Creat(host string,usr string,pass string)  (conn net.Conn, err error) {
    telnetClientObj := new(TelnetClient)
    telnetClientObj.IP = host
    telnetClientObj.Port = "23"
    telnetClientObj.IsAuthentication = true
    telnetClientObj.UserName = usr
    telnetClientObj.Password = pass
    conn,err =telnetClientObj.Telnet(20)

    return conn,err
}
func (this *TelnetClient) Telnet(timeout int) (conn net.Conn, err error) {
raddr := this.IP + ":" + this.Port
conn, err = net.DialTimeout("tcp", raddr, time.Duration(timeout)*time.Second)
if nil != err {
log.Print("pkg: model, func: Telnet, method: net.DialTimeout, errInfo:", err)
return
}
if false == this.telnetProtocolHandshake(conn) {
log.Print("pkg: model, func: Telnet, method: this.telnetProtocolHandshake, errInfo: telnet protocol handshake failed!!!")
return
}
return
}
func (this *TelnetClient) telnetProtocolHandshake(conn net.Conn) bool {
    var buf [4096]byte
    log.Print("telnetProtocolHandshake")
    n, err := conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    log.Print("1====",string(buf[0:n]))
    log.Printf("%x",(buf[0:n]))
    buf[0] = 0xff
    buf[1] = 0xfc
    buf[2] = 0x25
    buf[3] = 0xff
    buf[4] = 0xfe
    buf[5] = 0x01
    n, err = conn.Write(buf[0:6])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }

    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    log.Print("2====",string(buf[0:n]))
    buf[0] = 0xff
    buf[1] = 0xfe
    buf[2] = 0x03
    buf[3] = 0xff
    buf[4] = 0xfc
    buf[5] = 0x27
    n, err = conn.Write(buf[0:6])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }

    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    log.Print("3====",string(buf[0:n]))
    n, err = conn.Write([]byte(this.UserName + "\r\n"))
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }
    time.Sleep(time.Millisecond * 500)

    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    log.Print("4====",string(buf[0:n]))
    n, err = conn.Write([]byte(this.Password+ "\r\n"))
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }
    time.Sleep(time.Millisecond * 2000)
    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }

    log.Print("5====",string(buf[0:n]))

    buf[0] = 0xff
    buf[1] = 0xfc
    buf[2] = 0x18
    n, err = conn.Write(buf[0:3])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Write, errInfo:", err)
        return false
    }
    n, err = conn.Read(buf[0:])
    if nil != err {
        log.Print("pkg: model, func: telnetProtocolHandshake, method: conn.Read, errInfo:", err)
        return false
    }
    log.Print("6====",string(buf[0:n]))

    return true
}

 

如有问题可添加VX :d11235812  一起学习交流;

posted @ 2019-02-25 10:07  你且浅笑相安  阅读(1250)  评论(0编辑  收藏  举报