-
-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add attack type to the session * add status to the session * add session analyzer - tool to evaluate session
- Loading branch information
Showing
8 changed files
with
191 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import pickle | ||
|
||
|
||
class DorksManager: | ||
with open('dorks.pickle', 'rb') as fh: | ||
dorks = pickle.load(fh) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import pickle | ||
|
||
import redis | ||
import json | ||
import asyncio | ||
import socket | ||
import operator | ||
from dorks_manager import DorksManager | ||
|
||
|
||
class SessionAnalyzer: | ||
def __init__(self): | ||
self.r = redis.StrictRedis(host='localhost', port=6379) | ||
|
||
@asyncio.coroutine | ||
def analyze(self): | ||
session = None | ||
session_key = yield | ||
try: | ||
session = self.r.get(session_key) | ||
session = json.loads(session.decode('utf-8')) | ||
except (redis.ConnectionError, TypeError) as e: | ||
pass | ||
stats = self.create_stats(session) | ||
|
||
def create_stats(self, session): | ||
sess_duration = session['end_time'] - session['start_time'] | ||
rps = sess_duration / session['count'] | ||
tbr, errors, hidden_links, attack_types = self.analyze_paths(session['paths']) | ||
|
||
stats = dict( | ||
uuid=session['uuid'], | ||
peer_ip=session['peer']['ip'], | ||
peer_port=session['peer']['port'], | ||
user_agent=session['user_agent'], | ||
sensor_uuid=session['sensor'], | ||
start_time=session['start_time'], | ||
end_time=session['end_time'], | ||
requests_in_second=rps, | ||
approx_time_between_requests=tbr, | ||
accepted_paths=session['count'], | ||
errors=errors, | ||
hidden_links=hidden_links, | ||
attack_types=attack_types, | ||
paths=session['paths'] | ||
) | ||
|
||
owner = self.choose_possible_owner(stats) | ||
stats.update(owner) | ||
|
||
return stats | ||
|
||
def analyze_paths(self, paths): | ||
tbr = [] | ||
attack_types = set() | ||
current_path = paths[0] | ||
|
||
for i, path in enumerate(paths, start=1): | ||
tbr.append(path['timestamp'] - current_path['timestamp']) | ||
current_path = path | ||
tbr_average = sum(tbr) / float(len(tbr)) | ||
|
||
errors = 0 | ||
hidden_links = 0 | ||
for path in paths: | ||
if path['response_status'] is not 200: | ||
errors += 1 | ||
if path['path'] in DorksManager.dorks: | ||
hidden_links += 1 | ||
if 'attack_type' in path: | ||
attack_types.add(path['attack_type']) | ||
return tbr_average, errors, hidden_links, attack_types | ||
|
||
def choose_possible_owner(self, stats): | ||
possible_owners = dict( | ||
user=0, | ||
tool=0, | ||
crawler=0, | ||
attacker=0 | ||
) | ||
attacks = {'rfi', 'sqli', 'lfi', 'xss'} | ||
bots_owner = ['Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)' | ||
'Googlebot/2.1 (+http://www.google.com/bot.html)' | ||
'Mozilla/5.0 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)' | ||
'Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)' | ||
'Mozilla/5.0 (Windows Phone 8.1; ARM; Trident/7.0; Touch; rv:11.0; IEMobile/11.0; NOKIA; Lumia 530) like Gecko (compatible; bingbot/2.0; +http://www.bing.com/bingbot.htm)'] | ||
if stats['user_agent'] in bots_owner: | ||
hostname, aliaslist, ipaddrlist = socket.gethostbyaddr(stats['peer_ip']) | ||
if 'search.msn.com' or 'googlebot.com' in hostname: | ||
possible_owners['crawler'] += 1 | ||
else: | ||
possible_owners['attacker'] += 1 | ||
if stats['requests_in_second'] >= 10: | ||
possible_owners['tool'] += 1 | ||
possible_owners['crawler'] += 1 | ||
else: | ||
possible_owners['user'] += 1 | ||
possible_owners['attacker'] += 1 | ||
if stats['hidden_links'] > 0: | ||
possible_owners['crawler'] += 1 | ||
possible_owners['attacker'] += 1 | ||
if stats['attack_types'].intersection(attacks): | ||
possible_owners['attacker'] += 1 | ||
|
||
maxval = max(possible_owners.items(), key=operator.itemgetter(1))[1] | ||
owners = [k for k, v in possible_owners.items() if v == maxval] | ||
return {'possible_owners': owners} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import unittest | ||
import json | ||
import redis | ||
from unittest import mock | ||
from session_analyzer import SessionAnalyzer | ||
|
||
session = b'{"uuid": "c546114f97f548f982756495f963e280", "start_time": 1466091813.4780173, ' \ | ||
b'"user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2767.4 Safari/537.36", ' \ | ||
b'"end_time": 1466091899.9854035, "sensor": "78e51180-bf0d-4757-8a04-f000e5efa179", "count": 24, ' \ | ||
b'"paths": [{"timestamp": 1466091813.4779778, "path": "/", "attack_type": "index", "response_status": 200}, ' \ | ||
b'{"timestamp": 1466091842.7088752, "path": "/fluent-python.html", "attack_type": "index", "response_status": 200}, ' \ | ||
b'{"timestamp": 1466091858.214475, "path": "/wow-movie.html?exec=/bin/bash", "attack_type": "index", "response_status": 200}, ' \ | ||
b'{"timestamp": 1466091871.9076045, "path": "/wow-movie.html?exec=/etc/passwd", "attack_type": "lfi", "response_status": 200}, ' \ | ||
b'{"timestamp": 1466091885.1003792, "path": "/wow-movie.html?exec=/bin/bash", "attack_type": "index", "response_status": 200}, ' \ | ||
b'{"timestamp": 1466091899.9854052, "path": "/wow-movie.html?exec=/../../../..///././././.../../../etc/passwd", "attack_type": "lfi", "response_status": 200}], ' \ | ||
b'"peer": {"port": 56970, "ip": "192.168.1.3"}}' | ||
|
||
|
||
class TestSessionAnalyzer(unittest.TestCase): | ||
def setUp(self): | ||
self.session = json.loads(session.decode('utf-8')) | ||
self.handler = SessionAnalyzer() | ||
|
||
def tests_load_session(self): | ||
stats = None | ||
analyzer = self.handler.analyze() | ||
redis_mock = mock.Mock(return_value=session) | ||
with mock.patch('redis.StrictRedis.get', redis_mock): | ||
stats = yield from analyzer | ||
self.assertIsNotNone(stats) | ||
|
||
def test_create_stats(self): | ||
stats = self.handler.create_stats(self.session) | ||
self.assertEqual(stats['possible_owners'], ['attacker']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters