1414extern "C" {
1515#endif
1616
17- /**
18- * Libinjection's sqli module makes a "normalized"
19- * value of the token. This is the maximum size
20- * Token with values larger than this will be truncated
21- */
22- #ifndef LIBINJECTION_SQLI_TOKEN_SIZE
23- #define LIBINJECTION_SQLI_TOKEN_SIZE 32
24- #endif
25-
26- /**
27- * Number of tokens used to create a fingerprint
28- */
29- #ifndef LIBINJECTION_SQLI_MAX_TOKENS
30- #define LIBINJECTION_SQLI_MAX_TOKENS 5
31- #endif
32-
33- #if LIBINJECTION_SQLI_MAX_TOKENS >=8
34- #define LIBINJECTION_SQLI_BUFFER_SZ (LIBINJECTION_SQLI_MAX_TOKENS + 1)
35- #else
36- #define LIBINJECTION_SQLI_BUFFER_SZ 8
37- #endif
38-
39-
40- enum lookup_type {
41- FLAG_NONE = 0 ,
42- FLAG_QUOTE_NONE = 1 <<1 ,
43- FLAG_QUOTE_SINGLE = 1 <<2 ,
44- FLAG_QUOTE_DOUBLE = 1 <<3 ,
45-
46- FLAG_SQL_ANSI = 1 <<4 ,
47- FLAG_SQL_MYSQL = 1 <<5 ,
48-
49- LOOKUP_WORD ,
50- LOOKUP_TYPE ,
51- LOOKUP_OPERATOR ,
52- LOOKUP_FINGERPRINT
53- };
54-
55- struct libinjection_sqli_token {
56- #ifdef SWIG
57- %immutable ;
58- #endif
59- char type ;
60- char str_open ;
61- char str_close ;
62-
63- /*
64- * position and length of token
65- * in original string
66- */
67- size_t pos ;
68- size_t len ;
69-
70- /* count:
71- * in type 'v', used for number of opening '@'
72- * but maybe unsed in other contexts
73- */
74- int count ;
75-
76- char val [LIBINJECTION_SQLI_TOKEN_SIZE ];
77- };
78-
79- typedef struct libinjection_sqli_token stoken_t ;
80-
81- /**
82- * Pointer to function, takes cstr input,
83- * returns '\0' for no match, else a char
17+ /*
18+ * Pull in size_t
8419 */
85- struct libinjection_sqli_state ;
86- typedef char (* ptr_lookup_fn )(struct libinjection_sqli_state * ,int lookuptype ,const char * word ,size_t len );
87-
88- struct libinjection_sqli_state {
89- #ifdef SWIG
90- %immutable ;
91- #endif
92-
93- /*
94- * input, does not need to be null terminated.
95- * it is also not modified.
96- */
97- const char * s ;
98-
99- /*
100- * input length
101- */
102- size_t slen ;
103-
104- /*
105- * How to lookup a word or fingerprint
106- */
107- ptr_lookup_fn lookup ;
108- void * userdata ;
109-
110- /*
111- *
112- */
113- int flags ;
114-
115- /*
116- * pos is index in string we are at when tokenizing
117- */
118- size_t pos ;
119-
120- #ifndef SWIG
121- /* for SWIG.. don't use this.. use functional API instead */
122-
123- /* MAX TOKENS + 1 since we use one extra token
124- * to determine the type of the previous token
125- */
126- struct libinjection_sqli_token tokenvec [LIBINJECTION_SQLI_BUFFER_SZ ];
127- #endif
128-
129- /*
130- * Pointer to token position in tokenvec, above
131- */
132- struct libinjection_sqli_token * current ;
133-
134- /*
135- * fingerprint pattern c-string
136- * +1 for ending null
137- * Mimimum of 8 bytes to add gcc's -fstack-protector to work
138- */
139- char fingerprint [LIBINJECTION_SQLI_BUFFER_SZ ];
140-
141- /*
142- * Line number of code that said decided if the input was SQLi or
143- * not. Most of the time it's line that said "it's not a matching
144- * fingerprint" but there is other logic that sometimes approves
145- * an input. This is only useful for debugging.
146- *
147- */
148- int reason ;
149-
150- /* Number of ddw (dash-dash-white) comments
151- * These comments are in the form of
152- * '--[whitespace]' or '--[EOF]'
153- *
154- * All databases treat this as a comment.
155- */
156- int stats_comment_ddw ;
157-
158- /* Number of ddx (dash-dash-[notwhite]) comments
159- *
160- * ANSI SQL treats these are comments, MySQL treats this as
161- * two unary operators '-' '-'
162- *
163- * If you are parsing result returns FALSE and
164- * stats_comment_dd > 0, you should reparse with
165- * COMMENT_MYSQL
166- *
167- */
168- int stats_comment_ddx ;
169-
170- /*
171- * c-style comments found /x .. x/
172- */
173- int stats_comment_c ;
174-
175- /* '#' operators or mysql EOL comments found
176- *
177- */
178- int stats_comment_hash ;
179-
180- /*
181- * number of tokens folded away
182- */
183- int stats_folds ;
184-
185- /*
186- * total tokens processed
187- */
188- int stats_tokens ;
189-
190- };
191-
192- struct libinjection_sqli_token * libinjection_sqli_get_token (
193- struct libinjection_sqli_state * sqlistate ,int i );
194-
195-
196- typedef struct libinjection_sqli_state sfilter ;
197-
20+ #include <string.h>
19821
19922/*
20023 * Version info.
@@ -207,109 +30,18 @@ typedef struct libinjection_sqli_state sfilter;
20730 * See python's normalized version
20831 * http://www.python.org/dev/peps/pep-0386/#normalizedversion
20932 */
210- const char * libinjection_version ();
211-
212- /**
213- *
214- */
215- void libinjection_sqli_init (struct libinjection_sqli_state * sql_state ,
216- const char * s ,size_t slen ,
217- int flags );
33+ const char * libinjection_version (void );
21834
21935/**
220- * Main API: tests for SQLi in three possible contexts, no quotes,
221- * single quote and double quote
222- *
223- * \param sql_state
224- * \param s
225- * \param slen
226- * \param fn a pointer to a function that determines if a fingerprint
227- * is a match or not. If NULL, then a hardwired list is
228- * used. Useful for loading fingerprints data from custom
229- * sources.
230- *
231- * \return 1 (true) if SQLi, 0 (false) if benign
232- */
233- int libinjection_is_sqli (struct libinjection_sqli_state * sql_state );
234-
235- /* FOR H@CKERS ONLY
236- *
237- */
238- void libinjection_sqli_callback (struct libinjection_sqli_state * sql_state ,
239- ptr_lookup_fn fn ,
240- void * userdata );
241-
242-
243- /*
244- * Resets state, but keeps initial string and callbacks
245- */
246- void libinjection_sqli_reset (struct libinjection_sqli_state * sql_state ,
247- int flags );
248-
249- /**
250- *
251- */
252-
253- /**
254- * This detects SQLi in a single context, mostly useful for custom
255- * logic and debugging.
256- *
257- * \param sql_state
258- *
259- * \returns a pointer to sfilter.fingerprint as convenience
260- * do not free!
261- *
262- */
263- const char * libinjection_sqli_fingerprint (struct libinjection_sqli_state * sql_state ,
264- int flags );
265-
266- /**
267- * The default "word" to token-type or fingerprint function. This
268- * uses a ASCII case-insensitive binary tree.
269- */
270- char libinjection_sqli_lookup_word (struct libinjection_sqli_state * sql_state ,
271- int lookup_type ,
272- const char * s ,
273- size_t slen );
274-
275- /* Streaming tokenization interface.
276- *
277- * sql_state->current is updated with the current token.
278- *
279- * \returns 1, has a token, keep going, or 0 no tokens
280- *
281- */
282- int libinjection_sqli_tokenize (struct libinjection_sqli_state * sql_state );
283-
284- /**
285- * parses and folds input, up to 5 tokens
286- *
287- */
288- int libinjection_sqli_fold (struct libinjection_sqli_state * sql_state );
289-
290- /** The built-in default function to match fingerprints
291- * and do false negative/positive analysis. This calls the following
292- * two functions. With this, you over-ride one part or the other.
293- *
294- * return libinjection_sqli_blacklist(sql_state) &&
295- * libinject_sqli_not_whitelist(sql_state);
296- *
297- * \param sql_state should be filled out after libinjection_sqli_fingerprint is called
298- */
299- int libinjection_sqli_check_fingerprint (struct libinjection_sqli_state * sql_state );
300-
301- /* Given a pattern determine if it's a SQLi pattern.
302- *
303- * \return TRUE if sqli, false otherwise
304- */
305- int libinjection_sqli_blacklist (struct libinjection_sqli_state * sql_state );
306-
307- /* Given a positive match for a pattern (i.e. pattern is SQLi), this function
308- * does additional analysis to reduce false positives.
36+ * Simple API for SQLi detection - returns a SQLi fingerprint or NULL
37+ * is benign input
30938 *
310- * \return TRUE if sqli, false otherwise
39+ * \param[in] s input string, may contain nulls, does not need to be null-terminated
40+ * \param[in] slen input string length
41+ * \param[out] fingerprint buffer of 8+ characters. c-string,
42+ * \return 1 if SQLi, 0 if benign. fingerprint will be set or set to empty string.
31143 */
312- int libinjection_sqli_not_whitelist ( struct libinjection_sqli_state * sql_state );
44+ int libinjection_sqli ( const char * s , size_t slen , char fingerprint [] );
31345
31446#ifdef __cplusplus
31547}