Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #define RETSIGTYPE void
  2. #include <sys/types.h>
  3. #include <sys/time.h>
  4. #include <netinet/in.h>
  5. #include <pcap.h>
  6. #include <signal.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <unistd.h>
  11.  
  12. #ifndef setsignal_h
  13. #define setsignal_h
  14.  
  15. RETSIGTYPE (*setsignal(int, RETSIGTYPE (*)(int)))(int);
  16. #endif
  17.  
  18. char cpre580f98[] = "netdump";
  19.  
  20. void raw_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p);
  21.  
  22. int packettype;
  23.  
  24. char *program_name;
  25.  
  26. /* Externs */
  27. extern void bpf_dump(const struct bpf_program *, int);
  28.  
  29. extern char *copy_argv(char **);
  30.  
  31. /* Forwards */
  32. void program_ending(int);
  33.  
  34. /* Length of saved portion of packet. */
  35. int snaplen = 1500;;
  36.  
  37. static pcap_t *pd;
  38.  
  39. extern int optind;
  40. extern int opterr;
  41. extern char *optarg;
  42. int pflag = 0, aflag = 0;
  43.  
  44. int
  45. main(int argc, char **argv)
  46. {
  47. int cnt, op, i, done = 0;
  48. bpf_u_int32 localnet, netmask;
  49. char *cp, *cmdbuf, *device;
  50. struct bpf_program fcode;
  51. void (*oldhandler)(int);
  52. u_char *pcap_userdata;
  53. char ebuf[PCAP_ERRBUF_SIZE];
  54.  
  55. cnt = -1;
  56. device = NULL;
  57.  
  58. if ((cp = strrchr(argv[0], '/')) != NULL)
  59. program_name = cp + 1;
  60. else
  61. program_name = argv[0];
  62.  
  63. opterr = 0;
  64. while ((i = getopt(argc, argv, "pa")) != -1)
  65. {
  66. switch (i)
  67. {
  68. case 'p':
  69. pflag = 1;
  70. break;
  71. case 'a':
  72. aflag = 1;
  73. break;
  74. case '?':
  75. default:
  76. done = 1;
  77. break;
  78. }
  79. if (done) break;
  80. }
  81. if (argc > (optind)) cmdbuf = copy_argv(&argv[optind]);
  82. else cmdbuf = "";
  83.  
  84. if (device == NULL) {
  85. device = pcap_lookupdev(ebuf);
  86. if (device == NULL)
  87. error("%s", ebuf);
  88. }
  89. pd = pcap_open_live(device, snaplen, 1, 1000, ebuf);
  90. if (pd == NULL)
  91. error("%s", ebuf);
  92. i = pcap_snapshot(pd);
  93. if (snaplen < i) {
  94. warning("snaplen raised from %d to %d", snaplen, i);
  95. snaplen = i;
  96. }
  97. if (pcap_lookupnet(device, &localnet, &netmask, ebuf) < 0) {
  98. localnet = 0;
  99. netmask = 0;
  100. warning("%s", ebuf);
  101. }
  102. /*
  103. * Let user own process after socket has been opened.
  104. */
  105. setuid(getuid());
  106.  
  107. if (pcap_compile(pd, &fcode, cmdbuf, 1, netmask) < 0)
  108. error("%s", pcap_geterr(pd));
  109.  
  110. (void)setsignal(SIGTERM, program_ending);
  111. (void)setsignal(SIGINT, program_ending);
  112. /* Cooperate with nohup(1) */
  113. if ((oldhandler = setsignal(SIGHUP, program_ending)) != SIG_DFL)
  114. (void)setsignal(SIGHUP, oldhandler);
  115.  
  116. if (pcap_setfilter(pd, &fcode) < 0)
  117. error("%s", pcap_geterr(pd));
  118. pcap_userdata = 0;
  119. (void)fprintf(stderr, "%s: listening on %s\n", program_name, device);
  120. if (pcap_loop(pd, cnt, raw_print, pcap_userdata) < 0) {
  121. (void)fprintf(stderr, "%s: pcap_loop: %s\n",
  122. program_name, pcap_geterr(pd));
  123. exit(1);
  124. }
  125. pcap_close(pd);
  126. exit(0);
  127. }
  128.  
  129. /* routine is executed on exit */
  130. void program_ending(int signo)
  131. {
  132. struct pcap_stat stat;
  133.  
  134. if (pd != NULL && pcap_file(pd) == NULL) {
  135. (void)fflush(stdout);
  136. putc('\n', stderr);
  137. if (pcap_stats(pd, &stat) < 0)
  138. (void)fprintf(stderr, "pcap_stats: %s\n",
  139. pcap_geterr(pd));
  140. else {
  141. (void)fprintf(stderr, "%d packets received by filter\n",
  142. stat.ps_recv);
  143. (void)fprintf(stderr, "%d packets dropped by kernel\n",
  144. stat.ps_drop);
  145. }
  146. }
  147. exit(0);
  148. }
  149.  
  150. /* Like default_print() but data need not be aligned */
  151. void
  152. default_print_unaligned(register const u_char *cp, register u_int length)
  153. {
  154. register u_int i, s;
  155. register int nshorts;
  156.  
  157. nshorts = (u_int) length / sizeof(u_short);
  158. i = 0;
  159. while (--nshorts >= 0) {
  160. if ((i++ % 8) == 0)
  161. (void)printf("\n\t\t\t");
  162. s = *cp++;
  163. (void)printf(" %02x%02x", s, *cp++);
  164. }
  165. if (length & 1) {
  166. if ((i % 8) == 0)
  167. (void)printf("\n\t\t\t");
  168. (void)printf(" %02x", *cp);
  169. }
  170. }
  171.  
  172. /*
  173. * By default, print the packet out in hex.
  174. */
  175. void
  176. default_print(register const u_char *bp, register u_int length)
  177. {
  178. register const u_short *sp;
  179. register u_int i;
  180. register int nshorts;
  181.  
  182. if ((long)bp & 1) {
  183. default_print_unaligned(bp, length);
  184. return;
  185. }
  186. sp = (u_short *)bp;
  187. nshorts = (u_int) length / sizeof(u_short);
  188. i = 0;
  189. while (--nshorts >= 0) {
  190. if ((i++ % 8) == 0)
  191. (void)printf("\n\t");
  192. (void)printf(" %04x", ntohs(*sp++));
  193. }
  194. if (length & 1) {
  195. if ((i % 8) == 0)
  196. (void)printf("\n\t");
  197. (void)printf(" %02x", *(u_char *)sp);
  198. }
  199. }
  200.  
  201. /*
  202. insert your code in this routine
  203.  
  204. */
  205.  
  206. void raw_print(u_char *user, const struct pcap_pkthdr *h, const u_char *p)
  207. {
  208. u_int length = h->len;
  209. u_int caplen = h->caplen;
  210.  
  211.  
  212. default_print(p, caplen);
  213. putchar('\n');
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement