Create new modules
Viper in itself is simply a framework, modules are what give it analytical capabilities. We receive and include new modules all the time from contributors, but there are always new features to add. If you have an idea, you should implement a module for it and contribute it back to the community.
The following paragraphs introduce you to the first steps to create a new module.
First thing first, you need to create your .py script under the
modules/ directory: all modules are dynamically loaded by Viper from that folder exclusively. You can create subfolders and place your modules anywhere, Viper will be able to find them.
Any module needs to have some basic attributes that will make it recognizable. It needs to be a Python class inheriting
Module, it needs to have a
description attribute and it needs to have a
run() function. For example the following would be a valid, although not very useful, Viper module:
When a module is invoked from the Viper shell it can be provided with a number of arguments and options. These should be parsed with the python
argparse module as show in the example below.
Using the Config File
Viper provides a config file that will allow you to store user editable sections in a single file rather than inside the modules.
You can easily access the config file:
From here you can access any element in the config file by name:
Using common config settings for outbound http connections
A common use case for modules is to implement the API of an external web service (e.g. https://koodous.com/).
The (great!) requests library (https://github.com/requests/requests/) provides an easy interface for making
outbound http connections.
Viper provides a global configuration section
[http_client] where certain requests options can be set
for Proxies, TLS Verfication, CA_BUNDLE and TLS Client Certificates.
Please check the current
viper.conf.sample for more details.
When implementing a custom module settings from the global
[http_client]] can be overridden by specifying
them again in the configuration section of the custom module and then calling the
method for the custom module configuration section. Example:
Accessing the session
In most cases, you will probably want to execute some analysis function on the currently opened file and in order to do so you'll need to access the session. Sessions are internally made available through a global object called
__sessions__, which has the following attributes:
Sessionobject for the currently opened file.
__sessions__.sessions: the list of all
Sessionobjects opened during the current Viper execution.
__sessions__.find: a list contains all the results from the last executed
Session object has the following attributes:
Session.id: an incremental ID for the session.
Session.created_at: the date and time when the session was opened.
Fileobject containing common attributes of the currently opened file (generally speaking, the same information returned by the
Following are the information available on the opened file:
Here is an example:
Accessing the database
In case you're interested in automatically retreiving all files stored in the local repository or just a subset, you'll need to access the local database. Viper provides an interface called
Database() to be imported from
You can then use the
find() function, specify a key and an optional value and you will obtain a list of objects you can loop through. For example:
Viper provides several function to facilitate and standardize the output of your modules. Viper uses a logging function to return the output to the console or web application.
The format is
self.log('type', "Your Text") and the following types are made available in Viper.
info: prints the message with a
warning: prints the message with a yellow
error: prints the message with a red
success: prints the message with a green
item: prints an item from a list.
table: prints a table with headers and rows.
You can also easily print tables, such as in the following example: