Command-Line Scripting in Cerbero Suite

The basic syntax to execute a script from the command line is the following:

# Windows
cerpro.exe -r foo.py
# Linux
./cerpro.sh -r foo.py
# macOS
./cerpro -r foo.py

It is also possible to directly execute a Python statement from the command line:

cerpro.exe -e "print('Hello, world!')"

To execute a specific function inside of a script the syntax is:

cerpro.exe -r foo.py:bar

This calls the function bar() inside of the script ‘foo.py’.

The syntax to specify the path of the script in conjunction with the function name is:

cerpro.exe -r "path/to/foo.py":bar

Everything following the script/function is passed on as string arguments to the script/function itself.

cerpro.exe -r foo.py these "are arguments" for the script

When a function isn’t specified, the arguments can be retrieved from sys.argv.

import sys

print(sys.argv)

The output in this case is:

['script/path/foo.py', 'these', 'are arguments', 'for', 'the', 'script']

When a function is specified, the number of arguments are passed on to the function directly:

# cerpro.exe -r foo.py:sum 1 2

def sum(a, b):
    print(int(a) + int(b))

In these examples, Cerbero Suite opens its main window and sets the focus to the output view. The reason for this behaviour is that the command-line support also permits to instrument the UI from the command line.

If console output is desired, the ‘-c’ argument must be specified:

cerpro.exe -c -r foo.py:sum 1 2

This additional argument must specified before ‘-r’, since otherwise it would be consumed as an argument for the script.

Important

On Windows running scripts with the ‘-c’ argument results in not being able to see the stdout output. The reason for this is that the cerpro executable is built as a GUI application and therefore is not attached to a terminal.

To overcome this limitation we have added a launcher on Windows called “cerpro_console.exe”.

For example:

cerpro_console.exe -e "t=input('Enter a string: ');print(t)"

The code prompts the user to enter a string and prints it back.

Alternatively, the creation of a main window can be avoided using the ‘-g’ argument:

cerpro.exe -g -r foo.py

By specifying the ‘-g’ argument, the application is launched in UI mode but without creating a main window. This option is intended to be used to execute scripts which create their own UI.

In this case, if the script doesn’t create an output view, the output of the print() function won’t be shown to the user.

The following script prints out the import descriptors of a Portable Executable (PE):

from Pro.Core import *
from Pro.PE import *

def printImports(fname):
    c = createContainerFromFile(fname)
    pe = PEObject()
    if not pe.Load(c):
        return
    it = pe.ImportDescriptors().iterator()
    while it.hasNext():
        descr = it.next()
        offs = pe.RvaToOffset(descr.Num("Name"))
        name, ret = pe.ReadUInt8String(offs, 400)
        if ret:
            print(name.decode("ascii"))

The script can be executed with the following command line:

cerpro_console.exe -r peutil.py:printImports C:\Windows\regedit.exe

And produces the following output:

ADVAPI32.dll
KERNEL32.dll
GDI32.dll
USER32.dll
msvcrt.dll
api-ms-win-core-path-l1-1-0.dll
SHLWAPI.dll
COMCTL32.dll
COMDLG32.dll
SHELL32.dll
AUTHZ.dll
ACLUI.dll
ole32.dll
ulib.dll
clb.dll
ntdll.dll
UxTheme.dll

Another important part of the command-line support is the capability to register logic providers on the fly. Which means that it is possible to configure a custom scan logic from the command line.

from Pro.Core import *
import sys

def init():
    proCoreContext().getSystem().addFile(sys.argv[1])
    return True

def end(ud):
    pass

def scanning(sp, ud):
    pass

def scanned(sp, ud):
    pass

def rload():
    proCoreContext().unregisterLogicProvider("test_logic")

def main():
    ctx = proCoreContext()
    ctx.registerLogicProvider("test_logic", init, end, scanning, scanned, rload)
    ctx.startScan("test_logic")

main()

This script scans a single file passed to it as argument. All callbacks, aside from init(), are optional.

Note

When performing scan operations the ‘-g’ argument cannot be specified, since a main window must be created.