ip.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @Author: hywell
  5. @Email: hywell.28@gmail.com
  6. @Blog: iassas.com
  7. @Date: 2019/10/16 16:32
  8. """
  9. import re
  10. import socket
  11. import ipaddress
  12. from lib.core.data import logger
  13. from urllib.parse import urlparse
  14. def url2ip(url):
  15. ip = ''
  16. try:
  17. handel_url = urlparse(url).hostname
  18. ip = socket.gethostbyname(handel_url)
  19. except Exception as e:
  20. logger.warning('[AWIScan] %s can not get ip! Please verify', url)
  21. return ip
  22. # def c_ip(ip):
  23. # ip_list = []
  24. # ip_split = ip.split('.')
  25. # for c in range(1, 255):
  26. # ip = "%s.%s.%s.%d" % (ip_split[0], ip_split[1], ip_split[2], c)
  27. # ip_list.append(ip)
  28. # return ip_list
  29. def genIP(ip_range):
  30. """
  31. print (genIP('192.18.1.1-192.168.1.3'))
  32. ['192.168.1.1', '192.168.1.2', '192.168.1.3']
  33. """
  34. # from https://segmentfault.com/a/1190000010324211
  35. def num2ip(num):
  36. return '%s.%s.%s.%s' % ((num >> 24) & 0xff, (num >> 16) & 0xff, (num >> 8) & 0xff, (num & 0xff))
  37. def ip2num(ip):
  38. ips = [int(x) for x in ip.split('.')]
  39. return ips[0] << 24 | ips[1] << 16 | ips[2] << 8 | ips[3]
  40. start, end = [ip2num(x) for x in ip_range.split('-')]
  41. return [num2ip(num) for num in range(start, end + 1) if num & 0xff]
  42. def parseTarget(target):
  43. lists = []
  44. ipv4_re = re.compile(r'[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
  45. ipv4withmask_re = re.compile("^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|["
  46. "1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1["
  47. "0-9][0-9]|[1-9]?[0-9])/(3[0-2]|[1-2]?[0-9])$")
  48. ipv4range_re = re.compile("^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|["
  49. "1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1["
  50. "0-9][0-9]|[1-9]?[0-9])-(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4]["
  51. "0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.(25["
  52. "0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$")
  53. try:
  54. parsed_url = urlparse(target)
  55. if parsed_url.scheme == "http" or parsed_url.scheme == "https":
  56. # e.x http://10.1.1.1 https://10.1.1.1
  57. url2ip(target)
  58. target = {"ip": None, "domain": None, "url": "%s://%s" % (parsed_url.scheme, parsed_url.netloc)}
  59. lists.append(target)
  60. else:
  61. if ipv4withmask_re.search(parsed_url.path):
  62. # e.x 10.1.1.1/24
  63. network = list(ipaddress.ip_interface(target).network)
  64. for ip in network:
  65. target = {"ip": str(ip), "domain": None, "url": None}
  66. lists.append(target)
  67. elif ipv4range_re.search(target):
  68. # e.x 10.1.1.1-10.1.1.10
  69. ips = genIP(target)
  70. for ip in ips:
  71. target = {"ip": ip, "domain": None, "url": None}
  72. lists.append(target)
  73. else:
  74. if ipv4_re.search(target):
  75. # e.x 10.1.1.1
  76. target = {"ip": target, "domain": None, "url": None}
  77. lists.append(target)
  78. else:
  79. target = {"ip": None, "domain": target, "url": None}
  80. lists.append(target)
  81. except Exception as e:
  82. logger.error(e)
  83. return lists