Advertisement
Harman5007

pcap

Feb 5th, 2021
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. import re
  2. import zlib
  3. import cv2
  4. from scapy.all import *
  5.  
  6. pictures_directory = "/home/justin/pic_carver/pictures"
  7. faces_directory= "/home/justin/pic_carver/faces"
  8. pcap_file= "bhp.pcap"
  9. def http_assembler(pcap_file):
  10. carved_images = 0
  11. faces_detected = 0
  12. a = rdpcap(pcap_file)
  13. sessions= a.sessions()
  14. for session in sessions:
  15. http_payload = ""
  16. for packet in sessions[session]:
  17. try:
  18. if packet[TCP].dport == 80 or packet[TCP].sport == 80:
  19. # reassemble the stream
  20. http_payload += str(packet[TCP].payload)
  21. except:
  22. pass
  23. headers = get_http_headers(http_payload)
  24. if headers is None:
  25. continue
  26. image,image_type = extract_image(headers,http_payload)
  27. if image is not None and image_type is not None:
  28. file_name = "%s-pic_carver_%d.%s" %(pcap_file,carved_images,image_type)
  29. fd = open("%s/%s" %(pictures_directory,file_name),"wb")
  30. fd.write(image)
  31. fd.close()
  32. carved_images += 1
  33. try:
  34. result = face_detect("%s/%s" %(pictures_directory,file_name),file_name)
  35. if result is True:
  36. faces_detected += 1
  37. except:
  38. pass
  39.  
  40. return carved_images, faces_detected
  41.  
  42. carved_images, faces_detected = http_assembler(pcap_file)
  43. print ("Extracted: %d images" % carved_images)
  44. print ("Detected: %d faces" % faces_detected)
  45.  
  46. def get_http_headers(http_payload):
  47. try:
  48. headers_raw = http_payload[:http_payload.index("\r\n\r\n")+2]
  49. # break out the headers
  50. headers = dict(re.findall(r"(?P<'name>.*?): (?P<value>.*?)\r\n",headers_raw))
  51. except:
  52. return None
  53. if "Content-Type" not in headers:
  54. return None
  55. return headers
  56. def extract_image(headers,http_payload):
  57. image= None
  58. image_type = None
  59. try:
  60. if "image" in headers['Content-Type']:
  61. image_type = headers['Content-Type'].split("/")[1]
  62. image = http_payload[http_payload.index("\r\n\r\n")+4:]
  63. # if we detect compression decompress the image
  64. try:
  65. if "Content-Encoding" in headers.keys():
  66. if headers['Content-Encoding'] == "gzip":
  67. image = zlib.decompress(image, 16+zlib.MAX_WBITS)
  68. elif headers['Content-Encoding'] == "deflate":
  69. image = zlib.decompress(image)
  70. except:
  71. pass
  72. except:
  73. return None,None
  74. return image,image_type
  75.  
  76.  
  77. def face_detect(path,file_name):
  78. img= cv2.imread(path)
  79. cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
  80. rect= cascade.detectMultiScale(img, 1.3, 4, cv2.cv.CV_HAAR_SCALE_IMAGE, (20,20))
  81. if len(rects) == 0:
  82. return False
  83. rects[:, 2:] += rects[:, :2]
  84. # highlight the faces in the image
  85. for x1,y1,x2,y2 in rects:
  86. cv2.rectangle(img,(x1,y1),(x2,y2),(127,255,0),2)
  87. cv2.imwrite("%s/%s-%s" % (faces_directory,pcap_file,file_name),img)
  88. return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement