Beispiel #1
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}"))
Beispiel #2
0
def scope():
	scope = ScopeItem.get_scope()
	scopeSize = current_app.ScopeManager.get_scope_size()

	# if it's zero, let's make sure the ScopeManager is up to date
	if scopeSize == 0:
		current_app.ScopeManager.update()
		scopeSize = current_app.ScopeManager.get_scope_size()
		# if it's zero again that's fine, we just had to check

	newForm = forms.NewScopeForm()
	delForm = forms.ScopeDeleteForm()
	editForm = forms.ScopeToggleForm()
	importForm = forms.ImportScopeForm()
	addTagForm = forms.TagScopeForm()
	addTagForm.tagname.choices = [(row.name, row.name) for row in Tag.query.all()]
	if newForm.validate_on_submit():
		if '/' not in newForm.target.data:
			newForm.target.data = newForm.target.data + '/32'
		target = ipaddress.ip_network(newForm.target.data, False)
		newTarget = ScopeItem(target=target.with_prefixlen, blacklist=False)
		db.session.add(newTarget)
		db.session.commit()
		current_app.ScopeManager.update()
		flash('%s added!' % newTarget.target, 'success')
		return redirect(url_for('admin.scope'))
	return render_template(
		"admin/scope.html", scope=scope, scopeSize=scopeSize, delForm=delForm,
		editForm=editForm, newForm=newForm, importForm=importForm, addTagForm=addTagForm)
Beispiel #3
0
def importScope(scopetype=''):
	if scopetype == 'blacklist':
		importBlacklist = True
		importForm = forms.ImportBlacklistForm()
	elif scopetype == 'scope':
		importBlacklist = False
		importForm = forms.ImportScopeForm()
	else:
		abort(404)
	if importForm.validate_on_submit():
		successImports = []
		alreadyExists = []
		failedImports = []
		newScopeItems = importForm.scope.data.split('\n')
		for item in newScopeItems:
			item = item.strip()
			fail, exist, success = ScopeItem.importScope(item, importBlacklist)
			failedImports = failedImports + fail
			alreadyExists = alreadyExists + exist
			successImports = successImports + success
		db.session.commit()
		current_app.ScopeManager.update()
		if len(successImports) > 0:
			flash('%s targets added to %s!' % (len(successImports), scopetype), 'success')
		if len(alreadyExists) > 0:
			flash('%s targets already existed!' % len(alreadyExists), 'info')
		if len(failedImports) > 0:
			flash('%s targets failed to import!' % len(failedImports), 'danger')
			for item in failedImports:
				flash('%s' % item, 'danger')
	else:
		for field, errors in importForm.errors.items():
			for error in errors:
				flash(error, 'danger')
	return redirect(url_for('admin.%s' % scopetype))
Beispiel #4
0
def blacklist():
    render = {
        "scope": ScopeItem.getBlacklist(),
        "blacklistSize": current_app.ScopeManager.get_blacklist_size(),
        "effectiveScopeSize": current_app.ScopeManager.get_effective_scope_size(),
        "newForm": forms.NewScopeForm(),
        "delForm": forms.ScopeDeleteForm(),
        "editForm": forms.ScopeToggleForm(),
        "importForm": forms.ImportScopeForm(),
        "addTagForm": forms.TagScopeForm(),
    }
    render["addTagForm"].tagname.choices = [
        (row.name, row.name) for row in Tag.query.all()
    ]
    if render["newForm"].validate_on_submit():
        target = ipaddress.ip_network(render["newForm"].target.data, False)
        newTarget = ScopeItem(target=target.with_prefixlen, blacklist=True)
        db.session.add(newTarget)
        db.session.commit()
        current_app.ScopeManager.update()
        flash(f"{newTarget.target} blacklisted.", "success")
        return redirect(url_for("admin.blacklist"))
    return render_template("admin/blacklist.html", **render)