Giter Site home page Giter Site logo

ovh / cerberus-core Goto Github PK

View Code? Open in Web Editor NEW
14.0 8.0 4.0 14.16 MB

Cerberus is a toolkit to receive, parse, process and automate abuse reports handling received by ISP or hosting providers.

License: GNU General Public License v3.0

Python 99.85% Makefile 0.15%

cerberus-core's Introduction

cerberus-core

Summary

Cerberus is a toolkit to receive, parse, process and automate abuse reports handling received by ISP or hosting providers. This toolkit includes an email fetcher, parsers, a business rules engine and a ticketing system.

cerberus

Setup

This project is mainly based on Django, Flask, RQ and Rq-Scheduler. To setup cerberus-core, you'll need:

  • A Linux environment
  • Python 2.7+
  • A PostgreSQL database (9.4.x or greater)
  • A Redis server

When all of these requirements are met, you can checkout the sources and install Cerberus:

$ virtualenv venv
$ source venv/bin/activate
$ pip install --upgrade pip setuptools
$ pip install -r requirements.txt

Running

$ python main.py

Usage: main.py [OPTIONS] COMMAND [ARGS]...

  Cerberus CLI

Options:
  --version  Show the flask version
  --help     Show this message and exit.

Commands:

  add-rule         Add or update a Cerberus rule from file.
  fetch-email      Runs Cerberus email fetcher.
  initdb           Init Cerberus database.
  run              Runs a development server.
  run-worker       Runs a Cerberus worker.
  shell            Runs a shell in the app context.
  test             Runs tests.
  ticket-workflow  Runs Cerberus ticket workflow.

First, you need to init the database

$ APP_ENV=dev APP_SETTINGS=settings-sample.yml python main.py initdb

Check if database is correctly populated

$ APP_ENV=dev APP_SETTINGS=settings-sample.yml python main.py shell

In [4]: from abuse.models import Category
In [5]: list(Category.all().values_list('name', flat=True))
Out[5]:
[u'Copyright',
 u'Illegal',
 u'Intrusion',
 u'Malware',
 u'Network Attack',
 u'Other',
 u'Phishing',
 u'Spam']

To run Cerberus, you need at least to run these main.py commands:

  • run for the API
  • run-worker --queues default,email to run a worker
  • fetch-email to fetch abuse emails
  • ticket-workflow to update tickets status

The whole project uses python-rq and rq-scheduler. You can see jobs status with:

$ rq-info

Start the scheduler:

$ rqscheduler &

Overview

alt text

Business rules ? (aka workflows)

A rule contains conditions and actions. If all conditions are met, then actions are executed sequentially.

Cerberus business rules engine is based on https://github.com/venmo/business-rules You can find business rules example in abuse/rules/definitions.

Phishing rule example:

config:
    actions:
    -   name: set_ticket_phishtocheck
    conditions:
        all:
        -   name: has_defendant
            operator: is_true
            value: true
        -   name: report_category
            operator: equal_to
            value: phishing
        -   name: urls_down
            operator: is_false
            value: true
        -   name: has_urls
            operator: is_true
            value: true
name: phishing_phishtocheck
orderId: 22 # lower number means higher priority
rulesType: Report

Implementing your own business rules functions

You can add your own business rules variables and actions. Only requirement is that a function's name need to be unique. For example, in abuse.rules.variables.report, add myvar.py:

from ...engine.variables import boolean_rule_variable, BaseVariables

class MyVariables(BaseVariables):
    """
        This class implements My variables getters
        for rules engine
    """
    def __init__(self, parsed_email, report, ticket, is_trusted=False):
        """
            :param `cerberus.parsers.ParsedEmail` parsed_email: The parsed email
            :param `abuse.models.Report` report: A Cerberus report instance
            :param `abuse.models.Ticket` ticket: A Cerberus ticket instance
            :param bool is_trusted: if the report is trusted
        """
        self.report = report
        self.ticket = ticket

    @boolean_rule_variable(label='Check if report category is Phishing')
    def is_report_phishing(self):

        return self.report.category.lower() == 'phishing'

Now you can register variables definitions in your settings:

RULES:
    variables:
        report:
            - 'abuse.rules.variables.report.default.DefaultReportVariables'
            - 'abuse.rules.variables.report.myvar.MyVariables'

Customisation

Implementing your own Services

Cerberus uses many services. You can rewrite/override default implementation of services. Required services implementations are:

  • abuse.services.action.ActionServiceBase
  • abuse.services.crm.base.CRMServiceBase
  • abuse.services.email.base.EmailServiceBase
  • abuse.services.phishing.base.PhishingServiceBase
  • abuse.services.storage.base.StorageServiceBase

Optional, but usefull, are:

  • abuse.services.kpi.base.KPIServiceBase
  • abuse.services.reputation.base.ReputationServiceBase
  • abuse.services.search.base.SearchServiceBase

Then, tell Cerberus to use this implementation by editing IMPLEMENTATIONS in your settings file.

IMPLEMENTATIONS:
    EmailServiceBase:
        class: 'abuse.services.email.myemailservice.MyEmailService'

Services are called through a singleton for each service. For example:

from abuse.services.email import EmailService

emails = EmailService.get_emails(ticket)

will now execute your own get_emails implementation.

Implementing your own async tasks

For example if you insert rq jobs from external API, Kafka messages ...

Simply put source files in abuse.tasks.

$ tree abuse/tasks/
	abuse/tasks/
	├── __init__.py
	├── action.py
	├── defendant.py
	├── helpers.py
	├── masscontact.py
	├── **mytasks.py**
	├── phishing.py
	├── report.py
	└── ticket.py

Project structure

  • abuse/api: Cerberus API for cerberus-ux.
  • abuse/controllers: API controllers.
  • abuse/commands: Cerberus main commands (run server, workers ...).
  • abuse/models: Django app models description.
  • abuse/parsers: email parsing engine and templates.
  • abuse/rules: business rules engine and rules definitions.
  • abuse/services: services used by Cerberus core functions.
  • abuse/tasks: tasks handled by python-rq workers.
  • abuse/utils: some utils functions.

Definitions

  • Plaintiff

(Legal) A person who brings a civil action in a court (aka claimant).

  • Defendant

(Legal) A person or entity against whom an action or claim is brought in a court of law. It is one of your customer who is suspected of illegal activities on his service(s).

  • Service

A Defendant has suscribed to one or more services(s) (product(s)) your company offers. It can be ADSL, hosting, email services ...

  • Provider

The source of the email. It can be directly the plaintiff or an representative third party.

  • Report

One Provider + one Category. If a defendant is identified, the report is linked to a defendant, a service and contains item(s).

An email can generates one or more reports (if multiple defendants are identified). These reports can be attached to existing ticket, create one or just do nothing (depending on rules you have defined). So tickets can have multiple reports/providers.

So not all reports are attached to tickets. It's important, operators process tickets, not reports.

All the effective jobs are done with ticket: customer interaction (emails), action on service ...

  • Ticket

One or more reports + one Category. It cans bel linked to a defendant/service, so all reports themselves linked to this defendant/service.

  • Item

Fraudulent URL/IP/FQDN found in an email.

Tests

Common tests:

$APP_ENV=test APP_SETTINGS=abuse/tests/settings-test.yml python main.py test --pattern="test_*"

cerberus-core's People

Contributors

pandipanda69 avatar simon-vasseur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cerberus-core's Issues

error buidling with docker

using a newer docker version, trying to build it following the doc with:
how can I fix that?

$ docker build -t cerberus .

resulting into the following error.

Failed building wheel for uwsgi
[...]

*** uWSGI compiling embedded plugins ***
[thread 0][x86_64-linux-gnu-gcc -pthread] plugins/corerouter/cr_map.o
[thread 1][x86_64-linux-gnu-gcc -pthread] plugins/corerouter/corerouter.o
plugins/corerouter/cr_map.c: In function 'uwsgi_cr_map_use_static_nodes':
plugins/corerouter/cr_map.c:154:5: error: this 'if' clause does not guard... [-Werror=misleading-indentation]
if (!next_node)
^~
plugins/corerouter/cr_map.c:157:6: note: ...this statement, but the latter is misleadingly indented as if it is guarded by the 'if'
while (tmp_node != next_node) {
^~~~~
cc1: all warnings being treated as errors

----------------------------------------

Command "/usr/bin/python -u -c "import setuptools, tokenize;file='/tmp/pip-build-GppuyU/uwsgi/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-wBVNLF-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-GppuyU/uwsgi/
The command '/bin/sh -c mkdir cerberus-core && cd cerberus-core && mv /abuse . && mv /adapters . && mv /api . && mv /config . && mv /default . && mv /event . && mv /factory . && mv /manage.py . && mv /requirements . && mv /requirements.txt . && mv /utils . && mv /worker . && cp abuse/fixtures/data.json abuse/fixtures/initial_data.json && chown -R cerberus:cerberus /home/cerberus/cerberus-core && pip install -r requirements/dev.txt && ln -s /etc/supervisor/supervisord.conf /etc/supervisord.conf && mkdir ../cerberus-ux && cd ../cerberus-ux && tar xvzf /cerberus-ux.tar.gz && chown -R www-data:www-data /home/cerberus/cerberus-ux/client && sed -i 's/80/6060/g' /etc/apache2/ports.conf && apt-get clean && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 1

Connecting Cerberus-UX with the Backend

there where some smaller dependency issues and the app.less wasnt compiled right to app.css

testing with the express server of cerberus-ux trying to use the backend, im getting the following error:

$ npm start

[email protected] start /home/roland/cerberus-ux
node server

Express server listening on 9000, in development mode
[REMOTE] POST /api/auth
Error: Invalid URI "/api/auth"
at Request.init (/home/roland/cerberus-ux/node_modules/request/request.js:273:31)
at new Request (/home/roland/cerberus-ux/node_modules/request/request.js:127:8)
at request (/home/roland/cerberus-ux/node_modules/request/index.js:53:10)
at /home/roland/cerberus-ux/server/proxy/remote/index.js:12:13
at Layer.handle [as handle_request] (/home/roland/cerberus-ux/node_modules/express/lib/router/layer.js:95:5)
at next (/home/roland/cerberus-ux/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/roland/cerberus-ux/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/roland/cerberus-ux/node_modules/express/lib/router/layer.js:95:5)
at /home/roland/cerberus-ux/node_modules/express/lib/router/index.js:281:22
at param (/home/roland/cerberus-ux/node_modules/express/lib/router/index.js:354:14)
POST /api/auth 500 12.408 ms - -
[REMOTE] POST /api/logout
Error: Invalid URI "/api/logout"
at Request.init (/home/roland/cerberus-ux/node_modules/request/request.js:273:31)
at new Request (/home/roland/cerberus-ux/node_modules/request/request.js:127:8)
at request (/home/roland/cerberus-ux/node_modules/request/index.js:53:10)
at /home/roland/cerberus-ux/server/proxy/remote/index.js:12:13
at Layer.handle [as handle_request] (/home/roland/cerberus-ux/node_modules/express/lib/router/layer.js:95:5)
at next (/home/roland/cerberus-ux/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/roland/cerberus-ux/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/roland/cerberus-ux/node_modules/express/lib/router/layer.js:95:5)
at /home/roland/cerberus-ux/node_modules/express/lib/router/index.js:281:22
at param (/home/roland/cerberus-ux/node_modules/express/lib/router/index.js:354:14)
POST /api/logout 500 1.537 ms - -

using an apache2 and putting dist/client/ into var/www wasnt helping

how to connect it?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.