Stoq

Overview

The Stoq class is the core of the framework. It must be instantiated in order for all other modules to function properly. This class is meant to be called from stoq.py.

Upon instantiation, default configuration options are defined within __init__. These are overridden if there is identical configuration option in stoq.cfg.

The StoqPluginManager will also be instantiated as a child class automatically. This allows for the ability to globally access the API for plugins and easily grant the ability for plugins to load other plugins.

Examples

Instantiate the Stoq class:

from stoq.core import Stoq
stoq = Stoq()

Retrieve a file from a url:

content = stoq.get_file("http://google.com")

Write content to disk:

stoq.write("example content", path="/tmp", filename="example.txt")

Note

If no filename is given, Stoq.get_uuid will be called and a random filename will be defined automatically. Additionally, if the filename already exists, the file will not be overwritten. However, if Stoq.write() is called with overwrite=True, the file will be overwritten. If the content to be written is binary, one may add binary=True when calling Stoq.write().

API

class stoq.core.Stoq(argv=None, base_dir=None)

Core stoQ Framework Class

dumps(data, compactly=False)

Wrapper for json library. Dump dict to a json string

Parameters:
  • data (dict) – Python dict to convert to json
  • compactly – set to True to return unindented JSON (no newlines between key/values),
Returns:

Converted json string

Return type:

str

force_unicode(payload)

Force a string to be properly encoded in unicode using BeautifulSoup4

Parameters:payload (bytes) – String to be forced into unicode
Returns:Unicode bytes
Return type:bytes
get_file(source, params=None, verify=True, auth=None, **kwargs)

Obtain contents of file from disk or URL.

Note

A file will only be opened from disk if the path of the file matches the regex defined by source_base_tuple in stoq.cfg.

Parameters:
  • source (bytes) – Path or URL of file to read.
  • params (bytes) – Additional parameters to pass if requesting a URL
  • verify (bool) – Ensure SSL Certification Verification
  • auth – Authentication methods supported by python-requests
  • **kwargs – Additional HTTP headers
Returns:

Content of file retrieved

Return type:

bytes or None

get_time

Get the current time, in ISO format

Returns:Current time in ISO Format
Return type:str
get_uuid

Generate a random uuid

Returns:Random uuid
Return type:str
hashpath(sha1)

Generate a path based on the first five chars of a SHA1 hash

example: The SHA1 4caa16eba080d3d4937b095fb68999f3dbabd99d would return a path similar to: /opt/malware/4/c/a/a/1

Parameters:sha1 (str) – SHA1 hash of a payload
Returns:Path
Return type:str
load_config()

Load configuration file. Defaults to stoq.cfg.

loads(data)

Wrapper for json library. Load json string as a python dict

Parameters:data (str) – json string to load into dict
Returns:Converted dict
Return type:dict
logger_init()

Initialize the logger globally.

Returns:True
post_file(url, params=None, files=None, data=None, auth=None, **kwargs)

Handles POST request to specified URL

Parameters:
  • url (bytes) – URL to for POST request
  • params (bytes) – Additional parameters to pass if requesting a URL
  • files (tuple) – Tuple of file data to POST
  • data (bytes) – Content to POST
  • auth – Authentication methods supported by python-requests
  • **kwargs – Additional HTTP headers
Returns:

Content returned from POST request

Return type:

bytes or None

put_file(url, params=None, data=None, auth=None, **kwargs)

Handles PUT request to specified URL

Parameters:
  • url (bytes) – URL to for PUT request
  • params (bytes) – Additional parameters to pass if requesting a URL
  • data (bytes) – Content to PUT
  • auth – Authentication methods supported by python-requests
  • **kwargs – Additional HTTP headers
Returns:

Content returned from PUT request

Return type:

bytes or None

write(payload, filename=None, path=None, binary=False, overwrite=False, append=False)

Write content to disk

Parameters:
  • payload (str) – Data to be written to disk
  • filename (str) – Filename, if none is provided, a random filename will be used
  • path (str) – Path for output file
  • binary (bool) – Define whether content is binary or not
  • overwrite (bool) – Define whether output file should be overwritten
  • append (bool) – Define whether output file should be appended to
Returns:

Full path of file that was written

Return type:

str or False