Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/metasploit/framework/telnet/client.rb
Views: 11784
require 'metasploit/framework/tcp/client'12module Metasploit3module Framework4module Telnet5module Client6extend ActiveSupport::Concern7include Metasploit::Framework::Tcp::Client8include Msf::Auxiliary::Login910attr_accessor :banner1112#13# CONSTANTS14#15# Borrowing constants from Ruby's Net::Telnet class (ruby license)16IAC = 255.chr # "\377" # "\xff" # interpret as command17DONT = 254.chr # "\376" # "\xfe" # you are not to use option18DO = 253.chr # "\375" # "\xfd" # please, you use option19WONT = 252.chr # "\374" # "\xfc" # I won't use option20WILL = 251.chr # "\373" # "\xfb" # I will use option21SB = 250.chr # "\372" # "\xfa" # interpret as subnegotiation22GA = 249.chr # "\371" # "\xf9" # you may reverse the line23EL = 248.chr # "\370" # "\xf8" # erase the current line24EC = 247.chr # "\367" # "\xf7" # erase the current character25AYT = 246.chr # "\366" # "\xf6" # are you there26AO = 245.chr # "\365" # "\xf5" # abort output--but let prog finish27IP = 244.chr # "\364" # "\xf4" # interrupt process--permanently28BREAK = 243.chr # "\363" # "\xf3" # break29DM = 242.chr # "\362" # "\xf2" # data mark--for connect. cleaning30NOP = 241.chr # "\361" # "\xf1" # nop31SE = 240.chr # "\360" # "\xf0" # end sub negotiation32EOR = 239.chr # "\357" # "\xef" # end of record (transparent mode)33ABORT = 238.chr # "\356" # "\xee" # Abort process34SUSP = 237.chr # "\355" # "\xed" # Suspend process35EOF = 236.chr # "\354" # "\xec" # End of file36SYNCH = 242.chr # "\362" # "\xf2" # for telfunc calls3738OPT_BINARY = 0.chr # "\000" # "\x00" # Binary Transmission39OPT_ECHO = 1.chr # "\001" # "\x01" # Echo40OPT_RCP = 2.chr # "\002" # "\x02" # Reconnection41OPT_SGA = 3.chr # "\003" # "\x03" # Suppress Go Ahead42OPT_NAMS = 4.chr # "\004" # "\x04" # Approx Message Size Negotiation43OPT_STATUS = 5.chr # "\005" # "\x05" # Status44OPT_TM = 6.chr # "\006" # "\x06" # Timing Mark45OPT_RCTE = 7.chr # "\a" # "\x07" # Remote Controlled Trans and Echo46OPT_NAOL = 8.chr # "\010" # "\x08" # Output Line Width47OPT_NAOP = 9.chr # "\t" # "\x09" # Output Page Size48OPT_NAOCRD = 10.chr # "\n" # "\x0a" # Output Carriage-Return Disposition49OPT_NAOHTS = 11.chr # "\v" # "\x0b" # Output Horizontal Tab Stops50OPT_NAOHTD = 12.chr # "\f" # "\x0c" # Output Horizontal Tab Disposition51OPT_NAOFFD = 13.chr # "\r" # "\x0d" # Output Formfeed Disposition52OPT_NAOVTS = 14.chr # "\016" # "\x0e" # Output Vertical Tabstops53OPT_NAOVTD = 15.chr # "\017" # "\x0f" # Output Vertical Tab Disposition54OPT_NAOLFD = 16.chr # "\020" # "\x10" # Output Linefeed Disposition55OPT_XASCII = 17.chr # "\021" # "\x11" # Extended ASCII56OPT_LOGOUT = 18.chr # "\022" # "\x12" # Logout57OPT_BM = 19.chr # "\023" # "\x13" # Byte Macro58OPT_DET = 20.chr # "\024" # "\x14" # Data Entry Terminal59OPT_SUPDUP = 21.chr # "\025" # "\x15" # SUPDUP60OPT_SUPDUPOUTPUT = 22.chr # "\026" # "\x16" # SUPDUP Output61OPT_SNDLOC = 23.chr # "\027" # "\x17" # Send Location62OPT_TTYPE = 24.chr # "\030" # "\x18" # Terminal Type63OPT_EOR = 25.chr # "\031" # "\x19" # End of Record64OPT_TUID = 26.chr # "\032" # "\x1a" # TACACS User Identification65OPT_OUTMRK = 27.chr # "\e" # "\x1b" # Output Marking66OPT_TTYLOC = 28.chr # "\034" # "\x1c" # Terminal Location Number67OPT_3270REGIME = 29.chr # "\035" # "\x1d" # Telnet 3270 Regime68OPT_X3PAD = 30.chr # "\036" # "\x1e" # X.3 PAD69OPT_NAWS = 31.chr # "\037" # "\x1f" # Negotiate About Window Size70OPT_TSPEED = 32.chr # " " # "\x20" # Terminal Speed71OPT_LFLOW = 33.chr # "!" # "\x21" # Remote Flow Control72OPT_LINEMODE = 34.chr # "\"" # "\x22" # Linemode73OPT_XDISPLOC = 35.chr # "#" # "\x23" # X Display Location74OPT_OLD_ENVIRON = 36.chr # "$" # "\x24" # Environment Option75OPT_AUTHENTICATION = 37.chr # "%" # "\x25" # Authentication Option76OPT_ENCRYPT = 38.chr # "&" # "\x26" # Encryption Option77OPT_NEW_ENVIRON = 39.chr # "'" # "\x27" # New Environment Option78OPT_EXOPL = 255.chr # "\377" # "\xff" # Extended-Options-List7980#81# This method establishes an Telnet connection to host and port specified by82# the RHOST and RPORT options, respectively. After connecting, the banner83# message is read in and stored in the 'banner' attribute. This method has the84# benefit of handling telnet option negotiation.85#86def connect(global = true, verbose = true)87@trace = ''88@recvd = ''89fd = super(global)9091self.banner = ''92# Wait for a banner to arrive...93begin94Timeout.timeout(banner_timeout) do95while(true)96buff = recv(fd)97self.banner << buff if buff98if(self.banner =~ @login_regex or self.banner =~ @password_regex)99break100elsif self.banner =~ @busy_regex101# It's about to drop connection anyway -- seen on HP JetDirect telnet server102break103end104end105end106rescue ::Timeout::Error107end108109self.banner.strip!110111# Return the file descriptor to the caller112fd113end114115# Sometimes telnet servers start RSTing if you get them angry.116# This is a short term fix; the problem is that we don't know117# if it's going to reset forever, or just this time, or randomly.118# A better solution is to get the socket connect to try again119# with a little backoff.120def connect_reset_safe121begin122connect123rescue Rex::ConnectionRefused124return :refused125end126return :connected127end128129def recv(fd=self.sock, timeout=telnet_timeout)130recv_telnet(fd, timeout.to_f)131end132133#134# Handle telnet option negotiation135#136# Appends to the @recvd buffer which is used to tell us whether we're at a137# login prompt, a password prompt, or a working shell.138#139def recv_telnet(fd, timeout)140141data = ''142143begin144data = fd.get_once(-1, timeout)145return nil if not data or data.length == 0146147# combine CR+NULL into CR148data.gsub!(/#{CR}#{NULL}/no, CR)149150# combine EOL into "\n"151data.gsub!(/#{EOL}/no, "\n")152153data.gsub!(/#{IAC}(154[#{IAC}#{AO}#{AYT}#{DM}#{IP}#{NOP}]|[#{DO}#{DONT}#{WILL}#{WONT}]155[#{OPT_BINARY}-#{OPT_NEW_ENVIRON}#{OPT_EXOPL}]|#{SB}[^#{IAC}]*#{IAC}#{SE}156)/xno) do157m = $1158159if m == IAC160IAC161elsif m == AYT162fd.write("YES" + EOL)163''164elsif m[0,1] == DO165if(m[1,1] == OPT_BINARY)166fd.write(IAC + WILL + OPT_BINARY)167else168fd.write(IAC + WONT + m[1,1])169end170''171elsif m[0,1] == DONT172fd.write(IAC + WONT + m[1,1])173''174elsif m[0,1] == WILL175if m[1,1] == OPT_BINARY176fd.write(IAC + DO + OPT_BINARY)177# Disable Echo178elsif m[1,1] == OPT_ECHO179fd.write(IAC + DONT + OPT_ECHO)180elsif m[1,1] == OPT_SGA181fd.write(IAC + DO + OPT_SGA)182else183fd.write(IAC + DONT + m[1,1])184end185''186elsif m[0,1] == WONT187fd.write(IAC + DONT + m[1,1])188''189else190''191end192end193194@trace << data195@recvd << data196fd.flush197198rescue ::EOFError, ::Errno::EPIPE199end200201data202end203204#205# Wrappers for getters206#207208def banner_timeout209raise NotImplementedError210end211212def telnet_timeout213raise NotImplementedError214end215216end217end218end219end220221222