-
Notifications
You must be signed in to change notification settings - Fork 274
/
libinjection_sqli.c
2325 lines (2107 loc) · 70.4 KB
/
libinjection_sqli.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright 2012,2016 Nick Galbreath
* BSD License -- see COPYING.txt for details
*
* https://libinjection.client9.com/
*
*/
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <assert.h>
#include <stddef.h>
#include "libinjection.h"
#include "libinjection_sqli.h"
#include "libinjection_sqli_data.h"
#define LIBINJECTION_VERSION "3.9.2"
#define LIBINJECTION_SQLI_TOKEN_SIZE sizeof(((stoken_t*)(0))->val)
#define LIBINJECTION_SQLI_MAX_TOKENS 5
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define CHAR_NULL '\0'
#define CHAR_SINGLE '\''
#define CHAR_DOUBLE '"'
#define CHAR_TICK '`'
/* faster than calling out to libc isdigit */
#define ISDIGIT(a) ((unsigned)((a) - '0') <= 9)
#if 0
#define FOLD_DEBUG printf("%d \t more=%d pos=%d left=%d\n", __LINE__, more, (int)pos, (int)left);
#else
#define FOLD_DEBUG
#endif
/*
* not making public just yet
*/
typedef enum {
TYPE_NONE = 0
, TYPE_KEYWORD = (int)'k'
, TYPE_UNION = (int)'U'
, TYPE_GROUP = (int)'B'
, TYPE_EXPRESSION = (int)'E'
, TYPE_SQLTYPE = (int)'t'
, TYPE_FUNCTION = (int)'f'
, TYPE_BAREWORD = (int)'n'
, TYPE_NUMBER = (int)'1'
, TYPE_VARIABLE = (int)'v'
, TYPE_STRING = (int)'s'
, TYPE_OPERATOR = (int)'o'
, TYPE_LOGIC_OPERATOR = (int)'&'
, TYPE_COMMENT = (int)'c'
, TYPE_COLLATE = (int)'A'
, TYPE_LEFTPARENS = (int)'('
, TYPE_RIGHTPARENS = (int)')' /* not used? */
, TYPE_LEFTBRACE = (int)'{'
, TYPE_RIGHTBRACE = (int)'}'
, TYPE_DOT = (int)'.'
, TYPE_COMMA = (int)','
, TYPE_COLON = (int)':'
, TYPE_SEMICOLON = (int)';'
, TYPE_TSQL = (int)'T' /* TSQL start */
, TYPE_UNKNOWN = (int)'?'
, TYPE_EVIL = (int)'X' /* unparsable, abort */
, TYPE_FINGERPRINT = (int)'F' /* not really a token */
, TYPE_BACKSLASH = (int)'\\'
} sqli_token_types;
/**
* Initializes parsing state
*
*/
static char flag2delim(int flag)
{
if (flag & FLAG_QUOTE_SINGLE) {
return CHAR_SINGLE;
} else if (flag & FLAG_QUOTE_DOUBLE) {
return CHAR_DOUBLE;
} else {
return CHAR_NULL;
}
}
/* memchr2 finds a string of 2 characters inside another string
* This a specialized version of "memmem" or "memchr".
* 'memmem' doesn't exist on all platforms
*
* Porting notes: this is just a special version of
* astring.find("AB")
*
*/
static const char *
memchr2(const char *haystack, size_t haystack_len, char c0, char c1)
{
const char *cur = haystack;
const char *last = haystack + haystack_len - 1;
if (haystack_len < 2) {
return NULL;
}
while (cur < last) {
/* safe since cur < len - 1 always */
if (cur[0] == c0 && cur[1] == c1) {
return cur;
}
cur += 1;
}
return NULL;
}
/**
* memmem might not exist on some systems
*/
static const char *
my_memmem(const char* haystack, size_t hlen, const char* needle, size_t nlen)
{
const char* cur;
const char* last;
assert(haystack);
assert(needle);
assert(nlen > 1);
last = haystack + hlen - nlen;
for (cur = haystack; cur <= last; ++cur) {
if (cur[0] == needle[0] && memcmp(cur, needle, nlen) == 0) {
return cur;
}
}
return NULL;
}
/** Find largest string containing certain characters.
*
* C Standard library 'strspn' only works for 'c-strings' (null terminated)
* This works on arbitrary length.
*
* Performance notes:
* not critical
*
* Porting notes:
* if accept is 'ABC', then this function would be similar to
* a_regexp.match(a_str, '[ABC]*'),
*/
static size_t
strlenspn(const char *s, size_t len, const char *accept)
{
size_t i;
for (i = 0; i < len; ++i) {
/* likely we can do better by inlining this function
* but this works for now
*/
if (strchr(accept, s[i]) == NULL) {
return i;
}
}
return len;
}
static size_t
strlencspn(const char *s, size_t len, const char *accept)
{
size_t i;
for (i = 0; i < len; ++i) {
/* likely we can do better by inlining this function
* but this works for now
*/
if (strchr(accept, s[i]) != NULL) {
return i;
}
}
return len;
}
static int char_is_white(char ch) {
/* ' ' space is 0x32
'\t 0x09 \011 horizontal tab
'\n' 0x0a \012 new line
'\v' 0x0b \013 vertical tab
'\f' 0x0c \014 new page
'\r' 0x0d \015 carriage return
0x00 \000 null (oracle)
0xa0 \240 is Latin-1
*/
return strchr(" \t\n\v\f\r\240\000", ch) != NULL;
}
/* DANGER DANGER
* This is -very specialized function-
*
* this compares a ALL_UPPER CASE C STRING
* with a *arbitrary memory* + length
*
* Sane people would just make a copy, up-case
* and use a hash table.
*
* Required since libc version uses the current locale
* and is much slower.
*/
static int cstrcasecmp(const char *a, const char *b, size_t n)
{
char cb;
for (; n > 0; a++, b++, n--) {
cb = *b;
if (cb >= 'a' && cb <= 'z') {
cb -= 0x20;
}
if (*a != cb) {
return *a - cb;
} else if (*a == '\0') {
return -1;
}
}
return (*a == 0) ? 0 : 1;
}
/**
* Case sensitive string compare.
* Here only to make code more readable
*/
static int streq(const char *a, const char *b)
{
return strcmp(a, b) == 0;
}
/**
*
*
*
* Porting Notes:
* given a mapping/hash of string to char
* this is just
* typecode = mapping[key.upper()]
*/
static char bsearch_keyword_type(const char *key, size_t len,
const keyword_t * keywords, size_t numb)
{
size_t pos;
size_t left = 0;
size_t right = numb - 1;
while (left < right) {
pos = (left + right) >> 1;
/* arg0 = upper case only, arg1 = mixed case */
if (cstrcasecmp(keywords[pos].word, key, len) < 0) {
left = pos + 1;
} else {
right = pos;
}
}
if ((left == right) && cstrcasecmp(keywords[left].word, key, len) == 0) {
return keywords[left].type;
} else {
return CHAR_NULL;
}
}
static char is_keyword(const char* key, size_t len)
{
return bsearch_keyword_type(key, len, sql_keywords, sql_keywords_sz);
}
/* st_token methods
*
* The following functions manipulates the stoken_t type
*
*
*/
static void st_clear(stoken_t * st)
{
memset(st, 0, sizeof(stoken_t));
}
static void st_assign_char(stoken_t * st, const char stype, size_t pos, size_t len,
const char value)
{
/* done to eliminate unused warning */
(void)len;
st->type = (char) stype;
st->pos = pos;
st->len = 1;
st->val[0] = value;
st->val[1] = CHAR_NULL;
}
static void st_assign(stoken_t * st, const char stype,
size_t pos, size_t len, const char* value)
{
const size_t MSIZE = LIBINJECTION_SQLI_TOKEN_SIZE;
size_t last = len < MSIZE ? len : (MSIZE - 1);
st->type = (char) stype;
st->pos = pos;
st->len = last;
memcpy(st->val, value, last);
st->val[last] = CHAR_NULL;
}
static void st_copy(stoken_t * dest, const stoken_t * src)
{
memcpy(dest, src, sizeof(stoken_t));
}
static int st_is_arithmetic_op(const stoken_t* st)
{
const char ch = st->val[0];
return (st->type == TYPE_OPERATOR && st->len == 1 &&
(ch == '*' || ch == '/' || ch == '-' || ch == '+' || ch == '%'));
}
static int st_is_unary_op(const stoken_t * st)
{
const char* str = st->val;
const size_t len = st->len;
if (st->type != TYPE_OPERATOR) {
return FALSE;
}
switch (len) {
case 1:
return *str == '+' || *str == '-' || *str == '!' || *str == '~';
case 2:
return str[0] == '!' && str[1] == '!';
case 3:
return cstrcasecmp("NOT", str, 3) == 0;
default:
return FALSE;
}
}
/* Parsers
*
*
*/
static size_t parse_white(struct libinjection_sqli_state * sf)
{
return sf->pos + 1;
}
static size_t parse_operator1(struct libinjection_sqli_state * sf)
{
const char *cs = sf->s;
size_t pos = sf->pos;
st_assign_char(sf->current, TYPE_OPERATOR, pos, 1, cs[pos]);
return pos + 1;
}
static size_t parse_other(struct libinjection_sqli_state * sf)
{
const char *cs = sf->s;
size_t pos = sf->pos;
st_assign_char(sf->current, TYPE_UNKNOWN, pos, 1, cs[pos]);
return pos + 1;
}
static size_t parse_char(struct libinjection_sqli_state * sf)
{
const char *cs = sf->s;
size_t pos = sf->pos;
st_assign_char(sf->current, cs[pos], pos, 1, cs[pos]);
return pos + 1;
}
static size_t parse_eol_comment(struct libinjection_sqli_state * sf)
{
const char *cs = sf->s;
const size_t slen = sf->slen;
size_t pos = sf->pos;
const char *endpos =
(const char *) memchr((const void *) (cs + pos), '\n', slen - pos);
if (endpos == NULL) {
st_assign(sf->current, TYPE_COMMENT, pos, slen - pos, cs + pos);
return slen;
} else {
st_assign(sf->current, TYPE_COMMENT, pos, (size_t)(endpos - cs) - pos, cs + pos);
return (size_t)((endpos - cs) + 1);
}
}
/** In ANSI mode, hash is an operator
* In MYSQL mode, it's a EOL comment like '--'
*/
static size_t parse_hash(struct libinjection_sqli_state * sf)
{
sf->stats_comment_hash += 1;
if (sf->flags & FLAG_SQL_MYSQL) {
sf->stats_comment_hash += 1;
return parse_eol_comment(sf);
} else {
st_assign_char(sf->current, TYPE_OPERATOR, sf->pos, 1, '#');
return sf->pos + 1;
}
}
static size_t parse_dash(struct libinjection_sqli_state * sf)
{
const char *cs = sf->s;
const size_t slen = sf->slen;
size_t pos = sf->pos;
/*
* five cases
* 1) --[white] this is always a SQL comment
* 2) --[EOF] this is a comment
* 3) --[notwhite] in MySQL this is NOT a comment but two unary operators
* 4) --[notwhite] everyone else thinks this is a comment
* 5) -[not dash] '-' is a unary operator
*/
if (pos + 2 < slen && cs[pos + 1] == '-' && char_is_white(cs[pos+2]) ) {
return parse_eol_comment(sf);
} else if (pos +2 == slen && cs[pos + 1] == '-') {
return parse_eol_comment(sf);
} else if (pos + 1 < slen && cs[pos + 1] == '-' && (sf->flags & FLAG_SQL_ANSI)) {
/* --[not-white] not-white case:
*
*/
sf->stats_comment_ddx += 1;
return parse_eol_comment(sf);
} else {
st_assign_char(sf->current, TYPE_OPERATOR, pos, 1, '-');
return pos + 1;
}
}
/** This detects MySQL comments, comments that
* start with /x! We just ban these now but
* previously we attempted to parse the inside
*
* For reference:
* the form of /x![anything]x/ or /x!12345[anything] x/
*
* Mysql 3 (maybe 4), allowed this:
* /x!0selectx/ 1;
* where 0 could be any number.
*
* The last version of MySQL 3 was in 2003.
* It is unclear if the MySQL 3 syntax was allowed
* in MySQL 4. The last version of MySQL 4 was in 2008
*
*/
static size_t is_mysql_comment(const char *cs, const size_t len, size_t pos)
{
/* so far...
* cs[pos] == '/' && cs[pos+1] == '*'
*/
if (pos + 2 >= len) {
/* not a mysql comment */
return 0;
}
if (cs[pos + 2] != '!') {
/* not a mysql comment */
return 0;
}
/*
* this is a mysql comment
* got "/x!"
*/
return 1;
}
static size_t parse_slash(struct libinjection_sqli_state * sf)
{
const char* ptr;
size_t clen;
const char *cs = sf->s;
const size_t slen = sf->slen;
size_t pos = sf->pos;
const char* cur = cs + pos;
char ctype = TYPE_COMMENT;
size_t pos1 = pos + 1;
if (pos1 == slen || cs[pos1] != '*') {
return parse_operator1(sf);
}
/*
* skip over initial '/x'
*/
ptr = memchr2(cur + 2, slen - (pos + 2), '*', '/');
/*
* (ptr == NULL) causes false positive in cppcheck 1.61
* casting to type seems to fix it
*/
if (ptr == (const char*) NULL) {
/* till end of line */
clen = slen - pos;
} else {
clen = (size_t)(ptr + 2 - cur);
}
/*
* postgresql allows nested comments which makes
* this is incompatible with parsing so
* if we find a '/x' inside the coment, then
* make a new token.
*
* Also, Mysql's "conditional" comments for version
* are an automatic black ban!
*/
if (memchr2(cur + 2, (size_t)(ptr - (cur + 1)), '/', '*') != NULL) {
ctype = TYPE_EVIL;
} else if (is_mysql_comment(cs, slen, pos)) {
ctype = TYPE_EVIL;
}
st_assign(sf->current, ctype, pos, clen, cs + pos);
return pos + clen;
}
static size_t parse_backslash(struct libinjection_sqli_state * sf)
{
const char *cs = sf->s;
const size_t slen = sf->slen;
size_t pos = sf->pos;
/*
* Weird MySQL alias for NULL, "\N" (capital N only)
*/
if (pos + 1 < slen && cs[pos +1] == 'N') {
st_assign(sf->current, TYPE_NUMBER, pos, 2, cs + pos);
return pos + 2;
} else {
st_assign_char(sf->current, TYPE_BACKSLASH, pos, 1, cs[pos]);
return pos + 1;
}
}
static size_t parse_operator2(struct libinjection_sqli_state * sf)
{
char ch;
const char *cs = sf->s;
const size_t slen = sf->slen;
size_t pos = sf->pos;
if (pos + 1 >= slen) {
return parse_operator1(sf);
}
if (pos + 2 < slen &&
cs[pos] == '<' &&
cs[pos + 1] == '=' &&
cs[pos + 2] == '>') {
/*
* special 3-char operator
*/
st_assign(sf->current, TYPE_OPERATOR, pos, 3, cs + pos);
return pos + 3;
}
ch = sf->lookup(sf, LOOKUP_OPERATOR, cs + pos, 2);
if (ch != CHAR_NULL) {
st_assign(sf->current, ch, pos, 2, cs+pos);
return pos + 2;
}
/*
* not an operator.. what to do with the two
* characters we got?
*/
if (cs[pos] == ':') {
/* ':' is not an operator */
st_assign(sf->current, TYPE_COLON, pos, 1, cs+pos);
return pos + 1;
} else {
/*
* must be a single char operator
*/
return parse_operator1(sf);
}
}
/*
* Ok! " \" " one backslash = escaped!
* " \\" " two backslash = not escaped!
* "\\\" " three backslash = escaped!
*/
static int is_backslash_escaped(const char* end, const char* start)
{
const char* ptr;
for (ptr = end; ptr >= start; ptr--) {
if (*ptr != '\\') {
break;
}
}
/* if number of backslashes is odd, it is escaped */
return (end - ptr) & 1;
}
static size_t is_double_delim_escaped(const char* cur, const char* end)
{
return ((cur + 1) < end) && *(cur+1) == *cur;
}
/* Look forward for doubling of delimiter
*
* case 'foo''bar' --> foo''bar
*
* ending quote isn't duplicated (i.e. escaped)
* since it's the wrong char or EOL
*
*/
static size_t parse_string_core(const char *cs, const size_t len, size_t pos,
stoken_t * st, char delim, size_t offset)
{
/*
* offset is to skip the perhaps first quote char
*/
const char *qpos =
(const char *) memchr((const void *) (cs + pos + offset), delim,
len - pos - offset);
/*
* then keep string open/close info
*/
if (offset > 0) {
/*
* this is real quote
*/
st->str_open = delim;
} else {
/*
* this was a simulated quote
*/
st->str_open = CHAR_NULL;
}
while (TRUE) {
if (qpos == NULL) {
/*
* string ended with no trailing quote
* assign what we have
*/
st_assign(st, TYPE_STRING, pos + offset, len - pos - offset, cs + pos + offset);
st->str_close = CHAR_NULL;
return len;
} else if ( is_backslash_escaped(qpos - 1, cs + pos + offset)) {
/* keep going, move ahead one character */
qpos =
(const char *) memchr((const void *) (qpos + 1), delim,
(size_t)((cs + len) - (qpos + 1)));
continue;
} else if (is_double_delim_escaped(qpos, cs + len)) {
/* keep going, move ahead two characters */
qpos =
(const char *) memchr((const void *) (qpos + 2), delim,
(size_t)((cs + len) - (qpos + 2)));
continue;
} else {
/* hey it's a normal string */
st_assign(st, TYPE_STRING, pos + offset,
(size_t)(qpos - (cs + pos + offset)), cs + pos + offset);
st->str_close = delim;
return (size_t)(qpos - cs + 1);
}
}
}
/**
* Used when first char is a ' or "
*/
static size_t parse_string(struct libinjection_sqli_state * sf)
{
const char *cs = sf->s;
const size_t slen = sf->slen;
size_t pos = sf->pos;
/*
* assert cs[pos] == single or double quote
*/
return parse_string_core(cs, slen, pos, sf->current, cs[pos], 1);
}
/**
* Used when first char is:
* N or n: mysql "National Character set"
* E : psql "Escaped String"
*/
static size_t parse_estring(struct libinjection_sqli_state * sf)
{
const char *cs = sf->s;
const size_t slen = sf->slen;
size_t pos = sf->pos;
if (pos + 2 >= slen || cs[pos+1] != CHAR_SINGLE) {
return parse_word(sf);
}
return parse_string_core(cs, slen, pos, sf->current, CHAR_SINGLE, 2);
}
static size_t parse_ustring(struct libinjection_sqli_state * sf)
{
const char *cs = sf->s;
size_t slen = sf->slen;
size_t pos = sf->pos;
if (pos + 2 < slen && cs[pos+1] == '&' && cs[pos+2] == '\'') {
sf->pos += 2;
pos = parse_string(sf);
sf->current->str_open = 'u';
if (sf->current->str_close == '\'') {
sf->current->str_close = 'u';
}
return pos;
} else {
return parse_word(sf);
}
}
static size_t parse_qstring_core(struct libinjection_sqli_state * sf, size_t offset)
{
char ch;
const char *strend;
const char *cs = sf->s;
size_t slen = sf->slen;
size_t pos = sf->pos + offset;
/* if we are already at end of string..
if current char is not q or Q
if we don't have 2 more chars
if char2 != a single quote
then, just treat as word
*/
if (pos >= slen ||
(cs[pos] != 'q' && cs[pos] != 'Q') ||
pos + 2 >= slen ||
cs[pos + 1] != '\'') {
return parse_word(sf);
}
ch = cs[pos + 2];
/* the ch > 127 is un-needed since
* we assume char is signed
*/
if (ch < 33 /* || ch > 127 */) {
return parse_word(sf);
}
switch (ch) {
case '(' : ch = ')'; break;
case '[' : ch = ']'; break;
case '{' : ch = '}'; break;
case '<' : ch = '>'; break;
}
strend = memchr2(cs + pos + 3, slen - pos - 3, ch, '\'');
if (strend == NULL) {
st_assign(sf->current, TYPE_STRING, pos + 3, slen - pos - 3, cs + pos + 3);
sf->current->str_open = 'q';
sf->current->str_close = CHAR_NULL;
return slen;
} else {
st_assign(sf->current, TYPE_STRING, pos + 3, (size_t)(strend - cs) - pos - 3, cs + pos + 3);
sf->current->str_open = 'q';
sf->current->str_close = 'q';
return (size_t)(strend - cs + 2);
}
}
/*
* Oracle's q string
*/
static size_t parse_qstring(struct libinjection_sqli_state * sf)
{
return parse_qstring_core(sf, 0);
}
/*
* mysql's N'STRING' or
* ... Oracle's nq string
*/
static size_t parse_nqstring(struct libinjection_sqli_state * sf)
{
size_t slen = sf->slen;
size_t pos = sf->pos;
if (pos + 2 < slen && sf->s[pos+1] == CHAR_SINGLE) {
return parse_estring(sf);
}
return parse_qstring_core(sf, 1);
}
/*
* binary literal string
* re: [bB]'[01]*'
*/
static size_t parse_bstring(struct libinjection_sqli_state *sf)
{
size_t wlen;
const char *cs = sf->s;
size_t pos = sf->pos;
size_t slen = sf->slen;
/* need at least 2 more characters
* if next char isn't a single quote, then
* continue as normal word
*/
if (pos + 2 >= slen || cs[pos+1] != '\'') {
return parse_word(sf);
}
wlen = strlenspn(cs + pos + 2, sf->slen - pos - 2, "01");
if (pos + 2 + wlen >= slen || cs[pos + 2 + wlen] != '\'') {
return parse_word(sf);
}
st_assign(sf->current, TYPE_NUMBER, pos, wlen + 3, cs + pos);
return pos + 2 + wlen + 1;
}
/*
* hex literal string
* re: [xX]'[0123456789abcdefABCDEF]*'
* mysql has requirement of having EVEN number of chars,
* but pgsql does not
*/
static size_t parse_xstring(struct libinjection_sqli_state *sf)
{
size_t wlen;
const char *cs = sf->s;
size_t pos = sf->pos;
size_t slen = sf->slen;
/* need at least 2 more characters
* if next char isn't a single quote, then
* continue as normal word
*/
if (pos + 2 >= slen || cs[pos+1] != '\'') {
return parse_word(sf);
}
wlen = strlenspn(cs + pos + 2, sf->slen - pos - 2, "0123456789ABCDEFabcdef");
if (pos + 2 + wlen >= slen || cs[pos + 2 + wlen] != '\'') {
return parse_word(sf);
}
st_assign(sf->current, TYPE_NUMBER, pos, wlen + 3, cs + pos);
return pos + 2 + wlen + 1;
}
/**
* This handles MS SQLSERVER bracket words
* http://stackoverflow.com/questions/3551284/sql-serverwhat-do-brackets-mean-around-column-name
*
*/
static size_t parse_bword(struct libinjection_sqli_state * sf)
{
const char *cs = sf->s;
size_t pos = sf->pos;
const char* endptr = (const char*) memchr(cs + pos, ']', sf->slen - pos);
if (endptr == NULL) {
st_assign(sf->current, TYPE_BAREWORD, pos, sf->slen - pos, cs + pos);
return sf->slen;
} else {
st_assign(sf->current, TYPE_BAREWORD, pos, (size_t)(endptr - cs) - pos + 1, cs + pos);
return (size_t)((endptr - cs) + 1);
}
}
static size_t parse_word(struct libinjection_sqli_state * sf)
{
char ch;
char delim;
size_t i;
const char *cs = sf->s;
size_t pos = sf->pos;
size_t wlen = strlencspn(cs + pos, sf->slen - pos,
" []{}<>:\\?=@!#~+-*/&|^%(),';\t\n\v\f\r\"\240\000");
st_assign(sf->current, TYPE_BAREWORD, pos, wlen, cs + pos);
/* now we need to look inside what we good for "." and "`"
* and see if what is before is a keyword or not
*/
for (i =0; i < sf->current->len; ++i) {
delim = sf->current->val[i];
if (delim == '.' || delim == '`') {
ch = sf->lookup(sf, LOOKUP_WORD, sf->current->val, i);
if (ch != TYPE_NONE && ch != TYPE_BAREWORD) {
/* needed for swig */
st_clear(sf->current);
/*
* we got something like "SELECT.1"
* or SELECT`column`
*/
st_assign(sf->current, ch, pos, i, cs + pos);
return pos + i;
}
}
}
/*
* do normal lookup with word including '.'
*/
if (wlen < LIBINJECTION_SQLI_TOKEN_SIZE) {
ch = sf->lookup(sf, LOOKUP_WORD, sf->current->val, wlen);
if (ch == CHAR_NULL) {
ch = TYPE_BAREWORD;
}
sf->current->type = ch;
}
return pos + wlen;
}
/* MySQL backticks are a cross between string and
* and a bare word.
*
*/
static size_t parse_tick(struct libinjection_sqli_state* sf)
{
size_t pos = parse_string_core(sf->s, sf->slen, sf->pos, sf->current, CHAR_TICK, 1);
/* we could check to see if start and end of
* of string are both "`", i.e. make sure we have
* matching set. `foo` vs. `foo
* but I don't think it matters much
*/
/* check value of string to see if it's a keyword,
* function, operator, etc
*/
char ch = sf->lookup(sf, LOOKUP_WORD, sf->current->val, sf->current->len);
if (ch == TYPE_FUNCTION) {
/* if it's a function, then convert token */
sf->current->type = TYPE_FUNCTION;
} else {
/* otherwise it's a 'n' type -- mysql treats
* everything as a bare word
*/
sf->current->type = TYPE_BAREWORD;
}
return pos;
}
static size_t parse_var(struct libinjection_sqli_state * sf)
{
size_t xlen;
const char *cs = sf->s;
const size_t slen = sf->slen;
size_t pos = sf->pos + 1;
/*
* var_count is only used to reconstruct
* the input. It counts the number of '@'
* seen 0 in the case of NULL, 1 or 2
*/
/*
* move past optional other '@'
*/
if (pos < slen && cs[pos] == '@') {
pos += 1;
sf->current->count = 2;
} else {
sf->current->count = 1;
}
/*
* MySQL allows @@`version`
*/
if (pos < slen) {
if (cs[pos] == '`') {
sf->pos = pos;
pos = parse_tick(sf);
sf->current->type = TYPE_VARIABLE;
return pos;
} else if (cs[pos] == CHAR_SINGLE || cs[pos] == CHAR_DOUBLE) {
sf->pos = pos;
pos = parse_string(sf);
sf->current->type = TYPE_VARIABLE;
return pos;
}