Ejemplo n.º 1
0
def start_get_hash(config, github, config_file):
    repo_owner = config['repo_owner']
    for team in config['teams']:
        repo_name = config['teams'][team]['repo_name']
        if repo_name == '-':
            continue

        print('[*] Get the commit hash of %s repo.' % repo_name)
        bug_branches = config['teams'][team]['bug_branches']
        clone(repo_owner, repo_name)
        branches = bug_branches if len(bug_branches) > 0 \
            else list_branches(repo_name)
        if "master" in branches:
            branches.remove("master") # Do not consider master branch
        for branch in branches:
            checkout(repo_name, branch)
            hash = get_latest_commit_hash(repo_name, int(time.time()), branch)
            config['teams'][team][branch] = hash
        rmdir(repo_name)

    with open(config_file, 'w') as outfile:
        json.dump(config, outfile, indent=4)

    print ('[*] Successfully write in %s' % config_file)

    return
Ejemplo n.º 2
0
def verify_issue(defender,
                 repo_name,
                 issue_no,
                 config,
                 github,
                 target_commit=None):
    timeout = config["exploit_timeout"]["exercise_phase"]
    repo_owner = config['repo_owner']
    title, submitter, create_time, content = \
        get_github_issue(repo_owner, repo_name, issue_no, github)

    # Issue convention: "exploit-[branch_name]"
    target_branch = title[8:]

    clone(repo_owner, repo_name)

    # Write the fetched issue content to temp file
    tmpfile = "/tmp/gitctf_%s.issue" % random_string(6)
    tmpdir = "/tmp/gitctf_%s.dir" % random_string(6)

    with open(tmpfile, "w") as f:
        f.write(content)

    # Decrypt the exploit
    mkdir(tmpdir)

    team = defender
    decrypt_exploit(tmpfile, config, team, tmpdir, submitter)
    rmfile(tmpfile)

    # Now iterate through branches and verify exploit
    # zchn: not sure about this, was: branches = list_branches(repo_name)
    bug_branches = config['teams'][team]['bug_branches']
    branches = bug_branches + ['master'] if len(bug_branches) > 0 \
        else list_branches(repo_name)

    candidates = []
    if (target_branch in branches) and (target_commit is None):
        # Iterate through branches and collect candidates
        commit = get_latest_commit_hash(repo_name, create_time, target_branch)
        candidates.append((target_branch, commit))

    verified_branch = None
    verified_commit = None

    log = 'About %s (exploit-service branch)\n' % title

    for (branch, commit) in candidates:
        if branch in title:
            result, log = verify_exploit(tmpdir, repo_name, commit, timeout, \
                    config, log=log)
        else:
            result, _ = verify_exploit(tmpdir, repo_name, commit, timeout, \
                    config)

        if result:
            verified_branch = branch
            verified_commit = commit
            break

    rmdir(tmpdir)
    rmdir(repo_name)

    if verified_branch is None:
        print("[*] The exploit did not work against branch '%s'" % \
                target_branch)
    else:
        print("[*] The exploit has been verified against branch '%s'" %
              verified_branch)

    return (verified_branch, verified_commit, submitter, log)