Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. from scapy.all import *
  4.  
  5. #a = raw_input("Enter ip\n")
  6. #packet=IP(dst=a)/ICMP(type=8)
  7.  
  8. #res = sr(packet, timeout=10)
  9.  
  10. #if res[0]:
  11. # print 'Host is alive'
  12. #else:
  13. # print 'Host is not alive'
  14.  
  15. def ipFunction(ip):
  16. mas = ip.split('/')
  17. if len(mas) == 2:
  18. mas[1] = int(mas[1])
  19. if mas[1] <= 0:
  20. mas[1] = 1
  21. if mas[1] < 24:
  22. mas[1] = 24
  23. return mas[0], (2**(32-mas[1])-2)
  24. else:
  25. return mas[0], 1
  26.  
  27. class Interpreter(object):
  28. def __init__(self, text):
  29. self.text = text
  30. self.tokens = []
  31.  
  32. def error(self):
  33. raise Exception('Error parsing input')
  34.  
  35. def expr(self):
  36. self.tokens = self.text.split(" ")
  37. #return self.tokens
  38.  
  39. if self.tokens[0] == "discovery":
  40. if self.tokens[1] == "--icmp":
  41. ip, netMask = ipFunction(self.tokens[2])
  42. ipAddr = []
  43. i = 0
  44. while i < netMask:
  45. if netMask > 1:
  46. ipS = ip.split('.')
  47. ipS[3] = str(int(ipS[3])+1)
  48. ip='.'.join(ipS)
  49. packet=IP(dst=ip)/ICMP(type=8)
  50. res=sr(packet, timeout=1, verbose=0)
  51. if res[0]:
  52. print 'Host ' + ip + ' is alive'
  53. ipAddr.append(ip)
  54. else:
  55. print 'Host ' + ip + ' is not alive'
  56. i = i + 1
  57. return ipAddr
  58.  
  59. if self.tokens[1] == "--tcp":
  60. if self.tokens[2] == "--ports":
  61. ports = self.tokens[3].split(",")
  62. ip = self.tokens[4]
  63. for i in ports:
  64. packet=IP(dst=ip)/TCP(dport=int(i),flags="A")
  65. res=sr(packet, timeout=5, verbose=0)
  66. if res[0]:
  67. print 'Host '+ ip + ' is alive'
  68. return
  69. print "Host "+ ip+ " is not alive"
  70. else:
  71. port = 80
  72. ip = self.tokens[2]
  73. packet=IP(dst=ip)/TCP(dport=port,flags="A")
  74. res=sr(packet, timeout=5, verbose = 0)
  75. if res[0]:
  76. print 'Host '+ ip + ' is alive'
  77. print "Host "+ ip+ " is not alive"
  78.  
  79. if self.tokens[1] == "--udp":
  80. if self.tokens[2] == "--ports":
  81. ports = self.tokens[3].split(",")
  82. ip = self.tokens[4]
  83. for i in ports:
  84. packet=IP(dst=ip)/UDP(dport=int(i))
  85. res=sr(packet, timeout=5, verbose = 0)
  86. if res[0]:
  87. print 'Host '+ ip + ' is alive'
  88. return
  89. print "Host "+ ip+ " is not alive"
  90. else:
  91. port = 40125
  92. ip = self.tokens[2]
  93. packet=IP(dst=ip)/UDP(dport=port)
  94. res=sr(packet, timeout=10, verbose = 0)
  95. if res[0]:
  96. print 'Host '+ ip + ' is alive'
  97. return
  98. print "Host "+ ip+ " is not alive"
  99.  
  100. # if self.tokens[1] == "--arp":
  101. # ip = self.tokens[2]
  102. # packet=IP(dst=ip)/ARP()
  103. # res=sr(packet, timeout=5)
  104. # if res[0]:
  105. # return 'Host ' + ip + ' is alive'
  106. # return 'Host ' + ip + ' is not alive'
  107.  
  108. if self.tokens[1] == "--arp":
  109. ip, netMask = ipFunction(self.tokens[2])
  110. ipAddr = []
  111. i = 0
  112. while i < netMask:
  113. if netMask > 1:
  114. ipS = ip.split('.')
  115. ipS[3] = str(int(ipS[3])+1)
  116. ip='.'.join(ipS)
  117. packet=IP(dst=ip)/ARP()
  118. res=sr(packet, timeout=1, verbose = 0)
  119. if res[0]:
  120. print 'Host ' + ip + ' is alive'
  121. ipAddr.append(ip)
  122. else:
  123. print 'Host ' + ip + ' is not alive'
  124. i = i + 1
  125. return ipAddr
  126.  
  127. return self.error
  128.  
  129. def main():
  130. while True:
  131. try:
  132. text = raw_input('scapy_nmap> ')
  133. except EOFError:
  134. break
  135. if not text:
  136. continue
  137. interpreter = Interpreter(text)
  138. result = interpreter.expr()
  139. #print result
  140.  
  141. if __name__ == '__main__':
  142. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement