/pythem/modules/pforensic.py

https://github.com/m4n3dw0lf/pythem · Python · 193 lines · 153 code · 21 blank · 19 comment · 49 complexity · de11669dc6bca163cb896516cbba1d3e MD5 · raw file

  1. #!/usr/bin/env python2.7
  2. # coding=UTF-8
  3. # Copyright (c) 2016-2018 Angelo Moura
  4. #
  5. # This file is part of the program pythem
  6. #
  7. # pythem is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU General Public License as
  9. # published by the Free Software Foundation; either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # General Public License for more details.
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  19. # USA
  20. from scapy.all import *
  21. import sys
  22. import os
  23. import termcolor
  24. from utils import color
  25. class PcapReader(object):
  26. name = "Simple pcap analyzer"
  27. desc = "Use some functions to analyze a pcap file"
  28. version = "0.3"
  29. obs = "need to filter for images and decode encoded gzip content"
  30. def __init__(self, file):
  31. try:
  32. self.file = file
  33. self.packets = rdpcap(file)
  34. except:
  35. if file == "pythem_module_test":
  36. return
  37. print "You probably forgot to set a file to be analyzed."
  38. def printHelp(self):
  39. print
  40. print color(" [ pythem - Forensic pcap reader ]", "grey")
  41. print
  42. print color(" FILE - [ {} ]".format(self.file), "red")
  43. print
  44. print
  45. print color("[*] help: Print the help message", "blue")
  46. print
  47. print
  48. print color("[*] clear: Clean the screen, same as GNU/Linux OS 'clear'", "blue")
  49. print
  50. print
  51. print color("[*] exit/quit: Return to pythem", "blue")
  52. print
  53. print
  54. print color("[*] show: Display all the packets and their index numbers.", "blue")
  55. print
  56. print
  57. print color(
  58. "[*] conversations: Display pictogram with conversations between hosts from the analyzed file.",
  59. "blue")
  60. print
  61. print
  62. print color("[*] filter <string/layer>: Run a custom filter in the packets.", "blue")
  63. print
  64. print
  65. print color("[*] packetdisplay [num]: Display the full content of index selected packet.", "blue")
  66. print
  67. print
  68. print color("[*] count : Display how much packets the .pcap file have.", "blue")
  69. def custom_filter(self, packets, filter):
  70. x = 0
  71. if filter == "string":
  72. from StringIO import StringIO
  73. try:
  74. find = raw_input("[+] String to search on packets (case sensitive): ")
  75. for p in packets:
  76. capture = StringIO()
  77. save_stdout = sys.stdout
  78. sys.stdout = capture
  79. p.show()
  80. sys.stdout = save_stdout
  81. pkt = "\r\n\n\n------------------------[Packet n:{}]------------------------\r\n".format(x)
  82. end = "\r\n------------------------------------------------------------\r\n"
  83. if find in capture.getvalue():
  84. print pkt
  85. p.show()
  86. print end
  87. x += 1
  88. except KeyboardInterrupt:
  89. print "[-] User requested shutdown."
  90. except Exception as e:
  91. print "[!] Exception caught: {}".format(e)
  92. elif filter == "layer":
  93. try:
  94. find = raw_input("[+] Layer to search on packets (uppercase): ")
  95. for p in packets:
  96. pkt = "\r\n\n\n------------------------[Packet n:{}]------------------------\r\n".format(x)
  97. end = "\r\n------------------------------------------------------------\r\n"
  98. if p.haslayer(find):
  99. print pkt
  100. p.show()
  101. print end
  102. x += 1
  103. except KeyboardInterrupt:
  104. print "[-] User requested shutdown."
  105. except Exception as e:
  106. print "[!] Exception caught: {}".format(e)
  107. else:
  108. print "[-] Select a valid filter: 'string' or 'layer'"
  109. return
  110. def start(self):
  111. while True:
  112. try:
  113. console = termcolor.colored("pforensic>", "yellow", attrs=["bold"])
  114. self.command = raw_input("{} ".format(console))
  115. self.argv = self.command.split()
  116. self.input_list = [str(a) for a in self.argv]
  117. try:
  118. if self.input_list[0] == 'packetdisplay':
  119. try:
  120. self.packets[int(self.input_list[1])].show()
  121. except Exception as e:
  122. print "[!] Exception caught: {}".format(e)
  123. elif self.input_list[0] == "filter":
  124. try:
  125. self.filter = self.input_list[1]
  126. self.custom_filter(self.packets, self.filter)
  127. except IndexError:
  128. print "[!] Select a option to filter, string or layer"
  129. except Exception as e:
  130. print "[!] Exception caught: {}".format(e)
  131. elif self.input_list[0] == 'packetload':
  132. try:
  133. print "[+] Packet {} payload: ".format(self.input_list[1])
  134. self.filter_lookup(self.packets[int(self.input_list[1])])
  135. except Exception as e:
  136. print "[!] Exception caught: {}".format(e)
  137. elif self.input_list[0] == 'exit':
  138. break
  139. elif self.input_list[0] == 'quit':
  140. break
  141. elif self.input_list[0] == 'help':
  142. self.printHelp()
  143. elif self.input_list[0] == 'clear':
  144. os.system('clear')
  145. elif self.input_list[0] == 'ls':
  146. os.system('ls')
  147. elif self.input_list[0] == 'summary':
  148. try:
  149. self.packets.summary()
  150. except Exception as e:
  151. print "[!] Exception caught: {}".format(e)
  152. elif self.input_list[0] == 'show':
  153. try:
  154. self.packets.show()
  155. except Exception as e:
  156. print "[!] Exception caught: {}".format(e)
  157. elif self.input_list[0] == 'count':
  158. try:
  159. print "[+] Number of packets: {}".format(len(self.packets))
  160. except Exception as e:
  161. print "[!] Exception caught: {}".format(e)
  162. elif self.input_list[0] == 'conversations':
  163. try:
  164. self.packets.conversations()
  165. except Exception as e:
  166. print "[!] Exception caught: {}".format(e)
  167. else:
  168. print "[-] Select a valid option."
  169. except IndexError:
  170. pass
  171. except KeyboardInterrupt:
  172. break