A userspace application that filters DHCP floods to protect a DHCP server. It uses the Netfilter userspace packet queuing API.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

dhcp_protect.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. // dhcp_protect.c
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <unistd.h>
  6. #include <stdint.h>
  7. #include <errno.h>
  8. #include <time.h>
  9. #include <stdarg.h>
  10. #include <syslog.h>
  11. #include <arpa/inet.h>
  12. #include <linux/netfilter.h>
  13. #include <libnetfilter_queue/libnetfilter_queue.h>
  14. #include <uthash.h>
  15. #include "dhcp_protect.h"
  16. // main function
  17. int main(int argc, char **argv) {
  18. char *configfile;
  19. dp_conf conf;
  20. if ( argc == 2 ) {
  21. configfile = argv[1];
  22. }
  23. else {
  24. usage(argv[0]);
  25. return EXIT_FAILURE;
  26. }
  27. if ( load_config(&conf, configfile) == NULL )
  28. return EXIT_FAILURE;
  29. openlog("dhcp_protect", LOG_PID, LOG_DAEMON);
  30. nfq_start(&conf);
  31. return 0;
  32. }
  33. // syslog function
  34. void dp_log(unsigned char *remoteid, int remoteidlen, char *fmt, ...) {
  35. va_list argList;
  36. char buf[1000];
  37. int offset = remoteidlen*2;
  38. int i;
  39. for(i=0; i<remoteidlen; i++) {
  40. sprintf(buf+(i*2), "%02x", remoteid[i]);
  41. }
  42. buf[offset]=':'; offset++;
  43. buf[offset]=' '; offset++;
  44. va_start(argList, fmt);
  45. vsnprintf(buf+offset, sizeof(buf)-offset-1, fmt, argList);
  46. va_end(argList);
  47. syslog(LOG_DAEMON|LOG_INFO, "%s", buf);
  48. }
  49. // start netfilter queue
  50. void nfq_start(dp_conf *conf) {
  51. struct nfq_handle *h;
  52. struct nfq_q_handle *qh;
  53. int fd;
  54. if ( ( h = nfq_open() ) == NULL ) {
  55. fprintf(stderr,"error during nfq_open() %s\n", strerror(errno));
  56. return;
  57. }
  58. if ( ( qh = nfq_create_queue(h, conf->queue, &dp_callback, conf) ) == NULL ) {
  59. fprintf(stderr, "error during nfq_create_queue() %s\n", strerror(errno));
  60. return;
  61. }
  62. if ( nfq_set_mode(qh, NFQNL_COPY_PACKET, 1500) < 0 ) {
  63. fprintf(stderr,"error during nfq_set_mode() %s\n", strerror(errno));
  64. return;
  65. }
  66. if ( nfq_set_queue_flags(qh, NFQA_CFG_F_FAIL_OPEN, NFQA_CFG_F_FAIL_OPEN) < 0 ) {
  67. fprintf(stderr,"error during nfq_set_queue_flags() %s\n", strerror(errno));
  68. return;
  69. }
  70. fd = nfq_fd(h);
  71. while(1) {
  72. static char buf[65536];
  73. int rv;
  74. if ((rv = recv(fd, buf, sizeof(buf), 0)) >= 0) {
  75. nfq_handle_packet(h, buf, rv); /* send packet to callback */
  76. }
  77. }
  78. }
  79. // display usage
  80. void usage(char *prog) {
  81. fprintf(stderr,"Usage: %s <configuration file>\n",prog);
  82. }
  83. // load configuration file
  84. dp_conf *load_config(dp_conf *conf, char *file) {
  85. FILE *fh;
  86. char *line = NULL;
  87. size_t len = 0;
  88. int error = 0;
  89. printf("Loading configuration %s\n",file);
  90. if ( ( fh = fopen(file,"r") ) == NULL ) {
  91. fprintf(stderr,"Failed to open configuration file '%s': %s\n", file, strerror(errno));
  92. return NULL;
  93. }
  94. while (getline(&line, &len, fh) != -1) {
  95. char *name, *value;
  96. name = strtok(line, "=\r\n ");
  97. value = strtok(NULL,"=\r\n ");
  98. if ( name == NULL || value == NULL || name[0] == '#' )
  99. continue;
  100. if ( strcmp(name,"max_pkt_per_interval")==0 )
  101. conf->pktint = atoi(value);
  102. else if ( strcmp(name,"interval")==0 )
  103. conf->interval = atoi(value);
  104. else if ( strcmp(name, "debug")==0 )
  105. conf->debug = atoi(value) ? 1 : 0;
  106. else if ( strcmp(name, "blacklist_time")==0 )
  107. conf->bltime = atoi(value);
  108. else if ( strcmp(name, "queue")==0 )
  109. conf->queue = atoi(value);
  110. else if ( strcmp(name, "dryrun")==0 )
  111. conf->dryrun = atoi(value) ? 1 : 0;
  112. else
  113. fprintf(stderr,"unknown directive '%s', ignored\n", name);
  114. free(line);
  115. }
  116. fclose(fh);
  117. if ( conf->pktint < 1 || conf->pktint > 1000 ) {
  118. fprintf(stderr,"max_pkt_per_interval value invalid (min 1, max 1000)\n");
  119. error=1;
  120. }
  121. if ( conf->interval < 5 || conf->interval > 900 ) {
  122. fprintf(stderr,"interval value invalid (min 5, max 900)\n");
  123. error=1;
  124. }
  125. if ( conf->debug < 0 || conf->debug > 1 ) {
  126. fprintf(stderr,"debug value invalid (0 or 1)\n");
  127. error=1;
  128. }
  129. if ( conf->bltime < 10 || conf->bltime > 900 ) {
  130. fprintf(stderr,"blacklist_time value invalid (min 10, max 900)\n");
  131. error=1;
  132. }
  133. if ( conf->queue < 0 ) {
  134. fprintf(stderr,"queue must be a positive integer\n");
  135. error=1;
  136. }
  137. if ( conf->dryrun < 0 || conf->dryrun > 1 ) {
  138. fprintf(stderr, "dryrun value invalid (0 or 1)\n");
  139. error=1;
  140. }
  141. if ( error )
  142. return NULL;
  143. printf("Configuration:\n");
  144. printf("\t%-20s = %4s\n", "dryrun", conf->dryrun ? "Yes" : "No");
  145. printf("\t%-20s = %4s\n", "debug", conf->debug ? "Yes" : "No" );
  146. printf("\t%-20s = %4is\n", "interval", conf->interval);
  147. printf("\t%-20s = %4i\n", "max_pkt_per_interval", conf->pktint);
  148. printf("\t%-20s = %4is\n", "blacklist_time", conf->bltime);
  149. printf("\t%-20s = %4i\n", "queue", conf->queue);
  150. return conf;
  151. }
  152. // decode dhcp packet
  153. int dhcp_check(struct nfq_data *nfa, dp_conf *conf) {
  154. unsigned char *pkt;
  155. int pktlen;
  156. int offset = 0;
  157. unsigned char *remoteid = NULL;
  158. int remoteidlen = 0;
  159. int found = 0;
  160. uint8_t ipver = 0;
  161. uint8_t ihl = 0;
  162. //int i;
  163. int rv = NF_ACCEPT;
  164. pktlen = nfq_get_payload(nfa, &pkt);
  165. if ( conf->debug ) printf("got a packet, len = %i\n", pktlen);
  166. /* a bit too much ;)
  167. if ( conf->debug ) {
  168. for(i=0; i<pktlen; i++) {
  169. if ( !(i%16) ) printf("\n");
  170. printf("%02x ", pkt[i]);
  171. }
  172. }
  173. */
  174. // can we read the IP proto and IP header length ?
  175. if ( pktlen > 0 ) {
  176. ipver = pkt[offset];
  177. ipver >>= 4;
  178. ihl = pkt[offset];
  179. ihl &= 0x0f;
  180. if ( ipver != 4 ) {
  181. if ( conf->debug ) printf("not an IPv4 packet\n");
  182. rv = NF_ACCEPT;
  183. goto end;
  184. }
  185. // jump over the IPv4 header
  186. offset += ihl * 4;
  187. }
  188. // jump over UDP + DHCP header
  189. offset += 8 + 28 + 16 + 64 + 128;
  190. // minimum packet size, fixed header + magic cookie (4 octets)
  191. if ( pktlen < offset + 4 ) {
  192. if ( conf->debug ) printf("packet too small\n");
  193. rv = NF_ACCEPT;
  194. goto end;
  195. }
  196. // check magic cookie
  197. if ( pkt[offset] != 99 || pkt[offset+1] != 130 || pkt[offset+2] != 83 || pkt[offset+3] != 99 ) {
  198. if ( conf->debug )
  199. printf(
  200. "invalid magic cookie %02x%02x%02x%02x\n",
  201. pkt[offset], pkt[offset+1],
  202. pkt[offset+2], pkt[offset+3]);
  203. rv = NF_ACCEPT;
  204. goto end;
  205. }
  206. offset+=4;
  207. // parse TLV options
  208. while(offset<pktlen && !found) {
  209. uint8_t type = pkt[offset];
  210. uint8_t len;
  211. offset++;
  212. // padding of 1 octet
  213. if ( type == 0 ) {
  214. continue;
  215. }
  216. // end of options
  217. if ( type == 255 )
  218. break;
  219. // make sure we can read len
  220. if ( offset>=pktlen )
  221. break;
  222. len = pkt[offset];
  223. offset++;
  224. // can the value be read
  225. if ( offset+len>pktlen )
  226. break;
  227. // option 82 parser
  228. if ( type == 82 ) {
  229. unsigned char *o82 = pkt+offset;
  230. int o82off = 0;
  231. // loop until the end, +2 to ensure we can read type and length
  232. while(o82off+2<len && !found) {
  233. uint8_t otype = o82[o82off];
  234. uint8_t olen = o82[o82off+1];
  235. o82off+=2;
  236. // printf("o82 type=%i len=%i\n", otype, olen);
  237. // remoteid
  238. if ( otype == 2 ) {
  239. // make sure we don't overflow and can read all data
  240. if ( o82off + olen > len ) {
  241. if ( conf->debug) printf("option 82.2 data too long\n");
  242. rv = NF_ACCEPT;
  243. goto end;
  244. }
  245. else {
  246. remoteid = o82 + o82off;
  247. remoteidlen = olen;
  248. found=1;
  249. }
  250. }
  251. o82off+=olen;
  252. }
  253. }
  254. offset+=len;
  255. }
  256. if ( found ) {
  257. // count the packet, even when blacklisted.
  258. dp_accounting_add(conf, remoteid, remoteidlen);
  259. // check if already in the blacklist
  260. if ( dp_blacklist_check(conf, remoteid, remoteidlen) == NF_DROP )
  261. rv = NF_DROP;
  262. // check if it must be added to the blacklist
  263. else if ( dp_accounting_check(conf, remoteid, remoteidlen) == NF_DROP ) {
  264. dp_blacklist_add(conf, remoteid, remoteidlen);
  265. rv = NF_DROP;
  266. }
  267. }
  268. end:
  269. dp_hash_cleanup(conf);
  270. return rv;
  271. }
  272. // netfilter queue callback
  273. static int dp_callback(struct nfq_q_handle *qh, struct nfgenmsg *nfmsg, struct nfq_data *nfa, void *data) {
  274. struct nfqnl_msg_packet_hdr *ph = nfq_get_msg_packet_hdr(nfa);
  275. dp_conf *conf = (dp_conf*)data;
  276. int id = -1;
  277. int verdict;
  278. if ( ph ) {
  279. id = ntohl (ph->packet_id);
  280. if ( conf->debug ) printf ("received packet with id %d\n", id);
  281. }
  282. verdict = dhcp_check(nfa, conf); /* Treat packet */
  283. // override decision for dryrun
  284. if ( conf->dryrun )
  285. verdict = NF_ACCEPT;
  286. return nfq_set_verdict(qh, id, verdict, 0, NULL); /* Verdict packet */
  287. }
  288. void dp_accounting_add(dp_conf *conf, unsigned char *remoteid, int len) {
  289. //int i;
  290. dp_accounting *ac;
  291. // does the element already exist
  292. HASH_FIND(hh, accountings, remoteid, len, ac);
  293. if ( conf->debug ) printf("AC: add item\n");
  294. // found it, increment the counter
  295. if ( ac ) {
  296. if ( conf->debug ) printf("AC: item found, incrementing\n");
  297. ac->count++;
  298. }
  299. // not found, create a new one
  300. else {
  301. if ( conf->debug ) printf("AC: item not found, creating\n");
  302. ac = malloc(sizeof(dp_accounting));
  303. memcpy(ac->remoteid, remoteid, len);
  304. ac->len = len;
  305. ac->count = 1;
  306. HASH_ADD(hh, accountings, remoteid, len, ac);
  307. }
  308. }
  309. void dp_blacklist_add(dp_conf *conf, unsigned char *remoteid, int len) {
  310. dp_blacklist *bl;
  311. // alrady exists?
  312. HASH_FIND(hh, blacklists, remoteid, len, bl);
  313. if ( conf->debug ) printf("BL: add item\n");
  314. // found an entry, push the expiration further
  315. if ( bl ) {
  316. if ( conf->debug ) printf("BL: item found -> pushing further\n");
  317. bl->expire = time(NULL) + conf->bltime;
  318. }
  319. // not found, create a new one
  320. else {
  321. if ( conf->debug ) printf("BL: item not found, new entry in BL\n");
  322. dp_log(remoteid, len, "blacklisting started");
  323. bl = malloc(sizeof(dp_blacklist));
  324. memcpy(bl->remoteid, remoteid, len);
  325. bl->len = len;
  326. bl->expire = time(NULL) + conf->bltime;
  327. HASH_ADD(hh, blacklists, remoteid, len, bl);
  328. }
  329. }
  330. void dp_hash_cleanup(dp_conf *conf) {
  331. dp_accounting *ac, *actmp;
  332. dp_blacklist *bl, *bltmp;
  333. // is it time to cleanup the list?
  334. // cleanup every conf->interval seconds
  335. if ( dp_accountingtime + conf->interval < time(NULL) ) {
  336. if ( conf->debug ) printf("cleanup interval\n");
  337. dp_accountingtime = time(NULL);
  338. HASH_ITER(hh, accountings, ac, actmp) {
  339. HASH_DEL(accountings, ac);
  340. free(ac);
  341. }
  342. }
  343. // blacklist cleanup check every 1 sec
  344. if ( dp_cleanuptime < time(NULL) ) {
  345. if ( conf->debug ) printf("cleanup BL\n");
  346. dp_cleanuptime = time(NULL);
  347. HASH_ITER(hh, blacklists, bl, bltmp) {
  348. if ( bl->expire < time(NULL) ) {
  349. dp_log(
  350. bl->remoteid, bl->len,
  351. "blacklisting ended");
  352. HASH_DEL(blacklists, bl);
  353. free(bl);
  354. }
  355. }
  356. }
  357. }
  358. int dp_accounting_check(dp_conf *conf, unsigned char *remoteid, int len) {
  359. dp_accounting *ac;
  360. HASH_FIND(hh, accountings, remoteid, len, ac);
  361. if ( conf->debug ) printf("AC Check\n");
  362. if ( ac ) {
  363. if(conf->debug) printf("AC Check: found item %i > %i ?\n", ac->count, conf->pktint);
  364. if ( ac->count > conf->pktint ) {
  365. if(conf->debug) printf("flood detected!\n");
  366. return NF_DROP;
  367. }
  368. }
  369. return NF_ACCEPT;
  370. }
  371. int dp_blacklist_check(dp_conf *conf, unsigned char *remoteid, int len) {
  372. dp_blacklist *bl;
  373. HASH_FIND(hh, blacklists, remoteid, len, bl);
  374. if ( conf->debug ) printf("BL Check\n");
  375. if ( bl ) {
  376. if ( bl->expire > time(NULL) ) {
  377. if ( conf->debug ) printf("blacklisted!\n");
  378. return NF_DROP;
  379. }
  380. }
  381. return NF_ACCEPT;
  382. }