35
35
CUCKOO_ROOT = os .path .join (os .path .abspath (os .path .dirname (__file__ )), ".." )
36
36
sys .path .append (CUCKOO_ROOT )
37
37
38
+ from lib .cuckoo .common .iocs import dump_iocs , load_iocs
38
39
from lib .cuckoo .common .cleaners_utils import free_space_monitor
39
40
from lib .cuckoo .common .config import Config
40
41
from lib .cuckoo .common .dist_db import ExitNodes , Machine , Node , Task , create_session
54
55
init_database ,
55
56
)
56
57
from lib .cuckoo .core .database import Task as MD_Task
58
+ from dev_utils .mongodb import mongo_update_one
57
59
58
60
dist_conf = Config ("distributed" )
59
61
main_server_name = dist_conf .distributed .get ("main_server_name" , "master" )
@@ -369,7 +371,7 @@ def node_submit_task(task_id, node_id, main_task_id):
369
371
7. Logs relevant information and errors during the process.
370
372
"""
371
373
db = session ()
372
- node = db .scalar (select (Node . id , Node . name , Node . url , Node . apikey ).where (Node .id == node_id ))
374
+ node = db .scalar (select (Node ).where (Node .id == node_id ))
373
375
task = db .get (Task , task_id )
374
376
check = False
375
377
try :
@@ -939,6 +941,11 @@ def fetch_latest_reports_nfs(self):
939
941
940
942
start_copy = timeit .default_timer ()
941
943
copied = node_get_report_nfs (t .task_id , node .name , t .main_task_id )
944
+
945
+ if not copied :
946
+ log .error ("Can't copy report %d from node: %s for task: %d" , t .task_id , node .name , t .main_task_id )
947
+ continue
948
+
942
949
timediff = timeit .default_timer () - start_copy
943
950
log .info (
944
951
"It took %s seconds to copy report %d from node: %s for task: %d" ,
@@ -948,17 +955,20 @@ def fetch_latest_reports_nfs(self):
948
955
t .main_task_id ,
949
956
)
950
957
951
- if not copied :
952
- log .error ("Can't copy report %d from node: %s for task: %d" , t .task_id , node .name , t .main_task_id )
953
- continue
954
-
955
958
# this doesn't exist for some reason
956
959
if path_exists (t .path ):
957
960
sample_sha256 = None
961
+ sample_parent = None
958
962
with main_db .session .begin ():
959
963
samples = main_db .find_sample (task_id = t .main_task_id )
960
964
if samples :
961
965
sample_sha256 = samples [0 ].sample .sha256
966
+ if hasattr (samples [0 ].sample , "parent_links" ):
967
+ for parent in samples [0 ].sample .parent_links :
968
+ if parent .task_id == t .main_task_id :
969
+ sample_parent = parent .parent .to_dict ()
970
+ break
971
+
962
972
if sample_sha256 is None :
963
973
# keep fallback for now
964
974
sample = open (t .path , "rb" ).read ()
@@ -980,6 +990,18 @@ def fetch_latest_reports_nfs(self):
980
990
981
991
self .delete_target_file (t .main_task_id , sample_sha256 , t .path )
982
992
993
+ if sample_parent :
994
+ try :
995
+ report = load_iocs (t .main_task_id , detail = True )
996
+ report ["info" ].update ({"parent_sample" : sample_parent })
997
+ dump_iocs (report , t .main_task_id )
998
+ # ToDo insert into mongo
999
+ mongo_update_one (
1000
+ "analysis" , {"info.id" : int (t .main_task_id )}, {"$set" : {"info.parent_sample" : sample_parent }}
1001
+ )
1002
+ except Exception as e :
1003
+ log .exception ("Failed to save iocs for parent sample: %s" , str (e ))
1004
+
983
1005
t .retrieved = True
984
1006
t .finished = True
985
1007
db .commit ()
@@ -1059,7 +1081,7 @@ def fetch_latest_reports(self):
1059
1081
main_db .set_status (t .main_task_id , TASK_REPORTED )
1060
1082
1061
1083
# Fetch each requested report.
1062
- node = db .scalar (select (Node . id , Node . name , Node . url , Node . apikey ).where (Node .id == node_id ))
1084
+ node = db .scalar (select (Node ).where (Node .id == node_id ))
1063
1085
report = node_get_report (t .task_id , "dist/" , node .url , node .apikey , stream = True )
1064
1086
1065
1087
if report is None :
@@ -1217,12 +1239,12 @@ class StatusThread(threading.Thread):
1217
1239
The main loop that continuously checks the status of nodes and submits tasks.
1218
1240
"""
1219
1241
1220
- def submit_tasks (self , node_id , pend_tasks_num , options_like = False , force_push_push = False , db = None ):
1242
+ def submit_tasks (self , node_name , pend_tasks_num , options_like = False , force_push_push = False , db = None ):
1221
1243
"""
1222
1244
Submits tasks to a specified node.
1223
1245
1224
1246
Args:
1225
- node_id (str): The identifier of the node to which tasks will be submitted.
1247
+ node_name (str): The identifier of the node to which tasks will be submitted.
1226
1248
pend_tasks_num (int): The number of pending tasks to be submitted.
1227
1249
options_like (bool, optional): Flag to filter tasks based on options. Defaults to False.
1228
1250
force_push_push (bool, optional): Flag to forcefully push tasks to the node. Defaults to False.
@@ -1238,7 +1260,7 @@ def submit_tasks(self, node_id, pend_tasks_num, options_like=False, force_push_p
1238
1260
# HACK do not create a new session if the current one (passed as parameter) is still valid.
1239
1261
try :
1240
1262
# ToDo name should be id?
1241
- node = db .scalar (select (Node ).where (Node .name == node_id ))
1263
+ node = db .scalar (select (Node ).where (Node .name == node_name ))
1242
1264
except (OperationalError , SQLAlchemyError ) as e :
1243
1265
log .warning ("Got an operational Exception when trying to submit tasks: %s" , str (e ))
1244
1266
return False
@@ -1291,7 +1313,6 @@ def submit_tasks(self, node_id, pend_tasks_num, options_like=False, force_push_p
1291
1313
)
1292
1314
main_db .set_status (t .id , TASK_BANNED )
1293
1315
continue
1294
-
1295
1316
force_push = False
1296
1317
try :
1297
1318
# check if node exist and its correct
@@ -1308,18 +1329,17 @@ def submit_tasks(self, node_id, pend_tasks_num, options_like=False, force_push_p
1308
1329
except Exception as e :
1309
1330
log .exception (e )
1310
1331
# wtf are you doing in pendings?
1311
- tasks = db .scalars (select (Task ).where (Task .main_task_id == t .id ))
1332
+ tasks = db .scalars (select (Task ).where (Task .main_task_id == t .id )). all ()
1312
1333
if tasks :
1313
1334
for task in tasks :
1314
- # log.info("Deleting incorrectly uploaded file from dist db, main_task_id: %s", t.id)
1335
+ log .info ("Deleting incorrectly uploaded file from dist db, main_task_id: %s" , t .id )
1315
1336
if node .name == main_server_name :
1316
1337
main_db .set_status (t .id , TASK_RUNNING )
1317
1338
else :
1318
1339
main_db .set_status (t .id , TASK_DISTRIBUTED )
1319
1340
# db.delete(task)
1320
1341
db .commit ()
1321
1342
continue
1322
-
1323
1343
# Convert array of tags into comma separated list
1324
1344
tags = "," .join ([tag .name for tag in t .tags ])
1325
1345
# Append a comma, to make LIKE searches more precise
@@ -1340,7 +1360,6 @@ def submit_tasks(self, node_id, pend_tasks_num, options_like=False, force_push_p
1340
1360
t .options = "," .join ([f"{ k } ={ v } " for k , v in options .items ()])
1341
1361
if t .options :
1342
1362
t .options += ","
1343
-
1344
1363
t .options += f"main_task_id={ t .id } "
1345
1364
args = dict (
1346
1365
package = t .package ,
@@ -1360,7 +1379,6 @@ def submit_tasks(self, node_id, pend_tasks_num, options_like=False, force_push_p
1360
1379
tlp = t .tlp ,
1361
1380
)
1362
1381
task = Task (path = t .target , ** args )
1363
-
1364
1382
db .add (task )
1365
1383
try :
1366
1384
db .commit ()
@@ -1370,7 +1388,6 @@ def submit_tasks(self, node_id, pend_tasks_num, options_like=False, force_push_p
1370
1388
db .rollback ()
1371
1389
log .info (e )
1372
1390
continue
1373
-
1374
1391
if force_push or force_push_push :
1375
1392
# Submit appropriate tasks to node
1376
1393
submitted = node_submit_task (task .id , node .id , t .id )
@@ -1392,9 +1409,11 @@ def submit_tasks(self, node_id, pend_tasks_num, options_like=False, force_push_p
1392
1409
if q is None:
1393
1410
db.commit()
1394
1411
return True
1412
+
1395
1413
# Order by task priority and task id.
1396
1414
q = q.order_by(-Task.priority, Task.main_task_id)
1397
1415
# if we have node set in options push
1416
+
1398
1417
if dist_conf.distributed.enable_tags:
1399
1418
# Create filter query from tasks in ta
1400
1419
tags = [getattr(Task, "tags") == ""]
@@ -1410,14 +1429,18 @@ def submit_tasks(self, node_id, pend_tasks_num, options_like=False, force_push_p
1410
1429
tags.append(and_(*t_combined))
1411
1430
# Filter by available tags
1412
1431
q = q.filter(or_(*tags))
1432
+
1413
1433
to_upload = q.limit(pend_tasks_num).all()
1414
1434
"""
1415
1435
# 1. Start with a select() statement and initial filters.
1416
- stmt = select (Task ).where (or_ (Task .node_id .is_ (None ), Task .task_id .is_ (None )), Task .finished .is_ (False ))
1417
-
1418
- # 2. Apply ordering with modern syntax.
1419
- stmt = stmt .order_by (Task .priority .desc (), Task .main_task_id )
1420
-
1436
+ stmt = (
1437
+ select (Task )
1438
+ .where (or_ (Task .node_id .is_ (None ), Task .task_id .is_ (None )), Task .finished .is_ (False ))
1439
+ .order_by (Task .priority .desc (), Task .main_task_id )
1440
+ )
1441
+ # print(stmt, "stmt")
1442
+ # ToDo broken
1443
+ """
1421
1444
# 3. Apply the dynamic tag filter.
1422
1445
if dist_conf.distributed.enable_tags:
1423
1446
tags_conditions = [Task.tags == ""]
@@ -1432,9 +1455,10 @@ def submit_tasks(self, node_id, pend_tasks_num, options_like=False, force_push_p
1432
1455
tags_conditions.append(and_(*t_combined))
1433
1456
1434
1457
stmt = stmt.where(or_(*tags_conditions))
1435
-
1458
+ """
1436
1459
# 4. Apply the limit and execute the query.
1437
1460
to_upload = db .scalars (stmt .limit (pend_tasks_num )).all ()
1461
+ print (to_upload , node .name , pend_tasks_num )
1438
1462
1439
1463
if not to_upload :
1440
1464
db .commit ()
0 commit comments