def test_is_multi_cbn(self): r0 = Round.create(num=0) our_team = Team.create(name=Team.OUR_NAME) # CS single binary cs = ChallengeSet.create(name="single") cs.rounds = [r0] cbn = ChallengeBinaryNode.create(name="foo", cs=cs, blob="aaa1") # CS multi binary cs_multi = ChallengeSet.create(name="multi") cs_multi.rounds = [r0] cbn1 = ChallengeBinaryNode.create(name="foo1", cs=cs_multi, blob="aaa2") cbn2 = ChallengeBinaryNode.create(name="foo2", cs=cs_multi, blob="aaa3") # create fielding entries ChallengeSetFielding.create(cs=cs, cbns=[cbn], team=our_team, available_round=r0) ChallengeSetFielding.create(cs=cs_multi, cbns=[cbn1, cbn2], team=our_team, available_round=r0) assert_false(cs.is_multi_cbn) assert_true(cs_multi.is_multi_cbn)
def test_fielded_in_round(self): now = datetime.now() r1 = Round.create(num=0) r2 = Round.create(num=1) cs1 = ChallengeSet.create(name="foo") cs1.rounds = [r1, r2] cs2 = ChallengeSet.create(name="bar") cs2.rounds = [r1] assert_equals(len(ChallengeSet.fielded_in_round(r1)), 2) assert_in(cs1, ChallengeSet.fielded_in_round(r1)) assert_in(cs2, ChallengeSet.fielded_in_round(r1))
def test_all_tests_for_this_cs(self): cs = ChallengeSet.create(name="foo") job = AFLJob.create(cs=cs) test1 = farnsworth.models.Test.create(cs=cs, job=job, blob="test1") test2 = farnsworth.models.Test.create(cs=cs, job=job, blob="test2") assert_equals(len(cs.tests), 2)
def test_has_submissions_in_round(self): r0 = Round.create(num=0) r1 = Round.create(num=1) cs = ChallengeSet.create(name="foo") cbn = ChallengeBinaryNode.create(name="foo1", cs=cs, blob="aaa") our_team = Team.create(name=Team.OUR_NAME) other_team = Team.create(name="enemy") ChallengeSetFielding.create(cs=cs, cbns=[cbn], team=our_team, submission_round=r1) assert_false(cs.has_submissions_in_round(r0)) assert_true(cs.has_submissions_in_round(r1)) ChallengeSetFielding.create(cs=cs, cbns=[cbn], team=other_team, submission_round=r0) assert_false(cs.has_submissions_in_round(r0)) ChallengeSetFielding.create(cs=cs, cbns=[cbn], team=our_team, submission_round=r0) assert_true(cs.has_submissions_in_round(r0))
def test_cbns_by_patch_type(self): cs = ChallengeSet.create(name="foo") cbn = ChallengeBinaryNode.create(name="foo", cs=cs, blob="aaa") patch0 = PatchType.create( name="patch0", functionality_risk=0, exploitability=1, ) patch1 = PatchType.create( name="patch1", functionality_risk=0, exploitability=1, ) cbn1 = ChallengeBinaryNode.create(name="foo1", cs=cs, patch_type=patch0, blob="aaa1") cbn2 = ChallengeBinaryNode.create(name="foo2", cs=cs, patch_type=patch0, blob="aaa2") cbn3 = ChallengeBinaryNode.create(name="foo3", cs=cs, patch_type=patch1, blob="aaa3") assert_in(patch0, cs.cbns_by_patch_type().keys()) assert_in(patch1, cs.cbns_by_patch_type().keys()) assert_in(cbn1, cs.cbns_by_patch_type()[patch0]) assert_in(cbn2, cs.cbns_by_patch_type()[patch0]) assert_in(cbn3, cs.cbns_by_patch_type()[patch1])
def test_submitted_and_unsubmitted_patches(self): r0 = Round.create(num=0) team = Team.create(name=Team.OUR_NAME) cs = ChallengeSet.create(name="foo") cs.rounds = [r0] cbn = ChallengeBinaryNode.create(name="cbn", cs=cs, blob="aaa1") patchtype1 = PatchType.create(name="PatchType1", functionality_risk=0, exploitability=0) patchtype2 = PatchType.create(name="PatchType2", functionality_risk=0, exploitability=0) patch1 = ChallengeBinaryNode.create(name="patch1", patch_type=patchtype1, cs=cs, root=cbn, blob="aaa2") patch2 = ChallengeBinaryNode.create(name="patch2", patch_type=patchtype2, cs=cs, root=cbn, blob="aaa3") assert_equals(len(cbn.unsubmitted_patches), 2) assert_in(patch1, cbn.unsubmitted_patches) assert_in(patch2, cbn.unsubmitted_patches) assert_equals(len(cbn.submitted_patches), 0) ChallengeSetFielding.create_or_update_submission(team=team, cbns=[patch1, patch2], round=r0) assert_equals(len(cbn.submitted_patches), 2) assert_equals(len(cbn.unsubmitted_patches), 0)
def test_get_or_create(self): r = Round.create(num=0) cs = ChallengeSet.create(name="foo") cbn1 = ChallengeBinaryNode.create(name="foo1", cs=cs, blob="aaa1") cbn2 = ChallengeBinaryNode.create(name="foo1", cs=cs, blob="aaa2") ids = IDSRule.create(cs=cs, rules="aaa", sha256="sum") cbl1, crtd1 = CSSubmissionCable.get_or_create(cs=cs, ids=ids, cbns=[cbn1], round=r) assert_true(crtd1) cbl2, crtd2 = CSSubmissionCable.get_or_create(cs=cs, ids=ids, cbns=[cbn1], round=r) assert_false(crtd2) assert_equals(cbl1.id, cbl2.id) cbl3, crtd3 = CSSubmissionCable.get_or_create(cs=cs, ids=ids, cbns=[cbn1, cbn2], round=r) assert_true(crtd3) cbl4, crtd4 = CSSubmissionCable.get_or_create(cs=cs, ids=ids, cbns=[cbn1, cbn2], round=r) assert_false(crtd4) assert_equals(cbl3.id, cbl4.id)
def test_unsubmitted_exploits(self): r1 = Round.create(num=0) team = Team.create(name=Team.OUR_NAME) cs = ChallengeSet.create(name="foo") cs.rounds = [r1] job = RexJob.create(cs=cs) pov1 = Exploit.create(cs=cs, job=job, pov_type='type1', exploitation_method='rop', blob="exploit", c_code="exploit it") pov2 = Exploit.create(cs=cs, job=job, pov_type='type2', exploitation_method='rop', blob="exploit", c_code="exploit it") assert_equals(len(cs.unsubmitted_exploits), 2) assert_in(pov1, cs.unsubmitted_exploits) assert_in(pov2, cs.unsubmitted_exploits) pov1.submit_to(team, 10) assert_equals(len(cs.unsubmitted_exploits), 1) assert_not_in(pov1, cs.unsubmitted_exploits) assert_in(pov2, cs.unsubmitted_exploits) pov2.submit_to(team, 10) assert_equals(len(cs.unsubmitted_exploits), 0) assert_not_in(pov1, cs.unsubmitted_exploits) assert_not_in(pov2, cs.unsubmitted_exploits)
def test_get_by_sha256_or_create(self): cs = ChallengeSet.create(name="foo") assert_equals(len(IDSRule.all()), 0) ids1 = IDSRule.get_by_sha256_or_create(rules="aaa", cs=cs) ids2 = IDSRule.get_by_sha256_or_create(rules="aaa", cs=cs) assert_equals(ids1, ids2) assert_equals(len(IDSRule.all()), 1)
def test_cs_name_and_sha256_uniqueness(self): cs1 = ChallengeSet.create(name="foo") cs2 = ChallengeSet.create(name="bar") # first binary is ok ChallengeBinaryNode.create(name="test1", cs=cs1, blob=BLOB) # same binary with different name is ok ChallengeBinaryNode.create(name="test2", cs=cs1, blob=BLOB) # same binary with different cs is ok ChallengeBinaryNode.create(name="test1", cs=cs2, blob=BLOB) # same cs and name but different binary is ok ChallengeBinaryNode.create(name="test1", cs=cs2, blob=BLOB2) # same cs, name and binary raises error assert_raises(IntegrityError, ChallengeBinaryNode.create, name="test1", cs=cs1, blob=BLOB)
def test_most_recent(self): cs = ChallengeSet.create(name="foo") cs2 = ChallengeSet.create(name="bar") team = Team.create(name="opponent") exploit = Exploit.create(cs=cs, job=RexJob.create(), pov_type="type1", blob="abc", c_code="exploit it") exploit2 = Exploit.create(cs=cs2, job=RexJob.create(), pov_type="type1", blob="def", c_code="delfino") Round.create(num=0) cable = ExploitSubmissionCable.create(team=team, cs=cs, exploit=exploit, throws=10, round=Round.current_round()) cable2 = ExploitSubmissionCable.create(team=team, cs=cs2, exploit=exploit2, throws=10, round=Round.current_round()) assert_equals(len(ExploitSubmissionCable.most_recent()), 2) assert_items_equal(ExploitSubmissionCable.most_recent(), [cable, cable2]) # assert we get back only the most recent exploit r1 = Round.create(num=1) new_exploit = Exploit.create(cs=cs, job=RexJob.create(), pov_type="type2", blob="def", c_code="don't exploit it") new_cable = ExploitSubmissionCable.create(team=team, cs=cs, exploit=new_exploit, throws=10, round=r1) assert_equals(len(ExploitSubmissionCable.most_recent()), 2) assert_items_equal(ExploitSubmissionCable.most_recent(), [new_cable, cable2])
def test_found_crash_for_cs(self): cs = ChallengeSet.create(name="foo") job = AFLJob.create(cs=cs) crash = farnsworth.models.Crash.create(cs=cs, job=job, blob="crash", crash_pc=0x41414141) assert_true(cs.found_crash)
def test_undrilled_tests_for_cs(self): cs = ChallengeSet.create(name="foo") job = AFLJob.create(cs=cs) new_test = farnsworth.models.Test.create(cs=cs, job=job, blob="crash", drilled=False) assert_true(len(cs.undrilled_tests), 1)
def test_has_type1(self): from farnsworth.models.pov_test_result import PovTestResult pov_type = 'type1' cs = ChallengeSet.create(name="foo") job = AFLJob.create(cs=cs) Exploit.create(cs=cs, job=job, pov_type=pov_type, blob=BLOB, c_code="code", reliability=0) assert_false(cs.has_type1) Exploit.create(cs=cs, job=job, pov_type=pov_type, blob=BLOB, c_code="code", reliability=1) assert_true(cs.has_type1) # if not first condition is not met r2 = Round.create(num=2) cs2 = ChallengeSet.create(name="bar") job2 = AFLJob.create(cs=cs2) cbn2 = ChallengeBinaryNode.create(name="bar", cs=cs2, blob="aaa") team2 = Team.create(name=Team.OUR_NAME) exploit2 = Exploit.create(cs=cs2, job=job2, pov_type=pov_type, blob=BLOB, c_code="code", reliability=0) csf2 = ChallengeSetFielding.create_or_update_available(team=team2, cbn=cbn2, round=r2) pov_result2 = PovTestResult.create(exploit=exploit2, cs_fielding=csf2, num_success=0) assert_false(cs2.has_type1) pov_result2.num_success = 10 pov_result2.save() assert_true(cs2.has_type1)
def test_pov_type(self): cs = ChallengeSet.create(name="foo") job = AFLJob.create(cs=cs) exploit = Exploit.create(cs=cs, job=job, pov_type="type1", blob="exploit", c_code="exploit it") assert_equals(exploit.pov_type, "type1") def invalid_pov_type(): Exploit.create(cs=cs, job=job, pov_type="bar") assert_raises(Exception, invalid_pov_type)
def test_all(self): r1 = Round.create(num=0) cs = ChallengeSet.create(name="foo") cs.rounds = [r1] cbn1 = ChallengeBinaryNode.create(name="foo", cs=cs, blob="blob1") cbn2 = ChallengeBinaryNode.create(name="bar", cs=cs, blob="blob2") assert_equals(len(ChallengeBinaryNode.all()), 2) assert_equals(ChallengeBinaryNode.all()[0], cbn1) assert_equals(ChallengeBinaryNode.all()[1], cbn2)
def test_save(self): r1 = Round.create(num=0) cs = ChallengeSet.create(name="foo") cs.rounds = [r1] cbn = ChallengeBinaryNode.create(name="foo", cs=cs, blob="asdf") job = Job(cbn=cbn, worker='basemodel') updated_at = job.updated_at job.worker = '#aftersave' job.save() assert_greater(job.updated_at, updated_at)
def test_seen_in_round(self): r0 = Round.create(num=0) r1 = Round.create(num=1) cs = ChallengeSet.create(name="foo") cs.seen_in_round(r0) assert_equals(len(cs.rounds), 1) cs.seen_in_round(r0) assert_equals(len(cs.rounds), 1) cs.seen_in_round(r1) assert_equals(len(cs.rounds), 2)
def test_binary_is_created_and_deleted_properly(self): cs = ChallengeSet.create(name=str(time.time())) cbn = ChallengeBinaryNode.create(name="mybin-%s" % cs.name, cs=cs, blob=BLOB) binpath = cbn._path assert_false(os.path.isfile(binpath)) cbn.path assert_true(os.path.isfile(binpath)) assert_equals(open(cbn.path, 'rb').read(), BLOB) cbn = None
def test_unprocessed_submission_cables(self): r = Round.create(num=0) cs = ChallengeSet.create(name="foo") cbn = ChallengeBinaryNode.create(name="foo1", cs=cs, blob="aaa") ids = IDSRule.create(cs=cs, rules="aaa", blob="aaa") cable1 = CSSubmissionCable.create(cs=cs, ids=ids, cbns=[cbn], round=r) cable2 = CSSubmissionCable.create(cs=cs, ids=ids, cbns=[], round=r) assert_equals(len(cs.unprocessed_submission_cables()), 2) assert_equals(cable1, cs.unprocessed_submission_cables()[0]) assert_equals(cable2, cs.unprocessed_submission_cables()[1]) cable1.process() assert_equals(len(cs.unprocessed_submission_cables()), 1)
def test_get_or_create(self): cs = ChallengeSet.create(name="foo") cbn = ChallengeBinaryNode.create(name="foo", cs=cs, blob="aaa") job1, job1_created = RexJob.get_or_create(cbn=cbn, payload={'something': 'xxx'}) job2, job2_created = AFLJob.get_or_create(cbn=cbn, payload={'something': 'xxx'}) assert_not_equal(job1.id, job2.id) assert_true(job1_created) assert_true(job2_created) Job.delete().execute() ChallengeBinaryNode.delete().execute() ChallengeSet.delete().execute() Job._meta.database.commit()
def test_get_or_create(self): cs = ChallengeSet.create(name="foo") job = AFLJob.create() crash, created = Crash.get_or_create(cs=cs, job=job, blob="a blob") assert_true(created) crash, created = Crash.get_or_create(cs=cs, job=job, blob="a blob") assert_false(created) # because we're opening another transaction in create_or_get() # rollback doesn't work. clean everything in a transaction with Crash._meta.database.atomic(): crash.delete_instance() job.delete_instance() cs.delete_instance()
def test_to_job_type(self): cs = ChallengeSet.create(name="foo") cbn = ChallengeBinaryNode.create(name="foo", cs=cs, blob="aaa") afl_job = AFLJob.create(cbn=cbn) driller_job = DrillerJob.create(cbn=cbn) patcherex_job = PatcherexJob.create(cbn=cbn) rex_job = RexJob.create(cbn=cbn) tester_job = TesterJob.create(cbn=cbn) jobs = Job.select().order_by(Job.id.asc()) job_types = [AFLJob, DrillerJob, PatcherexJob, RexJob, TesterJob] for i in range(len(job_types)): job_type = to_job_type(jobs[i]).__class__ assert_in(job_type, job_types) job_types.remove(job_type)
def test_save_and_create_automatically_calculates_sha256(self): cs = ChallengeSet.create(name="foo") job = AFLJob.create() crash = Crash(cs=cs, job=job, blob="a blob") assert_is_none(crash.sha256) crash.save() assert_equals(crash.sha256, "bbffdf5ecaf101e014fa03c8d0b1996554bac10f") crash = Crash(cs=cs, job=job, blob="a blob", sha256="sum") crash.save() assert_equals(crash.sha256, "sum") crash = Crash.create(cs=cs, job=job, blob="another blob") assert_is_not_none(crash.sha256)
def test_input_test(self): cs = ChallengeSet.create(name="foo") generating_job = AFLJob.create(cs=cs) test = farnsworth.models.Test.create(job=generating_job, cs=cs, blob=str("ciao")) job = DrillerJob(cs=cs, payload={'test_id': test.id}) assert_equal(str(job.input_test.blob), "ciao") # it caches result between different requests assert_false(job.input_test.drilled) job.input_test.drilled = True job.input_test.save() assert_true(job.input_test.drilled)
def test_generate_hash_on_create_and_save_if_missing(self): cs = ChallengeSet.create(name="foo") ids_create = IDSRule.create(cs=cs, rules="aaa") assert_equals( ids_create.sha256, "9834876dcfb05cb167a5c24953eba58c4ac89b1adf57f28f2f9d09af107ee8f0") ids_save = IDSRule(cs=cs, rules="bbb") ids_save.save() assert_equals( ids_save.sha256, "3e744b9dc39389baf0c5a0660589b8402f3dbb49b89b3e75f2c9355852a3c677") ids_set = IDSRule.create(cs=cs, rules="ccc", sha256="ddd") assert_equals(ids_set.sha256, "ddd")
def test_root_association(self): cs = ChallengeSet.create(name="foo") root_cbn = ChallengeBinaryNode.create(name="root", cs=cs, blob=BLOB3) cbn1 = ChallengeBinaryNode.create(name="test1", cs=cs, root=root_cbn, blob=BLOB4) cbn2 = ChallengeBinaryNode.create(name="test2", cs=cs, root=root_cbn, blob=BLOB5) assert_equals(cbn1.root, root_cbn) assert_equals(len(root_cbn.descendants), 2) assert_in(cbn1, root_cbn.descendants) assert_in(cbn2, root_cbn.descendants)
def test_most_reliable_exploit(self): r1 = Round.create(num=0) team = Team.create(name=Team.OUR_NAME) cs = ChallengeSet.create(name="foo") cs.rounds = [r1] job1 = RexJob.create(cs=cs) job2 = RexJob.create(cs=cs) job3 = RexJob.create(cs=cs) job4 = RexJob.create(cs=cs) pov1 = Exploit.create(cs=cs, job=job1, pov_type='type1', exploitation_method='rop', blob="exploit1", c_code="exploit it", reliability=0.9) assert_equals(pov1, cs.most_reliable_exploit) pov2 = Exploit.create(cs=cs, job=job2, pov_type='type2', exploitation_method='rop', blob="exploit2", c_code="exploit it", reliability=0.5) assert_equals(pov1, cs.most_reliable_exploit) pov3 = Exploit.create(cs=cs, job=job3, pov_type='type2', exploitation_method='rop', blob="exploit3", c_code="exploit it", reliability=0.9) assert_equals(pov1, cs.most_reliable_exploit) pov4 = Exploit.create(cs=cs, job=job4, pov_type='type2', exploitation_method='rop', blob="exploit4", c_code="exploit it", reliability=1.0) assert_equals(pov4, cs.most_reliable_exploit)
def test_submit_to(self): r0 = Round.create(num=0) r1 = Round.create(num=1) cs = ChallengeSet.create(name="foo") cs.rounds = [r1.id] job = AFLJob.create(cs=cs) exploit = Exploit.create(job=job, cs=cs, pov_type="type1", blob=BLOB, c_code="exploit it") team = Team.create(name="opponent") assert_equals(len(exploit.fieldings), 0) ef = exploit.submit_to(team=team, throws=10) assert_equals(len(exploit.fieldings), 1) assert_equals(ef.submission_round, Round.current_round()) ef = exploit.submit_to(team=team, throws=5, round=r0) assert_equals(len(exploit.fieldings), 2) assert_equals(ef.submission_round, r0)
def test_all_descendants(self): cs = ChallengeSet.create(name="foo") cbn1 = ChallengeBinaryNode.create(name="root1", cs=cs, blob=BLOB, sha256="sum1") cbn2 = ChallengeBinaryNode.create(name="root2", cs=cs, blob=BLOB, sha256="sum2") cbn3 = ChallengeBinaryNode.create(name="child", cs=cs, blob=BLOB, root=cbn1, sha256="sum3") assert_equals(len(ChallengeBinaryNode.all_descendants()), 1) assert_in(cbn3, ChallengeBinaryNode.all_descendants())