ufoscan.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-"
  3. """
  4. This file is part of the UFONet project, https://ufonet.03c8.net
  5. Copyright (c) 2013/2024 | psy <epsylon@riseup.net>
  6. You should have received a copy of the GNU General Public License along
  7. with UFONet; if not, write to the Free Software Foundation, Inc., 51
  8. Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  9. """
  10. import sys, time, random
  11. from urllib.parse import urlparse as urlparse
  12. try:
  13. from scapy import *
  14. except:
  15. print("\nError importing: scapy lib.\n")
  16. sys.exit(2)
  17. # UFONet port scanner (UFOSCAN) class
  18. def randInt():
  19. x = random.randint(1,65535) # TCP ports
  20. return x
  21. def scan(self, ip, port, openp, closed):
  22. src_port = RandShort()
  23. seq = randInt()
  24. window = randInt()
  25. p = IP(dst=ip)/TCP(sport=src_port, dport=port, seq=seq, window=window, flags='S')
  26. resp = sr1(p, timeout=2)
  27. if str(type(resp)) == "<type 'NoneType'>":
  28. closed = closed + 1
  29. elif resp.haslayer(TCP):
  30. if resp.getlayer(TCP).flags == 0x12:
  31. send_rst = sr(IP(dst=ip)/TCP(sport=src_port, dport=port, flags='AR'), timeout=1)
  32. openp.append(port) # open port found!
  33. print("\n" + "="*54)
  34. print("[Info] [AI] [UFOSCAN] OPEN port found! [ " + str(port) + " ]")
  35. print("="*54 + "\n")
  36. elif resp.getlayer(TCP).flags == 0x14:
  37. closed = closed + 1
  38. return openp, closed
  39. def is_up(ip):
  40. p = IP(dst=ip)/ICMP()
  41. resp = sr1(p, timeout=10)
  42. if resp == None:
  43. return False
  44. elif resp.haslayer(ICMP):
  45. return True
  46. class UFOSCAN(object):
  47. def scanning(self, target, portX, portY):
  48. print("[Info] [AI] [UFOSCAN] Emitting X-Ray into range: [ "+str(portX)+"-"+str(portY)+" ]\n")
  49. print("="*74, "\n")
  50. if target.startswith('http://'):
  51. target = target.replace('http://','')
  52. elif target.startswith('https://'):
  53. target = target.replace('https://','')
  54. else:
  55. print("[Error] [AI] [UFOSCAN] Target url not valid ("+target+")! -> It should starts with 'http(s)://'\n")
  56. return
  57. try:
  58. ip = socket.gethostbyname(target)
  59. except:
  60. try:
  61. import dns.resolver
  62. r = dns.resolver.Resolver()
  63. r.nameservers = ['8.8.8.8', '8.8.4.4'] # google DNS resolvers
  64. url = urlparse(target)
  65. a = r.query(url.netloc, "A") # A record
  66. for rd in a:
  67. ip = str(rd)
  68. except:
  69. ip = target
  70. if ip == "127.0.0.1" or ip == "localhost":
  71. print("[Info] [AI] [UFOSCAN] Sending message '1/0 %====D 2 Ur ;-0' to 'localhost' -> [OK!]\n")
  72. return
  73. start_time = time.time()
  74. try:
  75. ports = list(range(int(portX), int(portY+1)))
  76. except:
  77. portX = 1
  78. portY = 1024 # 1024
  79. print("[Info] [AI] [UFOSCAN] Not any range of ports selected. Using by default: [ 1-1024 ]\n")
  80. ports = list(range(int(portX), int(portY+1)))
  81. portX = str(portX)
  82. portY = str(portY+1)
  83. if is_up(ip):
  84. openp = []
  85. closed = 0
  86. print("\n" + "="*50)
  87. print("[Info] [AI] [UFOSCAN] Host %s is UP!" % ip)
  88. print("="*50)
  89. print("\n"+"-"*22)
  90. for port in ports:
  91. try:
  92. openp, closed = scan(self, ip, port, openp, closed)
  93. except:
  94. pass
  95. duration = time.time()-start_time
  96. print("="*70)
  97. print("[Info] [AI] [UFOSCAN] Scan completed in: [ %s ]" % duration)
  98. print("="*70)
  99. if closed == len(ports):
  100. print("\n[Info] [AI] [UFOSCAN] [ %s/%d ] CLOSED ports. -> [Exiting!]\n" % (closed, len(ports)-1))
  101. else:
  102. print("\n[Info] [AI] [UFOSCAN] [ %s/%d ] OPEN ports FOUND!\n" % (len(openp), len(ports)-1))
  103. print("[+] TARGET: [ " + str(ip) + " ]\n")
  104. for o in openp:
  105. print(" [-] OPEN PORT: [ " + str(o) + " ]")
  106. print("")
  107. else:
  108. duration = time.time()-start_time
  109. print("-"*22)
  110. print("="*50, "\n")
  111. print("[Info] [AI] [UFOSCAN] Host %s is DOWN!" % ip)
  112. print("\n[Info] [AI] [UFOSCAN] Scan completed in: [ %s ]" % duration + "\n")