TDS Code Snippets

These TDS Code snippets utilise the default libraries and show some basic functionality in an easy to copy and paste way. Please make sure you are familiar with how the libraries and transforms work. If you are not sure we recommend taking a look at our getting started guide.
The samples are divided between the PHP examples (using the basic PHP library) and Python examples (using the TRX library). Please select below to navigate to that section:

PHP Code Snippets »    Python (TRX) Code Snippets »

 

PHP Code Snippets

 

Hello World [Back to Top]
This transform merely returns "hello world" as a phrase, it doesn't depend on the input entity at all:

<?php
//include our MaltegoTDS class(es)
include_once("Maltego.php");

//set return content-type to be XML
header ("content-type: text/xml");

$maltegoInput = new MaltegoTransformInput();
$maltegoTransform = new MaltegoTransformResponse();
$maltegoTransform->addEntity("maltego.Phrase","Hello Transform World");
$maltegoTransform->returnOutput();

 

Reading Input [Back to Top]
This is simple transform to show reading input from an entity and returning it to the graph (in this case working with a domain and returning a phrase with that domain in it):

<?php
//include our MaltegoTDS class(es)
include_once("Maltego.php");

//set return content-type to be XML
header ("content-type: text/xml");

$maltegoInput = new MaltegoTransformInput();
$maltegoTransform = new MaltegoTransformResponse();


if ($maltegoInput->getEntity())
{
	//get the details into local variables
	$entityValue = $maltegoInput->value;
	$maltegoTransform->addEntity("maltego.Phrase","hello" . $entityValue);
	$maltegoTransform->returnOutput();
}
else
{
	$maltegoTransform->addException("No input entity found");
	$maltegoTransform->throwExceptions();
}	

 

Reading the Slider Value [Back to Top]
This code shows a really simple call to get the slider value:

<?php
//include our MaltegoTDS class(es)
include_once("Maltego.php");

//set return content-type to be XML
header ("content-type: text/xml");

$maltegoInput = new MaltegoTransformInput();
$maltegoTransform = new MaltegoTransformResponse();


if ($maltegoInput->getEntity())
{
	$slider = $maltegoInput->slider;
	echo "slider value is currently: $slider";
	
}
else
{
	$maltegoTransform->addException("No input entity found");
	$maltegoTransform->throwExceptions();
}

 

Reading Transform Settings (aka Popups) [Back to Top]
This example illustrates reading a Popup from the client (popup is setup as seen in the image below):



NOTE: Whether the setting is a popup or not the code will not change, the user will merely not be prompted if it is not set to popup.

<?php
//include our MaltegoTDS class(es)
include_once("Maltego.php");

//set return content-type to be XML
header ("content-type: text/xml");

$maltegoInput = new MaltegoTransformInput();
$maltegoTransform = new MaltegoTransformResponse();


if ($maltegoInput->getEntity())
{
	//fetch the popup value (it is a transform field)
	$enteredName = $maltegoInput->transformFields["popupVar"];
	//return as phrase entity containing the above popup value
	$maltegoTransform->addEntity("maltego.Phrase","hello" . $enteredName);
	$maltegoTransform->returnOutput();
}
else
{
	$maltegoTransform->addException("No input entity found");
	$maltegoTransform->throwExceptions();
}

 

Reading Entity Properties [Back to Top]
This example simply illustrates using the library to read the properties of an entity and printing them out. Note this is just a snippet, not a transform!

<?php
///include our MaltegoTDS class(es)
include_once("Maltego.php");

//set return content-type to be XML
header ("content-type: text/xml");

$maltegoInput = new MaltegoTransformInput();
$maltegoTransform = new MaltegoTransformResponse();


if ($maltegoInput->getEntity())
{
	$entityAdditionalFields = $maltegoInput->additionalFields;
	$longitude = $entityAdditionalFields["longitude"];
	$latitude = $entityAdditionalFields["latitude"];
	echo "$longitude";
	echo "$latitude";
}
else
{
	$maltegoTransform->addException("No input entity found");
	$maltegoTransform->throwExceptions();
}

 

Returning a complex entity [Back to Top]
This transform example shows reading an entity as well as setting properties, additional fields, a UI message and the weight of the entity (run on a domain):

<?php
//include our MaltegoTDS class(es)
include_once("Maltego.php");

//set return content-type to be XML
header ("content-type: text/xml");

$maltegoInput = new MaltegoTransformInput();
$maltegoTransform = new MaltegoTransformResponse();


if ($maltegoInput->getEntity())
{
	$domain = $maltegoInput->value;
	
	$ent = $maltegoTransform->addEntity("maltego.Phrase","Complex." . $domain);
	$ent->setWeight(50);
	$ent->setDisplayInformation("this is a more complex entity addition");
	$ent->setIconURL("http://www.paterva.com/web5/img/chicken.png");
	$ent->setDisplayInformation("

Heading

content here about " . $domain . "!

"); $ent->addAdditionalFields("variable","Display Value","strict",$domain) //return output $maltegoTransform->returnOutput(); } else { $maltegoTransform->addException("No input entity found"); $maltegoTransform->throwExceptions(); }

 

Python TRX Example and API reference [Back to Top]

Here is an example using the Python TRX library. We definitely recommend you have a look at the documentation for the Python TRX library as it has many more examples as well as how to setup your own server from scratch. You can download that here.

 

Returning a complex entity [Back to Top]

This transform shows how to read input, create UI messages, use transform settings. It takes a phrase as input, requires a transform setting called 'ISDIV' and return AS numbers. It also sets the link properties based on the odd/even state of the AS number.

Transform stub (TRX.wsgi):

@route('/EnumAS', method='ANY')
def EnumAS():
    if request.body.len>0:
        return(trx_EnumAS(MaltegoMsg(request.body.getvalue())))

Transform code:

from Maltego import *

# Input -  phrase entity
# Output - AS numbers
# NB: You need ISDIV defined as transform setting

def trx_EnumAS(m):
    # construct a return vessel
    TRX = MaltegoTransform()

    #read the value, make sure its a digit
    if (not m.Value.isdigit()):
        # if not - complain
        TRX.addUIMessage('Sorry but ['+m.Value+'] is not a whole number',UIM_PARTIAL)
        return TRX.returnOutput()

    #read the setting - you need ISDIV defined as transform setting in the TDS
    isdiv = m.getTransformSetting('ISDIV')

    #check if its a digit - else complain even more bitterly
    if (not isdiv.isdigit()):
        TRX.addUIMessage('Silly! We need a number',UIM_FATAL)
        return TRX.returnOutput()

    #here we know we're good to go.
    #read the value of the node
    howmany = int(m.Value);

    # how many have accumulated?
    accum=0;

    for i in range(1,howmany+1):
        if (i % int(isdiv) == 0):

            # add an AS entity with the index as a value...
            Ent = TRX.addEntity('maltego.AS', str(i))

            # ... and set the weight
            Ent.setWeight(howmany-i)

            # add a property called 'div'
            Ent.addProperty('div','Divisible by','strict',str(isdiv))

            # see it's odd or even and set the link/note/bookmark properties
            # this makes for a very ugly graph..but..ya
            if (i%2==0):
                Ent.setLinkColor('0x00FF00')
                Ent.setNote('Even')
                Ent.setLinkLabel('Even link')
                Ent.setLinkStyle(LINK_STYLE_NORMAL)
                Ent.setLinkThickness(1)
                Ent.setBookmark(BOOKMARK_COLOR_GREEN)
            else:
                Ent.setLinkColor('0xFF0000')
                Ent.setNote('Odd')
                Ent.setLinkLabel('Odd link')
                Ent.setLinkStyle(LINK_STYLE_DASHED)
                Ent.setLinkThickness(2)
                Ent.setBookmark(BOOKMARK_COLOR_RED)

            accum=accum+1;
            if accum>=m.Slider:
                break

    # return the XML to the TDS server
    return TRX.returnOutput()

 

TRX Python API Guide

 

MaltegoMsg class [Back to Top]

This is used to read the Maltego request. It is passed along to each transform.

Member Name Description
Value (String) The value of the node as displayed on the graph. Note that this is not necessarily the value you want work with (see URL entity).
Weight (Integer) The weight of the node.
Slider (Integer) The slider's value – e.g. how many results should be returned.
Type (String) The type of the input node. See entity definitions for possible values.
Property (List) A list of properties of the node. Name, value pairs as strings
TransformSettings (List) The settings for the transform. A list of name, value pairs as strings.

The following methods are defined to read entity properties and transform settings. You can read it straight out of the Property and TransformSettings list, but it's nicer to use these functions as they do some error checking:

Member Name Description Returns
getProperty
(String key)
Returns the value of the key, None if not defined Value
getTransformSetting
(String key)
Returns the value of the key, None if not defined Value
 

MaltegoTransform class [Back to Top]

This is used to construct the reply to the TDS. All values are strings.

Method Name Description Returns
addEntity
([String Type,
String Value])
Adds an entity to the return vessel with type 'Type' and value 'Value'. Note that these can be set using functions in MaltegoEntity library. MaltegoEntity
addUIMessage
(String msg,
Const type)
Shows a message 'msg' in the Maltego GUI. Types could be
  • UIM_FATAL (pop up window)
  • UIM_PARTIAL (yellow)
  • UIM_INFORM (default)
  • UIM_DEBUG (light gray)
None
addException
(String msg)
Throws a transform exception None
returnOutput() Returns the XML of the vessel None
 

MaltegoEntity class [Back to Top]

This is the object that defines a single entity within Maltego. An entity can be created using the 'addEntity' method in the MaltegoTransform class.

Method Name Description
setType
(String Type)
Sets the type of the entity – see list of Entity definitions for possible values
setValue
(String Value)
Sets the value of the entity
setWeight
(Integer weight)
Sets the weight of the entity
addDisplayInformation
(String Value,
[String Label])
Adds display information for label named 'Label'. This field is rendered as HTML within Maltego. Default label is 'Info'.
addProperty
(String propertyname,
String displayname,
String matchingrule,
String value)
Adds a property to the entity. Each property has a name, value and a display name. The display name is how it will be represented within Maltego. The matching rule determines how entities will be matched and could be 'strict' (default) or 'loose'
setIconURL
(String URL)
If set it will change the appearance of the icon. The URL should point to a PNG or JPG file. Maltego will size to fit but lots of large files will drain resources.
setLinkColor
(String HexColor)
Sets the color of the link to the node. Colors are in hex –for example '0xff00ff'
setLinkStyle
(Const Style)
Sets the style of the link to the node. The following constants should be used:
  • LINK_STYLE_NORMAL
  • LINK_STYLE_DASHED
  • LINK_STYLE_DOTTED
  • LINK_STYLE_DASHDOT
setLinkThickness
(Integer thickness)
Sets the thickness of the link to the node. Value is in pixels.
setLinkLabel
(String value)
Sets the label of the link to the node.
setBookmark
(Const Color)
Sets the bookmark color of the node. Keep in mind that these are chosen from a set number of colors. Use the following constants:
  • BOOKMARK_COLOR_NONE
  • BOOKMARK_COLOR_BLUE
  • BOOKMARK_COLOR_GREEN
  • BOOKMARK_COLOR_YELLOW
  • BOOKMARK_COLOR_ORANGE
  • BOOKMARK_COLOR_RED
setNote
(String note)
Creates an annotation to the node with value 'note'. If a subsequent transform sets an annotation on the node it will appended to the note.