Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Function to print IP header
- print_ip_header() {
- local iphdr="$1"
- local iphdrlen=$(($(($(($(printf "%d" "'${iphdr:0:1}")) & 0x0F)) * 4)))
- local src_ip="$(printf "%d.%d.%d.%d" "'${iphdr:12:1}" "'${iphdr:13:1}" "'${iphdr:14:1}" "'${iphdr:15:1}")"
- local dst_ip="$(printf "%d.%d.%d.%d" "'${iphdr:16:1}" "'${iphdr:17:1}" "'${iphdr:18:1}" "'${iphdr:19:1}")"
- printf "\nIP Header\n"
- printf " |-IP Version : %d\n" "$((($(printf "%d" "'${iphdr:0:1}")) >> 4))"
- printf " |-IP Header Length : %d DWORDS or %d Bytes\n" "$((($(printf "%d" "'${iphdr:0:1}")) & 0x0F))" "$iphdrlen"
- printf " |-Type Of Service : %d\n" "$(printf "%d" "'${iphdr:1:1}")"
- printf " |-IP Total Length : %d Bytes(Size of Packet)\n" "$((($(printf "%d" "'${iphdr:2:1}") << 8) + $(printf "%d" "'${iphdr:3:1}")))"
- printf " |-Identification : %d\n" "$((($(printf "%d" "'${iphdr:4:1}") << 8) + $(printf "%d" "'${iphdr:5:1}")))"
- printf " |-TTL : %d\n" "$(printf "%d" "'${iphdr:8:1}")"
- printf " |-Protocol : %d\n" "$(printf "%d" "'${iphdr:9:1}")"
- printf " |-Checksum : %d\n" "$((($(printf "%d" "'${iphdr:10:1}") << 8) + $(printf "%d" "'${iphdr:11:1}")))"
- printf " |-Source IP : %s\n" "$src_ip"
- printf " |-Destination IP : %s\n" "$dst_ip"
- }
- # Function to print TCP header
- print_tcp_packet() {
- local tcp_hdr="$1"
- local src_port="$((($(printf "%d" "'${tcp_hdr:0:1}") << 8) + $(printf "%d" "'${tcp_hdr:1:1}")))"
- local dst_port="$((($(printf "%d" "'${tcp_hdr:2:1}") << 8) + $(printf "%d" "'${tcp_hdr:3:1}")))"
- 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}")))"
- 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}")))"
- local header_len=$((($(printf "%d" "'${tcp_hdr:12:1}") >> 4) * 4))
- printf "\n\n***********************TCP Packet*************************\n"
- print_ip_header "$2"
- printf "\nTCP Header\n"
- printf " |-Source Port : %d\n" "$src_port"
- printf " |-Destination Port : %d\n" "$dst_port"
- printf " |-Sequence Number : %u\n" "$seq_num"
- printf " |-Acknowledge Number : %u\n" "$ack_num"
- printf " |-Header Length : %d DWORDS or %d BYTES\n" "$((($(printf "%d" "'${tcp_hdr:12:1}") >> 4) & 0x0F))" "$header_len"
- printf " |-Urgent Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x20) >> 5))"
- printf " |-Acknowledgement Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x10) >> 4))"
- printf " |-Push Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x08) >> 3))"
- printf " |-Reset Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x04) >> 2))"
- printf " |-Synchronise Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x02) >> 1))"
- printf " |-Finish Flag : %d\n" "$((($(printf "%d" "'${tcp_hdr:13:1}") & 0x01)))"
- printf " |-Window : %d\n" "$((($(printf "%d" "'${tcp_hdr:14:1}") << 8) + $(printf "%d" "'${tcp_hdr:15:1}")))"
- printf " |-Checksum : %d\n" "$((($(printf "%d" "'${tcp_hdr:16:1}") << 8) + $(printf "%d" "'${tcp_hdr:17:1}")))"
- printf " |-Urgent Pointer : %d\n" "$((($(printf "%d" "'${tcp_hdr:18:1}") << 8) + $(printf "%d" "'${tcp_hdr:19:1}")))"
- printf "\n"
- printf " DATA Dump \n"
- printf "\nIP Header\n"
- printf "%s\n" "$2" | xxd -p -c $header_len
- printf "\nTCP Header\n"
- printf "%s\n" "${tcp_hdr:0:$header_len*2}" | xxd -p -c $header_len
- printf "\nData Payload\n"
- printf "%s\n" "${2:$header_len*2}"
- printf "\n###########################################################\n"
- }
- # Start tcpdump to capture packets
- tcpdump -n -l |
- # Process each line of tcpdump output
- while read -r line; do
- # Extract protocol, source IP, destination IP, and TCP header
- protocol=$(awk '{print $2}' <<< "$line")
- src_ip=$(awk '{print $3}' <<< "$line" | awk -F. '{print $1"."$2"."$3"."$4}')
- dst_ip=$(awk '{print $5}' <<< "$line" | awk -F. '{print $1"."$2"."$3"."$4}')
- tcp_hdr=$(awk '{print $10}' <<< "$line")
- # Check if the line contains TCP header information
- if [[ "$protocol" == "TCP" && -n "$tcp_hdr" ]]; then
- # Call function to print TCP packet details
- print_tcp_packet "$tcp_hdr" "$line"
- fi
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement