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
| \#include \<sys/socket.h\>
\#include \<netinet/in.h\>
\#include \<arpa/inet.h\>
\#include "nids.h"
char ascii_string[10000];
char \*char_to_ascii(char ch)
/\*此函数功能主要用于把协议数据进行显示\*/
{
char \*string;
ascii_string[0] = 0;
string = ascii_string;
/\*打印字符\*/
if(isgraph(ch))
{
\*string++ = ch;
}
/\*打印空格\*/
else if (ch == '.')
{
\*string++ == ch;
}
/\*打印回车和换行\*/
else if (ch == '\\n' \|\| ch == '\\r')
{
\*string++ = ch;
}
/\*打印其他字符,以.显示\*/
else
{
\*string++ = '.';
}
\*string = 0;
return ascii_string;
}
void parse_client_data(char content[], int number)
{
char temp[1024];
char str1[1024];//http协议
char str2[1024];//状态码显示
char str3[1024];//未知代码
unsigned int i;
unsigned int k=0;
unsigned int j;
char entity_content[1024];
if(content[0] != 'H' && content[1] != 'T' && content[2] != 'T' && content[3] != 'P')
{
printf("实体内容为(续): \\n");
for (i = 0; i \< number; i++)
{
printf("%s", char_to_ascii(content[i]));
}
printf("\\n");
}
else
{
printf("number:%d\\n",number);
for (i = 0; i \< strlen(content); i++)
{
if (content[i] != '\\n')
{
k++;
continue;
}
for (j = 0; j \< k; j++)
{
temp[j] = content[j + i - k];
}
temp[j] = '\\0';
if (strstr(temp, "HTTP"))
{
printf("状态行为: ");
printf("%s\\n",temp);
sscanf(temp, "%s %s %s", str1, str2,str3);
printf("HTTP协议为: %s\\n", str1);
printf("状态代码为: %s\\n", str2);
printf("未知代码:%s\\n",str3);
}
if (strstr(temp, "Date"))
{
printf("当前的时间为(Date) : %s\\n", temp + strlen("Date:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Server"))
{
printf("服务器为 ( Server ): %s\\n", temp + strlen("Server:"));
printf("%s\\n", temp);
}
if (strstr(temp, "CaChe-Control"))
{
printf("缓存机制为 ( CaChe-Control ): %s\\n", temp + strlen("CaChe-Control:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Expires"))
{
printf("资源期限为 ( Expires ): %s\\n", temp + strlen("Erpires:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Last-Modified"))
{
printf("最后一次修改时间为 ( Last-Modified ): %s\\n", temp + strlen("Last-Modified:"));
printf("%s\\n", temp);
}
if (strstr(temp, "ETag"))
{
printf("ETag为 ( ETag ): %s\\n", temp + strlen("ETag:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Accept-Ranges"))
{
printf("Accept-Ranges ( Accept-Ranges ): %s\\n", temp + strlen("Accept-Ranges:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Content-Length"))
{
printf("内容长度为 ( Content-Length ): %s\\n", temp + strlen("Content-Length:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Connection"))
{
printf("连接状态为 ( Connection ): %s\\n", temp + strlen("Connection:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Connent-Type"))
{
printf("内容类型为 ( Content-Type ): %s\\n", temp + strlen("Content-Type:"));
printf("%s\\n", temp);
}
/\*获取实体内容\*/
if ((content[i] == '\\n') && (content[i + 1] == '\\r'))
{
if (i + 3 == strlen(content))
{
printf("无实体内容\\n");
break;
}
for (j = 0; j \< number - i - 3; j++)
entity_content[j] = content[i + 3 + j];
entity_content[j] = '\\0';
printf("实体内容为: \\n");
for (i = 0; i \< j; i++)
{
printf("%s", char_to_ascii(entity_content[i]));
}
printf("\\n");
break;
}
k = 0;
}
}
}
void parse_server_data(char content[], int number)
{
char temp[1024];
char str1[1024];
char str2[1024];
char str3[1024];
unsigned int i;
unsigned int k=0;
unsigned int j;
char entity_content[1024];
for (i = 0; i \< strlen(content); i++)
{
if (content[i] != '\\n')
{
k++;
continue;
}
for(j = 0; j \< k; j++)
temp[j] = content[j + i - k];
temp[j]='\\0';
if (strstr(temp, "GET"))
{
printf("请求行为:");
printf("%s\\n", temp);
sscanf(temp, "%s %s %s", str1, str2, str3);
printf("使用的命令为: %s\\n", str1);
printf("获得的资源为: %s\\n", str2);
printf("HTTP协议类型为: %s\\n", str3);
}
if (strstr(temp, "Accept:"))
{
printf("接受的文件包括 ( Accpet: ) :%s\\n", temp + strlen("Accept:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Referer"))
{
printf("转移地址为 ( Referer ) :%s\\n", temp + strlen("Referer:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Accept-Language"))
{
printf("使用的语言为 ( Accept-Language ) :%s\\n", temp + strlen("Accept-Language:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Accept-Encoding"))
{
printf("接收的编码方式为 ( Accept-Encoding ) :%s\\n", temp + strlen("Accept-Encoding:"));
printf("%s\\n", temp);
}
if (strstr(temp, "If-Modified-Since"))
{
printf("上次修改的时间为 ( If-Modified-Since ) :%s\\n", temp + strlen("If-Modified-Since:"));
printf("%s\\n", temp);
}
if (strstr(temp, "If-None-Match"))
{
printf("If-None-Match 为 ( If-None-Match ) :%s\\n", temp + strlen("Id-None-Match:"));
printf("%s\\n", temp);
}
if (strstr(temp, "User-Agent"))
{
printf("用户的浏览信息为 ( User-Agent ) :%s\\n", temp + strlen("User-Agent:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Host"))
{
printf("访问的主机为 ( Host ) :%s\\n", temp + strlen("Host:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Connection"))
{
printf("连接状态为 ( Connection ) :%s\\n", temp + strlen("Connection:"));
printf("%s\\n", temp);
}
if (strstr(temp, "Cookie"))
{
printf("Cookie 为 ( Cookie ) :%s\\n", temp + strlen("Cookie:"));
printf("%s\\n", temp);
}
if((content[i] == '\\n') && (content[i + 1] = '\\r') && (content[i + 2] = '\\n'))
{
if (i + 3 == strlen(content))
{
printf("无实体内容 \\n");
break;
}
for (j = 0; j \< strlen(content) - i - 3; j++)
entity_content[j] = content[i + 3 + j];
entity_content[j] = '\\0';
printf("实体内容为: \\n");
printf("%s", entity_content);
printf("\\n");
break;
}
k = 0;
}
}
void http_protocol_callback(struct tcp_stream \*tcp_http_connection, void \*\*param)
{
char address_content[1024];
char content[65535];
//char content_urgent[65535];
struct tuple4 ip_and_port = tcp_http_connection-\>addr;
strcpy(address_content, inet_ntoa(\*((struct in_addr\*) & (ip_and_port.saddr))));
sprintf(address_content + strlen(address_content), " : %i", ip_and_port.source);
strcat(address_content, "\<----\>");
strcat(address_content, inet_ntoa(\*((struct in_addr\*) & (ip_and_port.daddr))));
sprintf(address_content + strlen(address_content), " : %i", ip_and_port.dest);
strcat(address_content, "\\n");
if(tcp_http_connection-\>nids_state == NIDS_JUST_EST)
{
if (tcp_http_connection-\>addr.dest !=80)
{
return ;
}
tcp_http_connection-\>client.collect++;
tcp_http_connection-\>server.collect++;
printf("\\n\\n\\n=========================================\\n");
printf("%s 建立连接...\\n",address_content);
return ;
}
if(tcp_http_connection-\>nids_state == NIDS_CLOSE)
{
printf("-----------------------------\\n");
printf("%s正常关闭...\\n",address_content);
return ;
}
if (tcp_http_connection-\>nids_state == NIDS_RESET)
{
printf("-----------------------------\\n");
printf("%s连接被RST关闭..\\n", address_content);
return ;
}
if (tcp_http_connection-\>nids_state == NIDS_DATA)
{
struct half_stream \*hlf;
if (tcp_http_connection-\>client.count_new)
{
hlf = \&tcp_http_connection-\>client;
strcpy(address_content, inet_ntoa(\*((struct in_addr\*)\&(ip_and_port.saddr))));
sprintf(address_content + strlen(address_content), ":%i", ip_and_port.source);
strcat(address_content, "\<----");
strcat(address_content, inet_ntoa(\*((struct in_addr\*)\&(ip_and_port.daddr))));
sprintf(address_content + strlen(address_content), ":%i", ip_and_port.dest);
strcat(address_content, "\\n");
printf("\\n");
printf("%s", address_content);
printf("浏览器接收的数据...\\n");
printf("\\n");
memcpy(content, hlf-\>data, hlf-\>count_new);
content[hlf-\>count_new] = '\\0';
parse_client_data(content, hlf-\>count_new);
}
else
{
hlf = \&tcp_http_connection-\>server;
strcpy(address_content, inet_ntoa(\*((struct in_addr\*) & (ip_and_port.saddr))));
sprintf(address_content + strlen(address_content), ":%i", ip_and_port.source);
strcat(address_content, "----\>");
strcat(address_content, inet_ntoa(\*((struct in_addr\*) & (ip_and_port.daddr))));
sprintf(address_content + strlen(address_content), ":%i", ip_and_port.dest);
strcat(address_content, "\\n");
printf("\\n");
printf("%s", address_content);
printf("服务器接收数据...\\n");
printf("\\n");
memcpy(content, hlf-\>data, hlf-\>count_new);
content[hlf-\>count_new] = '\\0';
parse_server_data(content, hlf-\>count_new);
}
}
return ;
}
int main(void)
{
struct nids_chksum_ctl tmp;
//关闭校验和
tmp.netaddr = 0;
tmp.mask = 0;
tmp.action = 1;
nids_register_chksum_ctl(&tmp, 1);
printf("nids_chksum_ctl:%d\\n",tmp.action);
if (!nids_init())
{
printf("出现错误: %s\\n", nids_errbuf);
exit(1);
}
nids_register_tcp(http_protocol_callback);
nids_run();
return 0;
}
|