/utils/etterlog/el_analyze.c

https://github.com/wertarbyte/ettercap · C · 199 lines · 111 code · 52 blank · 36 comment · 16 complexity · 49e2e601f12397c17a4b9a34b2320e07 MD5 · raw file

  1. /*
  2. etterlog -- analysis module
  3. Copyright (C) ALoR & NaGA
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #include <el.h>
  17. #include <ec_log.h>
  18. #include <ec_profiles.h>
  19. #include <el_functions.h>
  20. #include <sys/stat.h>
  21. void analyze(void);
  22. void analyze_packet(void);
  23. void analyze_info(void);
  24. void create_hosts_list(void);
  25. /*******************************************/
  26. void analyze(void)
  27. {
  28. switch(GBL.hdr.type) {
  29. case LOG_PACKET:
  30. analyze_packet();
  31. break;
  32. case LOG_INFO:
  33. analyze_info();
  34. break;
  35. }
  36. }
  37. /* analyze a packet log file */
  38. void analyze_packet(void)
  39. {
  40. struct log_header_packet pck;
  41. int ret, count = 0;
  42. int tot_size = 0, pay_size = 0;
  43. u_char *buf;
  44. struct stat st;
  45. fprintf(stdout, "\nAnalyzing the log file (one dot every 100 packets)\n");
  46. tot_size = sizeof(struct log_global_header);
  47. /* read the logfile */
  48. LOOP {
  49. memset(&pck, 0, sizeof(struct log_header_packet));
  50. ret = get_packet(&pck, &buf);
  51. /* on error exit the loop */
  52. if (ret != ESUCCESS)
  53. break;
  54. count++;
  55. tot_size += sizeof(struct log_header_packet) + pck.len;
  56. pay_size += pck.len;
  57. if (count % 100 == 0) {
  58. fprintf(stderr, ".");
  59. fflush(stderr);
  60. }
  61. SAFE_FREE(buf);
  62. }
  63. /* get the file stat */
  64. ret = stat(GBL.logfile, &st);
  65. ON_ERROR(ret, -1, "Cannot stat file");
  66. fprintf(stdout, "\n\n");
  67. fprintf(stdout, "Log file size (compressed) : %d\n", (int)st.st_size);
  68. fprintf(stdout, "Log file size (uncompressed) : %d\n", tot_size);
  69. if (tot_size != 0)
  70. fprintf(stdout, "Compression ratio : %.2f %%\n\n", 100 - ((float)st.st_size * 100 / (float)tot_size) );
  71. fprintf(stdout, "Effective payload size : %d\n", pay_size);
  72. if (tot_size != 0)
  73. fprintf(stdout, "Wasted percentage : %.2f %%\n\n", 100 - ((float)pay_size * 100 / (float)tot_size) );
  74. fprintf(stdout, "Number of packets : %d\n", count);
  75. if (count != 0)
  76. fprintf(stdout, "Average size per packet : %d\n", pay_size / count );
  77. fprintf(stdout, "\n");
  78. return;
  79. }
  80. /*
  81. * extract data form the file
  82. * and create the host list
  83. */
  84. void create_hosts_list(void)
  85. {
  86. struct log_header_info inf;
  87. int ret;
  88. struct dissector_info buf;
  89. /* read the logfile */
  90. LOOP {
  91. memset(&inf, 0, sizeof(struct log_header_info));
  92. memset(&buf, 0, sizeof(struct dissector_info));
  93. ret = get_info(&inf, &buf);
  94. /* on error exit the loop */
  95. if (ret != ESUCCESS)
  96. break;
  97. profile_add_info(&inf, &buf);
  98. SAFE_FREE(buf.user);
  99. SAFE_FREE(buf.pass);
  100. SAFE_FREE(buf.info);
  101. SAFE_FREE(buf.banner);
  102. }
  103. }
  104. /*
  105. * analyze an info log file
  106. */
  107. void analyze_info(void)
  108. {
  109. struct host_profile *h;
  110. struct open_port *o;
  111. struct active_user *u;
  112. TAILQ_HEAD(, host_profile) *hosts_list_head = get_host_list_ptr();
  113. int nhl = 0, nhnl = 0, ngw = 0;
  114. int nports = 0, nusers = 0, nhosts = 0;
  115. /* create the hosts' list */
  116. create_hosts_list();
  117. TAILQ_FOREACH(h, hosts_list_head, next) {
  118. if (h->type & FP_HOST_LOCAL)
  119. nhl++;
  120. if (h->type & FP_HOST_NONLOCAL)
  121. nhnl++;
  122. if (h->type & FP_GATEWAY)
  123. ngw++;
  124. nhosts++;
  125. LIST_FOREACH(o, &(h->open_ports_head), next) {
  126. nports++;
  127. LIST_FOREACH(u, &(o->users_list_head), next) {
  128. nusers++;
  129. }
  130. }
  131. }
  132. fprintf(stdout, "\n\n");
  133. fprintf(stdout, "Number of hosts (total) : %d\n\n", nhosts);
  134. fprintf(stdout, "Number of local hosts : %d\n", nhl);
  135. fprintf(stdout, "Number of non local hosts : %d\n", nhnl);
  136. fprintf(stdout, "Number of gateway : %d\n\n", ngw);
  137. fprintf(stdout, "Number of discovered services : %d\n", nports);
  138. fprintf(stdout, "Number of accounts captured : %d\n\n", nusers);
  139. fprintf(stdout, "\n");
  140. return;
  141. }
  142. /* EOF */
  143. // vim:ts=3:expandtab