Skip to content

SpiderFoot automates OSINT for threat intelligence and mapping your attack surface.

New structure for capturing additional info from scans (risk, visibility and confidence.)

smicallefd959beb18292
diff --git a/sfdb.py b/sfdb.py
index fd99d1c2..57e2c15d 100644
--- a/sfdb.py
+++ b/sfdb.py
@@ -140,10 +140,10 @@ class SpiderFootDb:
# Obtain a summary of the results per event type
def scanResultSummary(self, instanceId):
- qry = "SELECT r.event, e.event_descr, MAX(ROUND(generated))/1000 AS last_in, \
+ qry = "SELECT r.type, e.event_descr, MAX(ROUND(generated)) AS last_in, \
count(*) AS total FROM \
- tbl_scan_results r, tbl_event_types e WHERE e.event = r.event \
- AND r.scan_instance_id = ? GROUP BY r.event ORDER BY e.event_descr"
+ tbl_scan_results r, tbl_event_types e WHERE e.event = r.type \
+ AND r.scan_instance_id = ? GROUP BY r.type ORDER BY e.event_descr"
qvars = [instanceId]
try:
self.dbh.execute(qry, qvars)
@@ -154,15 +154,17 @@ class SpiderFootDb:
# Obtain the data for a scan and event type
def scanResultEvent(self, instanceId, eventType='ALL'):
- qry = "SELECT ROUND(generated/1000) AS generated, event_data, event_source, \
- event_data_source, event FROM tbl_scan_results WHERE scan_instance_id = ?"
+ qry = "SELECT DISTINCT ROUND(c.generated) AS generated, c.data, \
+ s.data as 'source_data', \
+ c.module, c.type FROM tbl_scan_results c, tbl_scan_results s \
+ WHERE c.scan_instance_id = ? AND c.source_event_hash = s.hash"
qvars = [instanceId]
if eventType != "ALL":
- qry = qry + " AND event = ?"
+ qry = qry + " AND c.type = ?"
qvars.append(eventType)
- qry = qry + " ORDER BY event_data, event_source"
+ qry = qry + " ORDER BY c.data"
try:
self.dbh.execute(qry, qvars)
@@ -171,6 +173,25 @@ class SpiderFootDb:
sf.error("SQL error encountered when fetching result events: " +
e.args[0])
+ # Obtain a unique list of elements
+ def scanResultEventUnique(self, instanceId, eventType='ALL'):
+ qry = "SELECT DISTINCT data, type FROM tbl_scan_results \
+ WHERE scan_instance_id = ?"
+ qvars = [instanceId]
+
+ if eventType != "ALL":
+ qry = qry + " AND type = ?"
+ qvars.append(eventType)
+
+ qry = qry + " ORDER BY type, data"
+
+ try:
+ self.dbh.execute(qry, qvars)
+ return self.dbh.fetchall()
+ except sqlite3.Error as e:
+ sf.error("SQL error encountered when fetching unique result events: " +
+ e.args[0])
+
# Get scan logs
def scanLogs(self, instanceId):
qry = "SELECT generated AS generated, component, \
@@ -286,14 +307,24 @@ class SpiderFootDb:
sf.error("SQL error encountered when fetching configuration: " + e.args[0])
# Store an event
- def scanEventStore(self, instanceId, eventName, eventSource,
- eventData, eventDataSource):
+ # eventData is a SpiderFootEvent object with the following variables:
+ # - eventType: the event, e.g. URL_FORM, RAW_DATA, etc.
+ # - generated: time the event occurred
+ # - confidence: how sure are we of this data's validity, 0-100
+ # - visibility: how 'visible' was this data, 0-100
+ # - risk: how much risk does this data represent, 0-100
+ # - module: module that generated the event
+ # - data: the actual data, i.e. a URL, port number, webpage content, etc.
+ # - sourceEventHash: hash of the event that triggered this event
+ # And getHash() will return the event hash.
+ def scanEventStore(self, instanceId, sfEvent):
qry = "INSERT INTO tbl_scan_results \
- (scan_instance_id, generated, event, event_source, \
- event_data, event_data_source) \
- VALUES (?, ?, ?, ?, ?, ?)"
- qvals = [instanceId, time.time() * 1000, eventName, eventSource,
- eventData, eventDataSource]
+ (scan_instance_id, hash, type, generated, confidence, \
+ visibility, risk, module, data, source_event_hash) \
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
+ qvals = [ instanceId, sfEvent.getHash(), sfEvent.eventType, sfEvent.generated,
+ sfEvent.confidence, sfEvent.visibility, sfEvent.risk,
+ sfEvent.module, sfEvent.data, sfEvent.sourceEventHash ]
#print "STORING: " + str(qvals)
@@ -311,7 +342,7 @@ class SpiderFootDb:
# does a UNION of scans with results and scans without results to
# get a complete listing.
qry = "SELECT i.guid, i.name, i.seed_target, ROUND(i.created/1000), \
- ROUND(i.started)/1000 as started, ROUND(i.ended)/1000, i.status, COUNT(r.event) \
+ ROUND(i.started)/1000 as started, ROUND(i.ended)/1000, i.status, COUNT(r.type) \
FROM tbl_scan_instance i, tbl_scan_results r WHERE i.guid = r.scan_instance_id \
GROUP BY i.guid \
UNION ALL \
@@ -329,8 +360,8 @@ class SpiderFootDb:
# History of data from the scan
def scanResultHistory(self, instanceId):
qry = "SELECT STRFTIME('%H:%M %w', ROUND(generated/1000), 'unixepoch') AS hourmin, \
- event, COUNT(*) FROM tbl_scan_results \
- WHERE scan_instance_id = ? GROUP BY hourmin, event"
+ type, COUNT(*) FROM tbl_scan_results \
+ WHERE scan_instance_id = ? GROUP BY hourmin, type"
qvars = [instanceId]
try:
self.dbh.execute(qry, qvars)
diff --git a/sfdbsetup.py b/sfdbsetup.py
index 551dee5a..8aa290af 100644
--- a/sfdbsetup.py
+++ b/sfdbsetup.py
@@ -68,14 +68,19 @@ class SpiderFootDbInit:
)",
"CREATE TABLE tbl_scan_results ( \
scan_instance_id VARCHAR NOT NULL REFERENCES tbl_scan_instance(guid), \
+ hash VARCHAR NOT NULL, \
+ type VARCHAR NOT NULL REFERENCES tbl_event_types(event), \
generated INT NOT NULL, \
- event VARCHAR NOT NULL REFERENCES tbl_event_types(event), \
- event_source VARCHAR NOT NULL, \
- event_data VARCHAR NOT NULL, \
- event_data_source VARCHAR NOT NULL \
+ confidence INT NOT NULL DEFAULT 100, \
+ visibility INT NOT NULL DEFAULT 100, \
+ risk INT NOT NULL DEFAULT 0, \
+ module VARCHAR NOT NULL, \
+ data VARCHAR, \
+ source_event_hash VARCHAR DEFAULT 'ROOT' \
)",
"CREATE INDEX idx_scan_results_id ON tbl_scan_results (scan_instance_id)",
- "CREATE INDEX idx_scan_results_event ON tbl_scan_results (scan_instance_id, event)",
+ "CREATE INDEX idx_scan_results_type ON tbl_scan_results (scan_instance_id, type)",
+ "CREATE INDEX idx_scan_results_hash ON tbl_scan_results (hash)",
"CREATE INDEX idx_scan_logs ON tbl_scan_log (scan_instance_id)",
"INSERT INTO tbl_event_types (event, event_descr, event_raw) VALUES ('AFFILIATE', 'Affiliate', 0)",
"INSERT INTO tbl_event_types (event, event_descr, event_raw) VALUES ('EMAILADDR', 'Email Address', 0)",
diff --git a/sflib.py b/sflib.py
index 949c42c0..ef3dea96 100644
--- a/sflib.py
+++ b/sflib.py
@@ -11,8 +11,10 @@
#-------------------------------------------------------------------------------
import inspect
+import hashlib
import re
import sys
+import time
import urllib2
class SpiderFoot:
@@ -405,7 +407,7 @@ class SpiderFoot:
fullPage = urllib2.urlopen(req, None, self.opts['_fetchtimeout'])
# Prepare result to return
- result['content'] = unicode(fullPage.read(), 'utf-8', errors='ignore')
+ result['content'] = unicode(fullPage.read(), 'ascii', errors='ignore')
result['headers'] = fullPage.info()
result['realurl'] = fullPage.geturl()
result['code'] = fullPage.getcode()
@@ -439,6 +441,8 @@ class SpiderFootPlugin(object):
_stopScanning = False
# Modules that will be notified when this module produces events
_listenerModules = list()
+ # Name of this module, set at startup time
+ __name__ = "module_name_not_set!"
# Not really needed in most cases.
def __init__(self):
@@ -462,20 +466,15 @@ class SpiderFootPlugin(object):
# Call the handleEvent() method of every other plug-in listening for
# events from this plug-in. Remember that those plug-ins will be called
# within the same execution context of this thread, not on their own.
- # eventName = the ID of the event, e.g. RAW_DATA
- # eventSource = the item where this event was extracted from, e.g. a URL
- # eventData = the actual data for this event (would correspond to the eventName,
- # so if we're reporting RAW_DATA, eventData is the actual content.
- def notifyListeners(self, eventName, eventSource, eventData):
+ def notifyListeners(self, sfEvent):
+ eventName = sfEvent.eventType
+ eventData = sfEvent.data
+
# Check if we've been asked to stop in the meantime, so that
# notifications stop triggering module activity.
if self.checkForStop():
return None
- if self.opts.has_key('blocknotif') and self.opts['__blocknotif']:
- #print "Notifications blocked for " + eventName + " to " + listener.__module__
- return None
-
if eventData == None or (type(eventData) is unicode and len(eventData) == 0):
#print "No data to send for " + eventName + " to " + listener.__module__
return None
@@ -486,7 +485,7 @@ class SpiderFootPlugin(object):
#print listener.__module__ + " not listening for " + eventName
continue
#print "Notifying " + eventName + " to " + listener.__module__
- listener.handleEvent(self.__module__, eventName, eventSource, eventData)
+ listener.handleEvent(sfEvent)
# Called to stop scanning
def stopScanning(self):
@@ -511,7 +510,7 @@ class SpiderFootPlugin(object):
# Handle events to this module
# Will usually be overriden by the implementer, unless it doesn't handle
# any events.
- def handleEvent(self, srcModuleName, eventName, eventSource, eventData):
+ def handleEvent(self, sfEvent):
return None
# Kick off the work (for some modules nothing will happen here, but instead
@@ -519,3 +518,51 @@ class SpiderFootPlugin(object):
# Will usually be overriden by the implementer.
def start(self):
return None
+
+# Class for SpiderFoot Events
+class SpiderFootEvent(object):
+ generated = None
+ eventType = None
+ confidence = None
+ visibility = None
+ risk = None
+ module = None
+ data = None
+ sourceEventHash = None
+
+ def __init__(self, eventType, data, module, sourceEventHash="ROOT",
+ confidence=100, visibility=100, risk=0):
+ self.eventType = eventType
+ self.generated = time.time()
+ self.confidence = confidence
+ self.visibility = visibility
+ self.risk = risk
+ self.module = module
+ self.data = data
+ self.sourceEventHash = sourceEventHash
+
+ # Unique hash of this event
+ def getHash(self):
+ if type(self.data) != unicode:
+ self.data = unicode(self.data, 'ascii', errors='ignore')
+
+ return hashlib.sha256(self.eventType + self.data + \
+ unicode(self.generated) + self.module).hexdigest()
+
+ # Reduce data down to a certain size
+ def truncateData(self, size):
+ self.data = self.data[0:size]
+
+ # Update variables as new information becomes available
+ def setConfidence(self, confidence):
+ self.confidence = confidence
+
+ def setVisibility(self, visibility):
+ self.visibility = visibility
+
+ def setRisk(self, risk):
+ self.risk = risk
+
+ def setSourceEventHash(self, srcHash):
+ self.sourceEventHash = srcHash
+
diff --git a/sfscan.py b/sfscan.py
index 798a6855..66d68837 100644
--- a/sfscan.py
+++ b/sfscan.py
@@ -81,6 +81,7 @@ class SpiderFootScanner:
module = __import__('modules.' + modName, globals(), locals(), [modName])
mod = getattr(module, modName)()
+ mod.__name__ = modName
# A bit hacky: we pass the database object as part of the config. This
# object should only be used by the internal SpiderFoot modules writing
diff --git a/spiderfoot.schema b/spiderfoot.schema
index de966ba7..cceb6590 100644
--- a/spiderfoot.schema
+++ b/spiderfoot.schema
@@ -78,13 +78,18 @@ CREATE TABLE tbl_scan_config (
DROP TABLE tbl_scan_results;
CREATE TABLE tbl_scan_results (
scan_instance_id VARCHAR NOT NULL REFERENCES tbl_scan_instance(guid),
- generated INT NOT NULL,
- event VARCHAR NOT NULL REFERENCES tbl_event_types(event),
- event_source VARCHAR NOT NULL,
- event_data VARCHAR NOT NULL,
- event_data_source VARCHAR NOT NULL
+ hash VARCHAR NOT NULL,
+ type VARCHAR NOT NULL REFERENCES tbl_event_types(event),
+ generated INT NOT NULL,
+ confidence INT NOT NULL DEFAULT 100,
+ visibility INT NOT NULL DEFAULT 100,
+ risk INT NOT NULL DEFAULT 0,
+ module VARCHAR NOT NULL,
+ data VARCHAR
+ source_event_hash VARCHAR DEFAULT 'ROOT'
);
CREATE INDEX idx_scan_results_id ON tbl_scan_results (scan_instance_id);
-CREATE INDEX idx_scan_results_event ON tbl_scan_results (scan_instance_id, event);
+CREATE INDEX idx_scan_results_type ON tbl_scan_results (scan_instance_id, type);
+CREATE INDEX idx_scan_results_hash ON tbl_scan_results (hash);
CREATE INDEX idx_scan_logs ON tbl_scan_log (scan_instance_id);