33%{
44#include " libinjection.h"
55#include " libinjection_sqli.h"
6+ #include " libinjection_xss.h"
7+ #include " libinjection_error.h"
68#include < stddef.h>
79
810/* This is the callback function that runs a python function
@@ -13,7 +15,6 @@ static char libinjection_python_check_fingerprint(sfilter* sf, int lookuptype, c
1315 PyObject *fp;
1416 PyObject *arglist;
1517 PyObject *result;
16- const char * strtype;
1718 char ch;
1819
1920 // get sfilter->pattern
@@ -25,14 +26,21 @@ static char libinjection_python_check_fingerprint(sfilter* sf, int lookuptype, c
2526 result = PyObject_CallObject ((PyObject*) sf->userdata , arglist);
2627 Py_DECREF (arglist);
2728 if (result == NULL ) {
28- printf (" GOT NULL\n " );
2929 // python call has an exception
3030 // pass it back
3131 ch = ' \0 ' ;
3232 } else {
33- // convert value of python call to a char
34- strtype = PyString_AsString (result);
35- ch = strtype[0 ];
33+ // convert value of python call to a char (Python 3 compatible)
34+ if (PyUnicode_Check (result)) {
35+ Py_ssize_t size;
36+ const char * str = PyUnicode_AsUTF8AndSize (result, &size);
37+ ch = (str != NULL && size > 0 ) ? str[0 ] : ' \0 ' ;
38+ } else if (PyBytes_Check (result)) {
39+ const char * str = PyBytes_AsString (result);
40+ ch = (str != NULL ) ? str[0 ] : ' \0 ' ;
41+ } else {
42+ ch = ' \0 ' ;
43+ }
3644 Py_DECREF (result);
3745 }
3846 return ch;
@@ -67,6 +75,18 @@ for (i = 0; i < $1_dim0; i++) {
6775
6876// automatically append string length into arg array
6977%apply (char *STRING, size_t LENGTH) { (const char *s, size_t slen) };
78+ %apply (char *STRING, size_t LENGTH) { (const char *s, size_t len) };
79+
80+ // Make the fingerprint output parameter in libinjection_sqli() work as an output
81+ // The fingerprint buffer size matches libinjection's internal LIBINJECTION_SQLI_MAX_TOKENS (5) + null byte
82+ #define LIBINJECTION_FINGERPRINT_SIZE 8
83+ %typemap(in, numinputs=0 ) char fingerprint[] (char temp[LIBINJECTION_FINGERPRINT_SIZE]) {
84+ memset (temp, 0 , sizeof (temp));
85+ $1 = temp;
86+ }
87+ %typemap(argout) char fingerprint[] {
88+ $result = SWIG_Python_AppendOutput ($result, PyUnicode_FromString ($1 ));
89+ }
7090
7191%typemap(in) (ptr_lookup_fn fn, void * userdata) {
7292 if ($input == Py_None) {
@@ -77,5 +97,7 @@ for (i = 0; i < $1_dim0; i++) {
7797 $2 = $input;
7898 }
7999}
100+ %include " libinjection_error.h"
80101%include " libinjection.h"
81102%include " libinjection_sqli.h"
103+ %include " libinjection_xss.h"
0 commit comments