2017-06-06 50 views
1

每当我使用我的程序发送SYN数据包时,我都没有回复。我知道服务器正在工作,因为我可以使用正常套接字connect()方法连接到它,但是当我尝试使用RAW套接字来做到这一点,我没有回复,甚至没有RST。SYN数据包得不到回复(Python)原始套接字

这是我的包根据Wireshark的

Transmission Control Protocol, Src Port: 5173 (5173), Dst Port: 5005 n (5005), Seq: 0, Len: 0 
    Source Port: 5173 
    Destination Port: 5005 
    [Stream index: 15] 
    [TCP Segment Len: 0] 
    Sequence number: 0 (relative sequence number) 
    Acknowledgment number: 0 
    Header Length: 40 bytes 
    Flags: 0x002 (SYN) 
     000. .... .... = Reserved: Not set 
     ...0 .... .... = Nonce: Not set 
     .... 0... .... = Congestion Window Reduced (CWR): Not set 
     .... .0.. .... = ECN-Echo: Not set 
     .... ..0. .... = Urgent: Not set 
     .... ...0 .... = Acknowledgment: Not set 
     .... .... 0... = Push: Not set 
     .... .... .0.. = Reset: Not set 
     .... .... ..1. = Syn: Set 
     .... .... ...0 = Fin: Not set 
     [TCP Flags: **********S*] 
    Window size value: 53270 
    [Calculated window size: 53270] 
    Checksum: 0x9f18 [incorrect, should be 0x90ae (maybe caused by "TCP checksum offload"?)] 
    Urgent pointer: 0 
    Options: (20 bytes), Maximum segment size, SACK permitted, Timestamps, No-Operation (NOP), Window scale 
     Maximum segment size: 65495 bytes 
      Kind: Maximum Segment Size (2) 
      Length: 4 
      MSS Value: 65495 
     TCP SACK Permitted Option: True 
      Kind: SACK Permitted (4) 
      Length: 2 
     Timestamps: TSval 378701, TSecr 0 
      Kind: Time Stamp Option (8) 
      Length: 10 
      Timestamp value: 378701 
      Timestamp echo reply: 0 
     No-Operation (NOP) 
      Type: 1 
       0... .... = Copy on fragmentation: No 
       .00. .... = Class: Control (0) 
       ...0 0001 = Number: No-Operation (NOP) (1) 
     Window scale: 7 (multiply by 128) 
    [SEQ/ACK analysis] 

这里是我的Python代码

#!/usr/bin/python 

import socket 
from struct import * 
import random 

s = socket.socket() 
host = "127.0.0.1" 
destination = "127.0.0.1" 

CLRF = '\r\n' 
#socket.gethostname() 
print destination 
port = 5173 

#s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
#s.connect((host, 5005)) 

try: 
    s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_RAW) 
except socket.error , msg: 
    print 'Socket could not be created. Error Code : ' + str(msg[0]) + ' Message ' + msg[1] 
    sys.exit() 

ipSource = '192.168.0.106' 

#IP header 

ipIHL = 5 # Header Length 
ipVersion = 4 # ipv4/v6 
ipTOS = 0 # type of service 
ipTotalLen = 0 ## Kernel will fill correct length apparently 
ipPackID = random.randint(1,1000) 

#Flags 
ipReserved = 0 
ipNoFrag = 1 
ipMoreFrags = 0 

ipFragOffset = 0 #Fragment offset 
ipTTL = 64 
ipProtocol = socket.IPPROTO_TCP 
ipChecksum = 0 # Magic kernel filling in at work again 
ipSource = socket.inet_aton (host) 
ipDest = socket.inet_aton (destination) 

#Packing IP flags 
ipFlags = ipMoreFrags + (ipNoFrag << 1) + (ipReserved << 2) 
ipFragOffset = (ipFlags << 13) + ipFragOffset 

ipIHLVersion = (ipVersion << 4) + ipIHL 

headerIP = pack('!BBHHHBBH4s4s',ipIHLVersion, ipTOS, ipTotalLen, ipPackID, ipFragOffset, ipTTL, ipProtocol, ipChecksum, ipSource, ipDest) 

#Checksum function 
def carry_around_add(a, b): 
    c = a + b 
    return (c & 0xffff) + (c >> 16) 

def checksum(msg): 
    s = 0 
    for i in range(0, len(msg), 2): 
     w = ord(msg[i]) + (ord(msg[i+1]) << 8) 
     s = carry_around_add(s, w) 
    return ~s & 0xffff 

#TCP Header 
tcpSourcePort = port #Source Port 
tcpDestPort = 5005 #Destination Port 
tcpSeqNum = 0 #Packet sequence 
tcpAckNum = 0 #Ackknowledge Number 
tcpOffset = 10 #Size of tcp header 20 bytes 
#tcpReserved = 0 
#tcpECN = 0 
#Control Flags 
tcpURG = 0 
tcpACK = 0 
tcpPSH = 0 
tcpRST = 0 
tcpSYN = 1 
tcpFIN = 0 
tcpWindow = socket.htons (5840) #Dunno how this works 
tcpChecksum = 0 
tcpUrgentPointer = 0 
#TCP Options 
tcpMaxSegmentSize = (2 << 24) + (4 << 16) + 65495 # Kind + Length + Max Segment Size 
tcpSACKPermitted = (4 << 8) + 2#Kind + Length 
#Split TCP TImestamps into 2 because too large 

tcpTimestampPartOne = (8 << 8) + (10) #Kind + Length 
tcpTimestampPartTwo = (378701 << 32) + 0 #Timestamp Value + Timestamp echo reply 
tcpNoOp = (0 << 7) + (0 << 5) + 1 #Copy on fragmentation + Class + Number 
tcpWindowScale = (3 << 16)+ (3 << 8) + 7 #Kind + Length(Bytes) +Shift CountS 

#Combine both due to length issues 
tcpNoOpAndWindowScale = (tcpNoOp << 24) + tcpWindowScale 


tcpOffsetResult = (tcpOffset << 4) + 0 #Shift 4 bytes to left 

#Putting together all the TCP Control Flags 
tcpFlags = tcpFIN + (tcpSYN << 1) + (tcpRST << 2) + (tcpPSH << 3) + (tcpACK << 4) + (tcpURG << 5) 

#Packing the pseudo TCP header 
headerTCP = pack('!HHLLBBHHHLHHQL', tcpSourcePort, tcpDestPort, tcpSeqNum, tcpAckNum, tcpOffsetResult, tcpFlags, tcpWindow, tcpChecksum, tcpUrgentPointer, tcpMaxSegmentSize, tcpSACKPermitted, tcpTimestampPartOne, tcpTimestampPartTwo, tcpNoOpAndWindowScale) 

#headerTCP = pack('!HHLLBBHHH', tcpSourcePort, tcpDestPort, tcpSeqNum, tcpAckNum, tcpOffsetResult, tcpFlags, tcpWindow, tcpChecksum, tcpUrgentPointer) 

#data = 'GET ./asd HTTP/1.1' 
data = '' 

#Checksum Calculation 
#Pseudo Header Fields 
sourceAddr = socket.inet_aton(host) 
destAddr = socket.inet_aton(destination) 
placeholder = 0 
protocol = socket.IPPROTO_TCP 
tcpLen = len(headerTCP) + len(data) 

psh = pack('!4s4sBBH', sourceAddr, destAddr, placeholder, protocol, tcpLen); 
psh = psh + headerTCP + data; 

#Calc checksum 
tcpChecksumReal = (checksum(psh) << 1) 

print(tcpChecksumReal) 

#Pack actual tcp header with checksum 
headerTCP = pack('!HHLLBBH', tcpSourcePort, tcpDestPort, tcpSeqNum, tcpAckNum, tcpOffsetResult, tcpFlags, tcpWindow) + pack('!H', 40728) + pack ('!H', tcpUrgentPointer) + pack('!LHHQL', tcpMaxSegmentSize, tcpSACKPermitted, tcpTimestampPartOne, tcpTimestampPartTwo, tcpNoOpAndWindowScale) 

#Build full packet/ip with tcp with data 
packet = headerIP + headerTCP + data 
#print [hex(ord(c)) for c in packet] 
s.sendto(packet, (destination,0)) 

任何帮助,将不胜感激,谢谢提前。

+1

我看到wireshark表示校验和不正确。如果您在运行python脚本的计算机上执行捕获,并且您的网卡不会通过卸载重新执行校验和计算,则数据包将不会被任何tcp堆栈接受,并且无法发送/接收。 –

+0

@KenCheung哦,对了,我解决了这个问题,但无论如何都可以启用卸载功能吗?我在搜索中找不到任何内容 –

回答

0

贷@KenCheung的答案

原来这是校验和,从我作为参考报头校验还是不正确的,但网络卡被卸载它们。

相关问题