<commit_before>#-------------------------------------------------------------------------------
# Name:         sfp_filemeta
# Purpose:      From Spidering and from searching search engines, extracts file
#               meta data from files matching certain file extensions.
#
# Author:      <NAME> <<EMAIL>>
#
# Created:     25/04/2014
# Copyright:   (c) <NAME> 2014
# Licence:     GPL
#-------------------------------------------------------------------------------

import sys
import random
import re
import time
import urllib
import mimetypes
import metapdf
import pyPdf
import openxmllib
from StringIO import StringIO
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent

# SpiderFoot standard lib (must be initialized in setup)
sf = None

class sfp_filemeta(SpiderFootPlugin):
    """File Metadata:Extracts meta data from certain file types."""

    # Default options
    opts = {
        'fileexts':     [ "docx", "pptx", 'xlsx', 'pdf' ],
        'timeout':      300
    }

    # Option descriptions
    optdescs = {
        'fileexts': "File extensions of files you want to analyze the meta data of.",
        'timeout':  "Download timeout for files, in seconds."
    }

    # Target
    baseDomain = None
    results = list()

    def setup(self, sfc, target, userOpts=dict()):
        global sf

        sf = sfc
        self.baseDomain = target
        self.results = list()

        for opt in userOpts.keys():
            self.opts[opt] = userOpts[opt]

    # What events is this module interested in for input
    def watchedEvents(self):
        return [ "LINKED_URL_INTERNAL", "INTERESTING_FILE" ]

    # What events this module produces
    # This is to support the end user in selecting modules based on events
    # produced.
    def producedEvents(self):
        return [ "RAW_FILE_META_DATA" ]

    # Handle events sent to this module
    def handleEvent(self, event):
        eventName = event.eventType
        srcModuleName = event.module
        eventData = event.data

        sf.debug("Received event, " + eventName + ", from " + srcModuleName)

        if eventData in self.results:
            return None
        else:
            self.results.append(eventData)

        for fileExt in self.opts['fileexts']:
            if self.checkForStop():
                return None

            if "." + fileExt.lower() in eventData.lower():
                # Fetch the file, allow much more time given that these files are
                # typically large.
                ret = sf.fetchUrl(eventData, timeout=self.opts['timeout'], 
                    useragent=self.opts['_useragent'], dontMangle=True)
                if ret['content'] == None:
                    sf.error("Unable to fetch file for meta analysis: " + \
                        eventData, False)
                    return None

                if len(ret['content']) < 1024:
                    sf.error("Strange content encountered, size of " + \
                        len(res['content']), False)

                meta = None
                # Based on the file extension, handle it
                if fileExt.lower() == "pdf":
                    try:
                        data = StringIO(ret['content'])
                        meta = str(metapdf.MetaPdfReader().read_metadata(data))
                        sf.debug("Obtained meta data from " + eventData)
                    except BaseException as e:
                        sf.error("Unable to parse meta data from: " + \
                            eventData + "(" + str(e) + ")", False)
                        return None

                if fileExt.lower() in [ "pptx", "docx", "xlsx" ]:
                    try:
                        mtype = mimetypes.guess_type(eventData)[0]
                        doc = openxmllib.openXmlDocument(data=ret['content'], mime_type=mtype)
                        sf.debug("Office type: " + doc.mimeType)
                        meta = str(doc.allProperties)
                    except ValueError as e:
                        sf.error("Unable to parse meta data from: " + \
                            eventData + "(" + str(e) + ")", False)

                if meta != None:
                    evt = SpiderFootEvent("RAW_FILE_META_DATA", meta,
                        self.__name__, event)
                    self.notifyListeners(evt)

                
# End of sfp_filemeta class
<commit_msg>Updated description.<commit_after>#-------------------------------------------------------------------------------
# Name:         sfp_filemeta
# Purpose:      From Spidering and from searching search engines, extracts file
#               meta data from files matching certain file extensions.
#
# Author:      <NAME> <<EMAIL>>
#
# Created:     25/04/2014
# Copyright:   (c) <NAME> 2014
# Licence:     GPL
#-------------------------------------------------------------------------------

import sys
import random
import re
import time
import urllib
import mimetypes
import metapdf
import pyPdf
import openxmllib
from StringIO import StringIO
from sflib import SpiderFoot, SpiderFootPlugin, SpiderFootEvent

# SpiderFoot standard lib (must be initialized in setup)
sf = None

class sfp_filemeta(SpiderFootPlugin):
    """File Metadata:Extracts meta data from certain file types."""

    # Default options
    opts = {
        'fileexts':     [ "docx", "pptx", 'xlsx', 'pdf' ],
        'timeout':      300
    }

    # Option descriptions
    optdescs = {
        'fileexts': "File extensions of files you want to analyze the meta data of (only PDF, DOCX, XLSX and PPTX are supported.)",
        'timeout':  "Download timeout for files, in seconds."
    }

    # Target
    baseDomain = None
    results = list()

    def setup(self, sfc, target, userOpts=dict()):
        global sf

        sf = sfc
        self.baseDomain = target
        self.results = list()

        for opt in userOpts.keys():
            self.opts[opt] = userOpts[opt]

    # What events is this module interested in for input
    def watchedEvents(self):
        return [ "LINKED_URL_INTERNAL", "INTERESTING_FILE" ]

    # What events this module produces
    # This is to support the end user in selecting modules based on events
    # produced.
    def producedEvents(self):
        return [ "RAW_FILE_META_DATA" ]

    # Handle events sent to this module
    def handleEvent(self, event):
        eventName = event.eventType
        srcModuleName = event.module
        eventData = event.data

        sf.debug("Received event, " + eventName + ", from " + srcModuleName)

        if eventData in self.results:
            return None
        else:
            self.results.append(eventData)

        for fileExt in self.opts['fileexts']:
            if self.checkForStop():
                return None

            if "." + fileExt.lower() in eventData.lower():
                # Fetch the file, allow much more time given that these files are
                # typically large.
                ret = sf.fetchUrl(eventData, timeout=self.opts['timeout'], 
                    useragent=self.opts['_useragent'], dontMangle=True)
                if ret['content'] == None:
                    sf.error("Unable to fetch file for meta analysis: " + \
                        eventData, False)
                    return None

                if len(ret['content']) < 1024:
                    sf.error("Strange content encountered, size of " + \
                        len(res['content']), False)

                meta = None
                # Based on the file extension, handle it
                if fileExt.lower() == "pdf":
                    try:
                        data = StringIO(ret['content'])
                        meta = str(metapdf.MetaPdfReader().read_metadata(data))
                        sf.debug("Obtained meta data from " + eventData)
                    except BaseException as e:
                        sf.error("Unable to parse meta data from: " + \
                            eventData + "(" + str(e) + ")", False)
                        return None

                if fileExt.lower() in [ "pptx", "docx", "xlsx" ]:
                    try:
                        mtype = mimetypes.guess_type(eventData)[0]
                        doc = openxmllib.openXmlDocument(data=ret['content'], mime_type=mtype)
                        sf.debug("Office type: " + doc.mimeType)
                        meta = str(doc.allProperties)
                    except ValueError as e:
                        sf.error("Unable to parse meta data from: " + \
                            eventData + "(" + str(e) + ")", False)

                if meta != None:
                    evt = SpiderFootEvent("RAW_FILE_META_DATA", meta,
                        self.__name__, event)
                    self.notifyListeners(evt)

                
# End of sfp_filemeta class

{'fraction_non_alphanumeric': 0.12216002634178466, 'fraction_numerical': 0.0043902974426517395, 'mean_word_length': 1.4214722296040394, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3675430', 'n_tokens_mistral': 2527, 'n_tokens_neox': 2257, 'n_words': 683}