johnmahugu

python picture carver

Jun 3rd, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.02 KB | None | 0 0
  1. import re
  2. import zlib
  3. import cv2
  4.  
  5. from scapy.all import *
  6.  
  7. pictures_directory = "pic_carver/pictures"
  8. faces_directory    = "pic_carver/faces"
  9. pcap_file          = "bhp.pcap"
  10.  
  11. def face_detect(path,file_name):
  12.  
  13.         img     = cv2.imread(path)
  14.         cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
  15.         rects   = cascade.detectMultiScale(img, 1.3, 4, cv2.cv.CV_HAAR_SCALE_IMAGE, (20,20))
  16.  
  17.         if len(rects) == 0:
  18.                 return False
  19.                
  20.         rects[:, 2:] += rects[:, :2]
  21.  
  22.     # highlight the faces in the image        
  23.     for x1,y1,x2,y2 in rects:
  24.         cv2.rectangle(img,(x1,y1),(x2,y2),(127,255,0),2)
  25.  
  26.     cv2.imwrite("%s/%s-%s" % (faces_directory,pcap_file,file_name),img)
  27.  
  28.         return True
  29.  
  30. def get_http_headers(http_payload):
  31.    
  32.     try:
  33.         # split the headers off if it is HTTP traffic
  34.         headers_raw = http_payload[:http_payload.index("\r\n\r\n")+2]
  35.    
  36.         # break out the headers
  37.         headers = dict(re.findall(r"(?P<name>.*?): (?P<value>.*?)\r\n", headers_raw))
  38.     except:
  39.         return None
  40.    
  41.     if "Content-Type" not in headers:
  42.         return None
  43.    
  44.     return headers
  45.  
  46. def extract_image(headers,http_payload):
  47.    
  48.     image      = None
  49.     image_type = None
  50.    
  51.     try:
  52.         if "image" in headers['Content-Type']:
  53.            
  54.             # grab the image type and image body
  55.             image_type = headers['Content-Type'].split("/")[1]
  56.        
  57.             image = http_payload[http_payload.index("\r\n\r\n")+4:]
  58.        
  59.             # if we detect compression decompress the image
  60.             try:
  61.                 if "Content-Encoding" in headers.keys():
  62.                     if headers['Content-Encoding'] == "gzip":
  63.                         image = zlib.decompress(image,16+zlib.MAX_WBITS)
  64.                     elif headers['Content-Encoding'] == "deflate":
  65.                         image = zlib.decompress(image)
  66.             except:
  67.                 pass   
  68.     except:
  69.         return None,None
  70.    
  71.     return image,image_type
  72.  
  73. def http_assembler(pcap_file):
  74.  
  75.     carved_images   = 0
  76.     faces_detected  = 0
  77.  
  78.     a = rdpcap(pcap_file)
  79.    
  80.     sessions      = a.sessions()   
  81.  
  82.     for session in sessions:
  83.  
  84.         http_payload = ""
  85.        
  86.         for packet in sessions[session]:
  87.    
  88.             try:
  89.                 if packet[TCP].dport == 80 or packet[TCP].sport == 80:
  90.    
  91.                     # reassemble the stream into a single buffer
  92.                     http_payload += str(packet[TCP].payload)
  93.    
  94.             except:
  95.                 pass
  96.    
  97.         headers = get_http_headers(http_payload)
  98.        
  99.         if headers is None:
  100.             continue
  101.    
  102.         image,image_type = extract_image(headers,http_payload)
  103.    
  104.         if image is not None and image_type is not None:               
  105.        
  106.             # store the image
  107.             file_name = "%s-pic_carver_%d.%s" % (pcap_file,carved_images,image_type)
  108.             fd = open("%s/%s" % (pictures_directory,file_name),"wb")
  109.             fd.write(image)
  110.             fd.close()
  111.            
  112.             carved_images += 1
  113.                    
  114.             # now attempt face detection
  115.             try:
  116.                 result = face_detect("%s/%s" % (pictures_directory,file_name),file_name)
  117.                
  118.                 if result is True:
  119.                     faces_detected += 1
  120.             except:
  121.                 pass
  122.            
  123.  
  124.     return carved_images, faces_detected
  125.  
  126.  
  127. carved_images, faces_detected = http_assembler(pcap_file)
  128.  
  129. print "Extracted: %d images" % carved_images
  130. print "Detected: %d faces" % faces_detected
Advertisement
Add Comment
Please, Sign In to add comment