示例#1
0
def test_export_items_tagged(runner):
    scope_items = ["10.0.0.0/8,a", "192.168.5.0/28"]
    blacklist_items = ["192.168.1.0/24,b"]
    ScopeItem.import_scope_list(scope_items, False)
    ScopeItem.import_scope_list(blacklist_items, True)
    result = runner.invoke(export_items)
    assert result.exit_code == 0
    result_dict = json.loads(result.output)
    assert len(result_dict["scope"]) == 2
    assert result_dict["scope"][0]["tags"] == ["a"]
    assert result_dict["scope"][1]["tags"] == []
    assert len(result_dict["blacklist"]) == 1
    assert result_dict["blacklist"][0]["blacklist"] is True
    assert result_dict["blacklist"][0]["tags"] == ["b"]
示例#2
0
def import_scope(scopetype=""):
    if scopetype == "blacklist":
        importBlacklist = True
        importForm = forms.ImportBlacklistForm()
    elif scopetype == "scope":
        importBlacklist = False
        importForm = forms.ImportScopeForm()
    else:
        return abort(404)
    if importForm.validate_on_submit():
        newScopeItems = importForm.scope.data.split("\n")
        fail, exist, success = ScopeItem.import_scope_list(
            newScopeItems, importBlacklist
        )
        db.session.commit()
        current_app.ScopeManager.update()
        if success:
            flash(f"{len(success)} targets added to {scopetype}!", "success")
        if exist:
            flash(f"{len(exist)} targets already existed!", "info")
        if fail:
            flash(f"{len(fail)} targets failed to import!", "danger")
            for item in fail:
                flash(f"{item}", "danger")
    else:
        for field, errors in importForm.errors.items():
            for error in errors:
                flash(error, "danger")
    return redirect(url_for(f"admin.{scopetype}"))
示例#3
0
def import_scope(scope_file: typing.TextIO, blacklist: bool):
    if not scope_file:
        return {"failed": 0, "existed": 0, "successful": 0}
    addresses = scope_file.readlines()
    fail, exist, success = ScopeItem.import_scope_list(addresses, blacklist)
    db.session.commit()

    return {"failed": fail, "existed": exist, "successful": success}
示例#4
0
def test_import_scope(app):
    fails, exists, succeeds = ScopeItem.import_scope_list(
        scopefile.split(), False)
    assert len(fails) == 1
    assert len(exists) == 1
    assert len(succeeds) == 4
    item = ScopeItem.query.filter_by(target="127.0.0.1/32").first()
    tags = [t.name for t in item.tags]
    assert len(tags) == 3
    assert "one" in tags
    assert "four" not in tags
示例#5
0
def test_import_scope(app):
    result = ScopeItem.import_scope_list(scopefile.split(), False)
    assert len(result["fail"]) == 1
    assert result["exist"] == 1
    assert result["success"] == 5
    item = ScopeItem.query.filter_by(target="127.0.0.1/32").first()
    tags = [t.name for t in item.tags]
    assert len(tags) == 3
    assert "one" in tags
    assert "four" not in tags
    item = ScopeItem.query.filter_by(target="10.12.13.14/32").first()
    assert len(item.tags) == 0
示例#6
0
def import_scope(file, blacklist, verbose):
    with open(file, "r") as scope:
        addresses = scope.readlines()
    fail, exist, success = ScopeItem.import_scope_list(addresses, blacklist)
    db.session.commit()

    summaryMessages = [
        f"{len(success)} successfully imported.",
        f"{len(exist)} already existed.",
        f"{len(fail)} failed to import.",
    ]

    if verbose:
        print_imports("\nSuccessful Imports:", success)
        print_imports("\nAlready Existed:", exist)
        print_imports("\nFailed Imports:", fail)
    print("\n".join(summaryMessages))