Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.46 KB | None | 0 0
  1. $ tcpdump -s 1024 -w - | ~/txr/txr pcap.tl
  2.  
  3. 192.168.1.102 --> 192.168.1.146
  4. ether hdr: #S(eth-header dst-mac #(8 0 39 249 113 4) src-mac #(0 30 79 164 102 184) eth-type ETH_IPV4)
  5. ipv4 hdr: #S(ipv4-header ihl 5 ver 4 ecn 0 dscp 0 len 101 ident 7434 fragoffs-hi 0 flags 2
  6. fragoffs-lo 0 ttl 64 proto 6 hdr-sum 39232 src-ip 3232235878
  7. dst-ip 3232235922)
  8. ipv4 payload as text: P��.;;�.�+i�.6�...
  9. KK-?9rrt2b
  10. 春が来た (Haru-ga Kita/Spring has Com
  11.  
  12. (defvarl big-endian-p (= 1 (ffi-get (ffi-put 1 (ffi be-uint32)) (ffi uint32))))
  13. (defvarl little-endian-p (not big-endian-p))
  14.  
  15. (typedef ll-t (enumed uint32 ll-t
  16. DLT_NULL DLT_EN10MB))
  17.  
  18. (typedef eth-t (enumed be-uint16 eth-t
  19. (ETH_IPV4 #x0800)
  20. (ETH_ARP #x0806)
  21. (ETH_IPV6 #x08DD)))
  22.  
  23. (typedef pcap-header (struct pcap-header
  24. (magic uint32)
  25. (majver uint16)
  26. (minver uint16)
  27. (tzoffs uint32)
  28. (tzprec uint32)
  29. (snaplen uint32)
  30. (lltype ll-t)))
  31.  
  32. (typedef pkt-header (struct pkt-header
  33. (tsec uint32)
  34. (tfrac uint32)
  35. (trunclen uint32)
  36. (origlen uint32)))
  37.  
  38.  
  39. (typedef eth-header (struct eth-header
  40. (dst-mac (array 6 uint8))
  41. (src-mac (array 6 uint8))
  42. (eth-type eth-t)))
  43.  
  44. (cond
  45. (big-endian-p
  46. (typedef ipv4-header (struct ipv4-header
  47. (ver (bit 4 uint8))
  48. (ihl (bit 4 uint8))
  49. (dscp (bit 6 uint8))
  50. (ecn (bit 2 uint8))
  51. (len uint16)
  52. (ident uint16)
  53. (flags (bit 3 uint8))
  54. (fragoffs-hi (bit 5 uint8))
  55. (fragoffs-lo uint8)
  56. (ttl uint8)
  57. (proto uint8)
  58. (hdr-sum uint16)
  59. (src-ip uint32)
  60. (dst-ip uint32))))
  61. (little-endian-p
  62. (typedef ipv4-header (struct ipv4-header
  63. (ihl (bit 4 uint8))
  64. (ver (bit 4 uint8))
  65. (ecn (bit 2 uint8))
  66. (dscp (bit 6 uint8))
  67. (len be-uint16)
  68. (ident be-uint16)
  69. (fragoffs-hi (bit 5 uint8))
  70. (flags (bit 3 uint8))
  71. (fragoffs-lo uint8)
  72. (ttl uint8)
  73. (proto uint8)
  74. (hdr-sum be-uint16)
  75. (src-ip be-uint32)
  76. (dst-ip be-uint32)))))
  77.  
  78. ;; Look for IPv4 packets and print headers
  79. (defun decode-packet (phdr buf)
  80. (let ((eh (ffi-get buf (ffi eth-header))))
  81. (unless (eq eh.eth-type 'ETH_IPV4)
  82. (return-from decode-packet))
  83. (let* ((ih (ffi-get buf (ffi ipv4-header) (sizeof eth-header)))
  84. (hdrsz (+ (sizeof eth-header) (sizeof ipv4-header)))
  85. (len (- (length buf) hdrsz))
  86. (body (carray-buf buf (ffi char) hdrsz))
  87. (rawtext (carray-get body))
  88. (text (mapcar (iffi [andf chr-iscntrl [notf chr-isspace]] (ret #.))
  89. rawtext)))
  90. (put-line `@(str-inaddr ih.src-ip) --> @(str-inaddr ih.dst-ip)`)
  91. (put-line ` ether hdr: @eh`)
  92. (put-line ` ipv4 hdr: @ih`)
  93. (put-line ` ipv4 payload as text: @text`))))
  94.  
  95. ;; main program
  96. (let ((*stdin* (open-fileno (fileno *stdin*) "rbu")) ;; binary, unbuffered
  97. (hdr (new pcap-header))
  98. (hdr-buf (make-buf (sizeof pcap-header)))
  99. (phdr (new pkt-header))
  100. (phdr-buf (make-buf (sizeof pkt-header)))
  101. (pay-buf (make-buf 65536)))
  102.  
  103. ;; read pcap file header
  104. (when (< (fill-buf hdr-buf) (sizeof pcap-header))
  105. (return))
  106.  
  107. ;; decode to structure
  108. (ffi-in hdr-buf hdr (ffi pcap-header) t)
  109.  
  110. (unless (eq hdr.lltype 'DLT_EN10MB)
  111. (put-line "can only deal with Ethernet frames")
  112. (exit nil))
  113.  
  114. ;; read and decode packets
  115. (while t
  116. (when (< (fill-buf phdr-buf) (sizeof pkt-header))
  117. (return))
  118. (ffi-in phdr-buf phdr (ffi pkt-header) t)
  119. (buf-set-length pay-buf phdr.trunclen)
  120. (when (< (fill-buf pay-buf) phdr.trunclen)
  121. (return))
  122. (decode-packet phdr pay-buf)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement