forked from simsong/tcpflow
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement scan_python to process flow by external python script (sim…
…song#164) * Implement scan_python to process flow by external python script Many authors have participed to this effort: - @jakesmo https://github.com/jakesmo - @lassimus https://github.com/lassimus - @olibre https://github.com/olibre The objective is to extend tcpflow using python language. The original work is available on @lassimus' fork: https://github.com/lassimus/tcpflow/commits/master @olibre has continued the work, and has deeply refactored the original source code from @jakesmo and @lassimus. Instead of adding a new option -P, this commit reuses option "-e python" and adds three parameters: - -S py_path=... - -S py_module=... - -S py_function=.... Autotools/Automake files have also been fixed in comparaison of original source code from @jakesmo and @lassimus. CMake files have been updated. For the Autotools/Automake side, the project builds fine with and without the package python-devel. However for CMake build, package python-devel is required This will be improved in a future pull request about CMake. The resulted tcpflow executable have been tested in many ways: - built with and without python-devel installation, - tested with and without options -a, -e python, - tested in situations where parameters were inconsistent - tested with mistakes in parameters - ... There are also some TODOs withing the source code assigned to @simsong: TODO simsong#1 When the scanner cannot initialize it, should we use sp.info->flags = scanner_info::SCANNER_DISABLED? TODO simsong#2 Why PHASE_THREAD_BEFORE_SCAN never called? TODO simsong#3 Similar to TODO simsong#1 This new feature will amplify the possibilities of tcpflow output data processing 😃 * Remplace XML tag <scan_python_result> by <tcpflow:result> For more information, see: dfxml-working-group/dfxml_schema#24 * Fix XML tag name <tcpflow:result> * Avoid symbols "<" and ">" in XML value * Rename XML attribute py_function -> function
- Loading branch information
Showing
12 changed files
with
370 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
*.o | ||
*.so | ||
*.a | ||
*.pyc | ||
|
||
# Packages # | ||
############ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
To execute customizable python plugins: | ||
|
||
1. Check examples in directory `tcpflow/python/plugins`. | ||
|
||
2. Create a python script with the following properties: | ||
|
||
- The script contains one or more functions for tcpflow usage. | ||
- Each intended function must take a single string parameter. | ||
This parameter will hold the contents of the application data captured by tcpflow. | ||
- If an intended function returns, it must return a string, | ||
which will then be added to the report.xml file with the "plugindata" tag. | ||
|
||
3. Execute the `tcpflow` command line with arguments `-e python -S py_path=path -S py_module=module -S py_function=foo`. | ||
Example: | ||
|
||
tcpflow -r my.cap -o flows -e python -S py_path=python/plugins -S py_module=samplePlugin -S py_function=sampleFunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
## Example of a python plugin for tcpflow. | ||
## This sample contains three functions. | ||
|
||
## The first function takes a string and returns a sample message. | ||
## The input string contains the application data from tcpflow's buffer. | ||
|
||
def sampleFunction(appData): | ||
return "This message appears in the XML tag 'tcpflow:result' of report.xml (DFXML)." | ||
|
||
## The second function takes a string (application data) | ||
## and writes the application (HTTP) header data to the file | ||
## myOutput.txt located in the python director. | ||
## This function does not return and simply prints to stdout. | ||
|
||
def headerWriter(appData): | ||
fName = "myOutput.txt" | ||
f = open("python/" + fName, 'a') | ||
headerFinish = appData.find("\r\n\r\n") + 4 | ||
headerData = appData[:headerFinish+1] | ||
f.write(headerData) | ||
f.close() | ||
print "Wrote data to " + fName | ||
|
||
## The third function takes a string (application data) | ||
## parses the HTTP message (without headers) | ||
## performs a bitwise xor operation with a key defined in the function | ||
## and returns the text corresponding to this binary result. | ||
|
||
def xorOp(appData): | ||
# Assume variable buffer includes message data. | ||
dataStart = appData.find("\r\n\r\n") + 4 | ||
httpData = appData[dataStart:] | ||
binaryData = ''.join(format(ord(x), 'b') for x in httpData) | ||
if len(binaryData) < 1: | ||
return 0 | ||
|
||
key = "01101011101" | ||
keyLen = len(key) | ||
newKey = "" | ||
while len(newKey) + keyLen <= len(binaryData): | ||
newKey += key | ||
i = 0 | ||
while len(newKey) < len(binaryData): | ||
if i == keyLen: | ||
i = 0 | ||
newKey += key[i] | ||
i += 1 | ||
xorRes = int(binaryData,2) ^ int(newKey,2) | ||
return '{0:b}'.format(xorRes) |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.