Advertisement
Sweetening

Untitled

May 26th, 2024
31
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.57 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Function to print IP header
  4. print_ip_header() {
  5. local iphdr="$1"
  6. local iphdrlen=$(($(($(($(printf "%d" "'${iphdr:0:1}")) & 0x0F)) * 4)))
  7. local src_ip="$(printf "%d.%d.%d.%d" "'${iphdr:12:1}" "'${iphdr:13:1}" "'${iphdr:14:1}" "'${iphdr:15:1}")"
  8. local dst_ip="$(printf "%d.%d.%d.%d" "'${iphdr:16:1}" "'${iphdr:17:1}" "'${iphdr:18:1}" "'${iphdr:19:1}")"
  9.  
  10. printf "\nIP Header\n"
  11. printf " |-IP Version : %d\n" "$((($(printf "%d" "'${iphdr:0:1}")) >> 4))"
  12. printf " |-IP Header Length : %d DWORDS or %d Bytes\n" "$((($(printf "%d" "'${iphdr:0:1}")) & 0x0F))" "$iphdrlen"
  13. printf " |-Type Of Service : %d\n" "$(printf "%d" "'${iphdr:1:1}")"
  14. printf " |-IP Total Length : %d Bytes(Size of Packet)\n" "$((($(printf "%d" "'${iphdr:2:1}") << 8) + $(printf "%d" "'${iphdr:3:1}")))"
  15. printf " |-Identification : %d\n" "$((($(printf "%d" "'${iphdr:4:1}") << 8) + $(printf "%d" "'${iphdr:5:1}")))"
  16. printf " |-TTL : %d\n" "$(printf "%d" "'${iphdr:8:1}")"
  17. printf " |-Protocol : %d\n" "$(printf "%d" "'${iphdr:9:1}")"
  18. printf " |-Checksum : %d\n" "$((($(printf "%d" "'${iphdr:10:1}") << 8) + $(printf "%d" "'${iphdr:11:1}")))"
  19. printf " |-Source IP : %s\n" "$src_ip"
  20. printf " |-Destination IP : %s\n" "$dst_ip"
  21. }
  22.  
  23. # Function to print TCP header
  24. print_tcp_packet() {
  25. local tcp_hdr="$1"
  26. local src_port="$((($(printf "%d" "'${tcp_hdr:0:1}") << 8) + $(printf "%d" "'${tcp_hdr:1:1}")))"
  27. local dst_port="$((($(printf "%d" "'${tcp_hdr:2:1}") << 8) + $(printf "%d" "'${tcp_hdr:3:1}")))"
  28. local seq_num="$((($(($(($(printf "%d" "'${tcp_hdr:4:1}")) << 24)) + ($(($(printf "%d" "'${tcp_hdr:5:1}")) << 16))) + ($(($(printf "%d" "'${tcp_hdr:6:1}")) << 8)) + $(printf "%d" "'${tcp_hdr:7:1}")))"
  29. local ack_num="$((($(($(($(printf "%d" "'${tcp_hdr:8:1}")) << 24)) + ($(($(printf "%d" "'${tcp_hdr:9:1}")) << 16))) + ($(($(printf "%d" "'${tcp_hdr:10:1}")) << 8)) + $(printf "%d" "'${tcp_hdr:11:1}")))"
  30. local header_len=$((($(printf "%d" "'${tcp_hdr:12:1}") >> 4) * 4))
  31.  
  32. printf "\n\n***********************TCP Packet*************************\n"
  33. print_ip_header "$2"
  34. printf "\nTCP Header\n"
  35. printf " |-Source Port : %d\n" "$src_port"
  36. printf " |-Destination Port : %d\n" "$dst_port"
  37. printf " |-Sequence Number : %u\n" "$seq_num"
  38. printf " |-Acknowledge Number : %u\n" "$ack_num"
  39. printf " |-Header Length : %d DWORDS or %d BYTES\n" "$((($(printf "%d" "'${tcp_hdr:12:1}") >> 4) & 0x0F))" "$header_len"
  40. printf " |-Urgent Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x20) >> 5))"
  41. printf " |-Acknowledgement Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x10) >> 4))"
  42. printf " |-Push Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x08) >> 3))"
  43. printf " |-Reset Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x04) >> 2))"
  44. printf " |-Synchronise Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x02) >> 1))"
  45. printf " |-Finish Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x01)))"
  46. printf " |-Window : %d\n" "$((($(printf "%d" "'${tcp_hdr:14:1}") << 8) + $(printf "%d" "'${tcp_hdr:15:1}")))"
  47. printf " |-Checksum : %d\n" "$((($(printf "%d" "'${tcp_hdr:16:1}") << 8) + $(printf "%d" "'${tcp_hdr:17:1}")))"
  48. printf " |-Urgent Pointer : %d\n" "$((($(printf "%d" "'${tcp_hdr:18:1}") << 8) + $(printf "%d" "'${tcp_hdr:19:1}")))"
  49. printf "\n"
  50. printf " DATA Dump \n"
  51. printf "\nIP Header\n"
  52. printf "%s\n" "$2" | xxd -p -c $header_len
  53. printf "\nTCP Header\n"
  54. printf "%s\n" "${tcp_hdr:0:$header_len*2}" | xxd -p -c $header_len
  55. printf "\nData Payload\n"
  56. printf "%s\n" "${2:$header_len*2}"
  57. printf "\n###########################################################\n"
  58. }
  59.  
  60. # Start tcpdump to capture packets
  61. tcpdump -n -l |
  62. # Process each line of tcpdump output
  63. while read -r line; do
  64. # Extract protocol, source IP, destination IP, and TCP header
  65. protocol=$(awk '{print $2}' <<< "$line")
  66. src_ip=$(awk '{print $3}' <<< "$line" | awk -F. '{print $1"."$2"."$3"."$4}')
  67. dst_ip=$(awk '{print $5}' <<< "$line" | awk -F. '{print $1"."$2"."$3"."$4}')
  68. tcp_hdr=$(awk '{print $10}' <<< "$line")
  69.  
  70. # Check if the line contains TCP header information
  71. if [[ "$protocol" == "TCP" && -n "$tcp_hdr" ]]; then
  72. # Call function to print TCP packet details
  73. print_tcp_packet "$tcp_hdr" "$line"
  74. fi
  75. done
  76.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement