Error message here!

Hide Error message here!

忘记密码?

Error message here!

请输入正确邮箱

Hide Error message here!

密码丢失?请输入您的电子邮件地址。您将收到一个重设密码链接。

Error message here!

返回登录

Close

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

你且浅笑相安 2019-02-25 10:07:00 阅读数:362 评论数:0 点赞数:0 收藏数:0

本篇博客记录本次使用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本身的换行:rn;

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= truetelnetClientObj.UserName=usr telnetClientObj.Password=pass conn,err=telnetClientObj.Telnet(20)returnconn,err } func (this /*TelnetClient) Telnet(timeout int) (conn net.Conn, err error) {raddr := this.IP + ":" + this.Portconn, 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]bytelog.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] = 0xffbuf[1] = 0xfcbuf[2] = 0x25buf[3] = 0xffbuf[4] = 0xfebuf[5] = 0x01n, 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] = 0xffbuf[1] = 0xfebuf[2] = 0x03buf[3] = 0xffbuf[4] = 0xfcbuf[5] = 0x27n, 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 + "rn"))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+ "rn"))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] = 0xffbuf[1] = 0xfcbuf[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 TelnetClientstruct{ IPstringPortstringIsAuthenticationboolUserNamestringPasswordstring} func main() { conn,err:=telnet_Creat(host,usr,pass)if nil !=err {returnerr } 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 = truetelnetClientObj.UserName =usr telnetClientObj.Password =pass conn,err =telnetClientObj.Telnet(20) returnconn,err } func (this /*TelnetClient) Telnet(timeout int) (conn net.Conn, err error) {raddr := this.IP + ":" + this.Portconn, 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]bytelog.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] = 0xffbuf[1] = 0xfcbuf[2] = 0x25buf[3] = 0xffbuf[4] = 0xfebuf[5] = 0x01n, 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] = 0xffbuf[1] = 0xfebuf[2] = 0x03buf[3] = 0xffbuf[4] = 0xfcbuf[5] = 0x27n, 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 + "rn")) 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+ "rn")) 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] = 0xffbuf[1] = 0xfcbuf[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  一起学习交流;

版权声明
本文为[你且浅笑相安]所创,转载请带上原文链接,感谢
https://www.cnblogs.com/dpf-10/p/10429358.html

编程之旅,人生之路,不止于编程,还有诗和远方。
阅代码原理,看框架知识,学企业实践;
赏诗词,读日记,踏人生之路,观世界之行;

支付宝红包,每日可领