2019-07-26 13:44:29 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*-coding:UTF-8 -*
|
|
|
|
|
|
|
|
import os
|
2019-08-09 12:20:13 +00:00
|
|
|
import sys
|
2019-08-01 11:16:57 +00:00
|
|
|
import gzip
|
2019-07-26 13:44:29 +00:00
|
|
|
import redis
|
|
|
|
|
2019-08-09 12:20:13 +00:00
|
|
|
sys.path.append(os.path.join(os.environ['AIL_BIN'], 'packages/'))
|
2019-07-26 13:44:29 +00:00
|
|
|
import Date
|
2019-08-01 11:16:57 +00:00
|
|
|
import Tag
|
2019-11-13 15:46:28 +00:00
|
|
|
import Correlation
|
2019-11-12 16:08:52 +00:00
|
|
|
import Cryptocurrency
|
2019-11-08 15:00:42 +00:00
|
|
|
from Pgp import pgp
|
2019-07-26 13:44:29 +00:00
|
|
|
|
2019-11-12 16:08:52 +00:00
|
|
|
sys.path.append(os.path.join(os.environ['AIL_BIN'], 'lib/'))
|
|
|
|
import ConfigLoader
|
2019-12-03 09:10:32 +00:00
|
|
|
import Correlate_object
|
2019-11-12 16:08:52 +00:00
|
|
|
import Decoded
|
2019-12-17 14:13:36 +00:00
|
|
|
import Screenshot
|
2019-11-12 16:08:52 +00:00
|
|
|
|
2019-11-05 14:18:03 +00:00
|
|
|
config_loader = ConfigLoader.ConfigLoader()
|
|
|
|
PASTES_FOLDER = os.path.join(os.environ['AIL_HOME'], config_loader.get_config_str("Directories", "pastes")) + '/'
|
|
|
|
r_cache = config_loader.get_redis_conn("Redis_Cache")
|
|
|
|
r_serv_metadata = config_loader.get_redis_conn("ARDB_Metadata")
|
2019-12-03 09:10:32 +00:00
|
|
|
screenshot_directory = os.path.join(os.environ['AIL_HOME'], config_loader.get_config_str("Directories", "crawled_screenshot"))
|
2019-11-05 14:18:03 +00:00
|
|
|
config_loader = None
|
2019-07-26 13:44:29 +00:00
|
|
|
|
|
|
|
def exist_item(item_id):
|
|
|
|
if os.path.isfile(os.path.join(PASTES_FOLDER, item_id)):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2019-07-30 11:49:21 +00:00
|
|
|
|
2019-09-12 11:25:29 +00:00
|
|
|
def get_item_id(full_path):
|
|
|
|
return full_path.replace(PASTES_FOLDER, '', 1)
|
|
|
|
|
2019-12-10 14:41:47 +00:00
|
|
|
def get_item_date(item_id, add_separator=False):
|
2019-07-30 11:49:21 +00:00
|
|
|
l_directory = item_id.split('/')
|
2019-12-10 14:41:47 +00:00
|
|
|
if add_separator:
|
|
|
|
return '{}/{}/{}'.format(l_directory[-4], l_directory[-3], l_directory[-2])
|
|
|
|
else:
|
|
|
|
return '{}{}{}'.format(l_directory[-4], l_directory[-3], l_directory[-2])
|
2019-08-01 11:16:57 +00:00
|
|
|
|
2019-09-22 12:44:37 +00:00
|
|
|
def get_source(item_id):
|
|
|
|
return item_id.split('/')[-5]
|
|
|
|
|
|
|
|
def get_item_basename(item_id):
|
|
|
|
return os.path.basename(item_id)
|
|
|
|
|
2019-08-01 11:16:57 +00:00
|
|
|
def get_item_size(item_id):
|
|
|
|
return round(os.path.getsize(os.path.join(PASTES_FOLDER, item_id))/1024.0, 2)
|
|
|
|
|
|
|
|
def get_lines_info(item_id, item_content=None):
|
|
|
|
if not item_content:
|
|
|
|
item_content = get_item_content(item_id)
|
|
|
|
max_length = 0
|
|
|
|
line_id = 0
|
|
|
|
nb_line = 0
|
|
|
|
for line in item_content.splitlines():
|
|
|
|
length = len(line)
|
|
|
|
if length > max_length:
|
|
|
|
max_length = length
|
|
|
|
nb_line += 1
|
|
|
|
return {'nb': nb_line, 'max_length': max_length}
|
|
|
|
|
|
|
|
|
|
|
|
def get_item_content(item_id):
|
|
|
|
item_full_path = os.path.join(PASTES_FOLDER, item_id)
|
|
|
|
try:
|
|
|
|
item_content = r_cache.get(item_full_path)
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
item_content = None
|
|
|
|
except Exception as e:
|
|
|
|
item_content = None
|
|
|
|
if item_content is None:
|
|
|
|
try:
|
|
|
|
with gzip.open(item_full_path, 'r') as f:
|
2019-08-05 14:00:23 +00:00
|
|
|
item_content = f.read().decode()
|
2019-08-01 11:16:57 +00:00
|
|
|
r_cache.set(item_full_path, item_content)
|
|
|
|
r_cache.expire(item_full_path, 300)
|
|
|
|
except:
|
|
|
|
item_content = ''
|
|
|
|
return str(item_content)
|
|
|
|
|
|
|
|
# API
|
|
|
|
def get_item(request_dict):
|
|
|
|
if not request_dict:
|
|
|
|
return Response({'status': 'error', 'reason': 'Malformed JSON'}, 400)
|
|
|
|
|
|
|
|
item_id = request_dict.get('id', None)
|
|
|
|
if not item_id:
|
|
|
|
return ( {'status': 'error', 'reason': 'Mandatory parameter(s) not provided'}, 400 )
|
|
|
|
if not exist_item(item_id):
|
|
|
|
return ( {'status': 'error', 'reason': 'Item not found'}, 404 )
|
|
|
|
|
|
|
|
dict_item = {}
|
|
|
|
dict_item['id'] = item_id
|
|
|
|
date = request_dict.get('date', True)
|
|
|
|
if date:
|
2019-12-10 14:41:47 +00:00
|
|
|
add_separator = False
|
|
|
|
if request_dict.get('date_separator', False):
|
|
|
|
add_separator = True
|
|
|
|
dict_item['date'] = get_item_date(item_id, add_separator=add_separator)
|
2019-08-01 11:16:57 +00:00
|
|
|
tags = request_dict.get('tags', True)
|
|
|
|
if tags:
|
2020-01-06 16:07:52 +00:00
|
|
|
dict_item['tags'] = Tag.get_obj_tag(item_id)
|
2019-08-01 11:16:57 +00:00
|
|
|
|
|
|
|
size = request_dict.get('size', False)
|
|
|
|
if size:
|
|
|
|
dict_item['size'] = get_item_size(item_id)
|
|
|
|
|
|
|
|
content = request_dict.get('content', False)
|
|
|
|
if content:
|
2019-08-01 11:43:28 +00:00
|
|
|
# UTF-8 outpout, # TODO: use base64
|
2019-08-01 11:16:57 +00:00
|
|
|
dict_item['content'] = get_item_content(item_id)
|
|
|
|
|
|
|
|
lines_info = request_dict.get('lines', False)
|
|
|
|
if lines_info:
|
|
|
|
dict_item['lines'] = get_lines_info(item_id, dict_item.get('content', 'None'))
|
|
|
|
|
2019-08-16 15:59:44 +00:00
|
|
|
if request_dict.get('pgp'):
|
|
|
|
dict_item['pgp'] = {}
|
|
|
|
if request_dict['pgp'].get('key'):
|
|
|
|
dict_item['pgp']['key'] = get_item_pgp_key(item_id)
|
|
|
|
if request_dict['pgp'].get('mail'):
|
|
|
|
dict_item['pgp']['mail'] = get_item_pgp_mail(item_id)
|
|
|
|
if request_dict['pgp'].get('name'):
|
|
|
|
dict_item['pgp']['name'] = get_item_pgp_name(item_id)
|
|
|
|
|
|
|
|
if request_dict.get('cryptocurrency'):
|
|
|
|
dict_item['cryptocurrency'] = {}
|
|
|
|
if request_dict['cryptocurrency'].get('bitcoin'):
|
|
|
|
dict_item['cryptocurrency']['bitcoin'] = get_item_bitcoin(item_id)
|
|
|
|
|
2019-08-01 11:16:57 +00:00
|
|
|
return (dict_item, 200)
|
2019-08-16 15:59:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
###
|
|
|
|
### correlation
|
|
|
|
###
|
2019-11-08 15:00:42 +00:00
|
|
|
def get_item_cryptocurrency(item_id, currencies_type=None, get_nb=False):
|
|
|
|
'''
|
|
|
|
Return all cryptocurrencies of a given item.
|
|
|
|
|
|
|
|
:param item_id: item id
|
|
|
|
:param currencies_type: list of cryptocurrencies type
|
|
|
|
:type currencies_type: list, optional
|
|
|
|
'''
|
2019-11-12 16:08:52 +00:00
|
|
|
return Cryptocurrency.cryptocurrency.get_item_correlation_dict(item_id, correlation_type=currencies_type, get_nb=get_nb)
|
2019-11-08 15:00:42 +00:00
|
|
|
|
|
|
|
def get_item_pgp(item_id, currencies_type=None, get_nb=False):
|
|
|
|
'''
|
|
|
|
Return all pgp of a given item.
|
|
|
|
|
|
|
|
:param item_id: item id
|
|
|
|
:param currencies_type: list of cryptocurrencies type
|
|
|
|
:type currencies_type: list, optional
|
|
|
|
'''
|
|
|
|
return pgp.get_item_correlation_dict(item_id, correlation_type=currencies_type, get_nb=get_nb)
|
|
|
|
|
|
|
|
def get_item_decoded(item_id):
|
|
|
|
'''
|
|
|
|
Return all pgp of a given item.
|
|
|
|
|
|
|
|
:param item_id: item id
|
|
|
|
:param currencies_type: list of cryptocurrencies type
|
|
|
|
:type currencies_type: list, optional
|
|
|
|
'''
|
|
|
|
return Decoded.get_item_decoded(item_id)
|
|
|
|
|
2019-12-17 14:13:36 +00:00
|
|
|
def get_item_all_screenshot(item_id):
|
|
|
|
'''
|
|
|
|
Return all screenshot of a given item.
|
|
|
|
|
|
|
|
:param item_id: item id
|
|
|
|
'''
|
|
|
|
return Screenshot.get_item_screenshot_list(item_id)
|
|
|
|
|
2019-11-13 15:46:28 +00:00
|
|
|
def get_item_all_correlation(item_id, correlation_names=[], get_nb=False):
|
2019-11-08 15:00:42 +00:00
|
|
|
'''
|
|
|
|
Retun all correlation of a given item id.
|
|
|
|
|
|
|
|
:param item_id: item id
|
|
|
|
:type domain: str
|
|
|
|
|
|
|
|
:return: a dict of all correlation for a item id
|
|
|
|
:rtype: dict
|
|
|
|
'''
|
2019-11-13 15:46:28 +00:00
|
|
|
if not correlation_names:
|
2019-12-03 09:10:32 +00:00
|
|
|
correlation_names = Correlate_object.get_all_correlation_names()
|
2019-11-08 15:00:42 +00:00
|
|
|
item_correl = {}
|
2019-11-13 15:46:28 +00:00
|
|
|
for correlation_name in correlation_names:
|
|
|
|
if correlation_name=='cryptocurrency':
|
|
|
|
res = get_item_cryptocurrency(item_id, get_nb=get_nb)
|
|
|
|
elif correlation_name=='pgp':
|
|
|
|
res = get_item_pgp(item_id, get_nb=get_nb)
|
|
|
|
elif correlation_name=='decoded':
|
|
|
|
res = get_item_decoded(item_id)
|
2019-12-17 14:13:36 +00:00
|
|
|
elif correlation_name=='screenshot':
|
|
|
|
res = get_item_all_screenshot(item_id)
|
2019-11-13 15:46:28 +00:00
|
|
|
else:
|
|
|
|
res = None
|
|
|
|
# add correllation to dict
|
|
|
|
if res:
|
|
|
|
item_correl[correlation_name] = res
|
2019-11-08 15:00:42 +00:00
|
|
|
return item_correl
|
|
|
|
|
|
|
|
|
2019-08-16 15:59:44 +00:00
|
|
|
|
2019-11-08 15:00:42 +00:00
|
|
|
## TODO: REFRACTOR
|
2019-08-16 15:59:44 +00:00
|
|
|
def _get_item_correlation(correlation_name, correlation_type, item_id):
|
|
|
|
res = r_serv_metadata.smembers('item_{}_{}:{}'.format(correlation_name, correlation_type, item_id))
|
|
|
|
if res:
|
|
|
|
return list(res)
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
2019-11-08 15:00:42 +00:00
|
|
|
## TODO: REFRACTOR
|
2019-08-16 15:59:44 +00:00
|
|
|
def get_item_bitcoin(item_id):
|
|
|
|
return _get_item_correlation('cryptocurrency', 'bitcoin', item_id)
|
|
|
|
|
2019-11-08 15:00:42 +00:00
|
|
|
## TODO: REFRACTOR
|
2019-08-16 15:59:44 +00:00
|
|
|
def get_item_pgp_key(item_id):
|
|
|
|
return _get_item_correlation('pgpdump', 'key', item_id)
|
|
|
|
|
2019-11-08 15:00:42 +00:00
|
|
|
## TODO: REFRACTOR
|
2019-08-16 15:59:44 +00:00
|
|
|
def get_item_pgp_name(item_id):
|
|
|
|
return _get_item_correlation('pgpdump', 'name', item_id)
|
|
|
|
|
2019-11-08 15:00:42 +00:00
|
|
|
## TODO: REFRACTOR
|
2019-08-16 15:59:44 +00:00
|
|
|
def get_item_pgp_mail(item_id):
|
|
|
|
return _get_item_correlation('pgpdump', 'mail', item_id)
|
2019-09-11 13:33:04 +00:00
|
|
|
|
2019-11-08 15:00:42 +00:00
|
|
|
## TODO: REFRACTOR
|
2019-10-17 14:39:43 +00:00
|
|
|
def get_item_pgp_correlation(item_id):
|
|
|
|
pass
|
2019-09-11 13:33:04 +00:00
|
|
|
|
|
|
|
###
|
|
|
|
### GET Internal Module DESC
|
|
|
|
###
|
|
|
|
def get_item_list_desc(list_item_id):
|
|
|
|
desc_list = []
|
|
|
|
for item_id in list_item_id:
|
2020-01-06 16:07:52 +00:00
|
|
|
desc_list.append( {'id': item_id, 'date': get_item_date(item_id), 'tags': Tag.get_obj_tag(item_id)} )
|
2019-09-11 13:33:04 +00:00
|
|
|
return desc_list
|
2019-10-17 14:39:43 +00:00
|
|
|
|
|
|
|
# # TODO: add an option to check the tag
|
|
|
|
def is_crawled(item_id):
|
|
|
|
return item_id.startswith('crawled')
|
|
|
|
|
|
|
|
def is_onion(item_id):
|
|
|
|
is_onion = False
|
|
|
|
if len(is_onion) > 62:
|
|
|
|
if is_crawled(item_id) and item_id[-42:-36] == '.onion':
|
|
|
|
is_onion = True
|
|
|
|
return is_onion
|
|
|
|
|
|
|
|
def is_item_in_domain(domain, item_id):
|
|
|
|
is_in_domain = False
|
|
|
|
domain_lenght = len(domain)
|
|
|
|
if len(item_id) > (domain_lenght+48):
|
|
|
|
if item_id[-36-domain_lenght:-36] == domain:
|
|
|
|
is_in_domain = True
|
|
|
|
return is_in_domain
|
|
|
|
|
|
|
|
def get_item_domain(item_id):
|
|
|
|
return item_id[19:-36]
|
|
|
|
|
2019-12-17 14:13:36 +00:00
|
|
|
def get_domain(item_id):
|
|
|
|
item_id = item_id.split('/')
|
|
|
|
item_id = item_id[-1]
|
|
|
|
return item_id[:-36]
|
|
|
|
|
2019-10-17 14:39:43 +00:00
|
|
|
def get_item_children(item_id):
|
|
|
|
return list(r_serv_metadata.smembers('paste_children:{}'.format(item_id)))
|
2019-10-31 16:14:23 +00:00
|
|
|
|
|
|
|
def get_item_link(item_id):
|
|
|
|
return r_serv_metadata.hget('paste_metadata:{}'.format(item_id), 'real_link')
|
|
|
|
|
|
|
|
def get_item_screenshot(item_id):
|
|
|
|
screenshot = r_serv_metadata.hget('paste_metadata:{}'.format(item_id), 'screenshot')
|
|
|
|
if screenshot:
|
|
|
|
return os.path.join(screenshot[0:2], screenshot[2:4], screenshot[4:6], screenshot[6:8], screenshot[8:10], screenshot[10:12], screenshot[12:])
|
|
|
|
return ''
|
2019-12-03 09:10:32 +00:00
|
|
|
|
|
|
|
def get_item_har_name(item_id):
|
|
|
|
os.path.join(screenshot_directory, item_id) + '.json'
|
|
|
|
if os.path.isfile(har_path):
|
|
|
|
return har_path
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_item_har(har_path):
|
|
|
|
pass
|