PracticeDev/study_python/tcp_relay-master/auth.py

57 lines
1.4 KiB
Python
Executable File

#!/usr/bin/python
import socket
import struct
import threading
import time
import sys
import random
import re
import logging
from optparse import OptionParser
from scapy.all import *
def calculate_tcp_length(pkt):
iphdr_len = pkt[IP].ihl * 4
total_len = pkt[IP].len
tcp_hdrlen = pkt[TCP].dataofs * 4
tcp_payload_len = total_len - iphdr_len - tcp_hdrlen
return tcp_payload_len
def recalculate_tcp_md5_signature(pkt,password):
destination_ip = pkt[IP].dst
source_ip = pkt[IP].src
reserved = 0
protocol = socket.IPPROTO_TCP
tcp_hdrlen = pkt[TCP].dataofs *4
tcp_payload_len =calculate_tcp_length(pkt)
total_length = tcp_hdrlen + tcp_payload_len
#pseudo header
psh = struct.pack("!4s4sBBH",
source_ip,
destination_ip,
reserved,
protocol,
total_length
)
iphdr_len = pkt[IP].ihl * 4
tcp_header = pkt[iphdr_len:iphdr_len+20]
tcp_payload = pkt[iphdr_len+20:]
tcp_sport = pkt[TCP].sport
tcp_dport = pkt[TCP].dport
tcp_seq = pkt[TCP].seq
tcp_ack = pkt[TCP].ack
tcp_dataofs = pkt[TCP].dataofs
tcp_reserved = pkt[TCP].reserved
tcp_flags = pkt[TCP].flags
tcp_window = pkt[TCP].window
tcp_chksum = pkt[TCP].chksum
tcp_urgptr = pkt[TCP].urgptr