Skip to content

Commit

Permalink
add a tool to extrat image from pcap file
Browse files Browse the repository at this point in the history
  • Loading branch information
zhbo committed Jul 17, 2017
1 parent 0c6e77f commit 83defc9
Show file tree
Hide file tree
Showing 21 changed files with 19 additions and 41 deletions.
Binary file removed security/web/arper.pcap
Binary file not shown.
58 changes: 18 additions & 40 deletions security/web/test.py → security/web/collect_imgs.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,12 @@
import re
import zlib
import cv2

from scapy.all import *
from scapy.layers.inet import TCP

pictures_directory = "/Users/river/project/security/dessert/security/web/pictures"
faces_directory = "/Users/river/project/security/dessert/security/web/faces"
pictures_directory = "./pic"
pcap_file = "arper.pcap"

def face_detect(path, file_name):
img = cv2.imread(path)
cascade = cv2.CascadeClassifier("haarcascade_frontalface_alt.xml")
rects = cascade.detectMultiScale(img, 1.3, 4, cv2.cv.CV_HAAR_SCALE_IMAGE, (20,20))

if len(rects) == 0:
return False

rects[:, 2:] += rects[:, :2]

# highlight the faces in the image
for x1, y1, x2, y2 in rects:
cv2.rectangle(img, (x1, y1), (x2, y2), (127, 255, 0), 2)

cv2.imwrite("%s/%s-%s" % (faces_directory, pcap_file, file_name), img)

return True

debug = False

def get_http_headers(http_payload):
headers = []
Expand All @@ -39,13 +19,15 @@ def get_http_headers(http_payload):
if end is -1:
return headers
headers_raw = http_payload[start:end+2]
print("start: "+str(start))
print("end: "+str(end))
print(headers_raw)
if debug:
print("start: "+str(start))
print("end: "+str(end))
print(headers_raw)

# break out the headers
header = dict(re.findall(r"(?P<name>.*?): (?P<value>.*?)\r\n", headers_raw))
print(header)
if debug:
print(header)
if "Content-Length" in header.keys():
len = int(header["Content-Length"])
payload = http_payload[end+4:end+4+len]
Expand Down Expand Up @@ -86,6 +68,9 @@ def extract_image(header, payload):

return image, image_type

def derepeat_packet(packetlist):
sortlist = sorted(packetlist, key=lambda packet: packet.id)
return sortlist

def http_assembler(pcap_file):
carved_images = 0
Expand All @@ -97,12 +82,14 @@ def http_assembler(pcap_file):

for session in sessions:
http_payload = ""
for packet in sessions[session]:
packetlist = derepeat_packet(sessions[session])
ids = []
for packet in packetlist:
try:
if packet[TCP].dport == 80 or packet[TCP].sport == 80:
if (packet[TCP].sport == 80) and not (packet.id in ids):
# reassemble the stream
http_payload += str(packet[TCP].payload)

ids.append(packet.id)
except:
pass

Expand All @@ -119,26 +106,17 @@ def http_assembler(pcap_file):
file_name = "%s-pic_carver_%d.%s" % \
(pcap_file, carved_images, image_type)

print(file_name)
fd = open("%s/%s" % (pictures_directory, file_name), "wb")

fd.write(image)
fd.close()

carved_images += 1

# now attempt face detection
try:
result = face_detect("%s/%s" % (pictures_directory, file_name), file_name)

if result is True:
faces_detected += 1
except:
pass
return carved_images, faces_detected

if __name__ == "__main__":
carved_images, faces_detected = http_assembler(pcap_file)

print "Extracted: %d images" % carved_images
print "Detected: %d faces" % faces_detected
print("Extracted: %d images" % carved_images)
print("Detected: %d faces" % faces_detected)
Binary file added security/web/pic/arper.pcap-pic_carver_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_1.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_10.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_11.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_13.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_14.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_15.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_16.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_17.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_2.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_3.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_4.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_5.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_6.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_7.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_8.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added security/web/pic/arper.pcap-pic_carver_9.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion security/web/pic_carver.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,4 @@ def http_assembler(pcap_file):
carved_images, faces_detected = http_assembler(pcap_file)

print "Extracted: %d images" % carved_images
print "Detected: %d faces" % faces_detected
print "Detected: %d faces" % faces_detected

0 comments on commit 83defc9

Please sign in to comment.