如何创建帧 icmp 我在 python 上标记格式错误的数据包
导入套接字、结构、uuid、子进程、fcntl、时间、随机
项目清单
from binascii import hexlify, unhexlify from datetime import datetime tarRed = raw_input('Ingrese nombre de Tarjeta de Red Utilizada:') subprocess.call(['ifconfig', tarRed, 'promisc']) pt = 0x0800 s = socket.socket( socket.AF_PACKET, socket.SOCK_RAW, socket.htons(pt)) s.bind((tarRed, pt)) #GETCheck def getchecksum(ip_header,size): cksum = 0 pointer = 0 while size > 1: cksum += int ((str("%02x" % (ip_header[pointer],)) + str("%02x" % (ip_header[pointer+1],))), 16) size -= 2 pointer += 2 #if size : #这说明了标头为奇数的情况 #cksum += int(ip_header[pointer]) cksum = (cksum >> 16) + (cksum & 0xffff) cksum += (cksum >>16) return (~cksum ) & 0xFFFF def _checksum(data):#计算头部总和 ip_header_sum = sum(struct.unpack_from("6H", data)) #加进位 ip_header_sum = (ip_header_sum & 0xFFFF) + (ip_header_sum >> 8 & 0xFFFF) ip_header_sum = ~ip_header_sum & 0xFFFF return ip_header_sum def i_checksum (checksum_packet): total = 0 num_words = len(checksum_packet) / 2 for chunk in struct.unpack("!%sH" % num_words, checksum_packet[0:num_words*2]): total += chunk if len(checksum_packet) % 2:总计 += ord(checksum_packet[-1]) << 总计 8 += 总计 >> 16 返回 (~total + 0xffff & 0xffff)0xFFFF) ip_header_sum = ~ip_header_sum & 0xFFFF return ip_header_sum def i_checksum(checksum_packet): total = 0 num_words = len(checksum_packet) / 2 for chunk in struct.unpack("!%sH" % num_words, checksum_packet[0:num_words*2] ): total += chunk if len(checksum_packet) % 2: total += ord(checksum_packet[-1]) << 8 total += total >> 16 return (~total + 0xffff & 0xffff)0xFFFF) ip_header_sum = ~ip_header_sum & 0xFFFF return ip_header_sum def i_checksum(checksum_packet): total = 0 num_words = len(checksum_packet) / 2 for chunk in struct.unpack("!%sH" % num_words, checksum_packet[0:num_words*2] ): total += chunk if len(checksum_packet) % 2: total += ord(checksum_packet[-1]) << 8 total += total >> 16 return (~total + 0xffff & 0xffff)16 个回报(〜总数 + 0xffff & 0xffff)16 个回报(〜总数 + 0xffff & 0xffff)
def checksum(source_string): sum = 0 countTo =
(len(source_string)/2)*2 count = 0 while count<countTo:
thisVal = ord(source_string[count + 1])*256 + ord(source_string[count]) sum = sum + thisVal sum = sum &
0xffffffff
count = count + 2 if countTo<len(source_string): sum = sum + ord(source_string[len(source_string) - 1]) sum = sum &
0xffffffff sum = (sum >> 16) + (sum & 0xffff) sum = sum + (sum
>> 16) answer = ~sum answer = answer & 0xffff #answer = answer >> 8 | (answer << 8 & 0xff00) return answer def
checksuma(source_string): sum = 0 countTo =
(len(source_string)/2)*2 count = 0 while count<countTo:
thisVal = ord(source_string[count + 1])*256 + ord(source_string[count]) sum = sum + thisVal sum = sum &
0xffffffff
count = count + 2 if countTo<len(source_string): sum = sum + ord(source_string[len(source_string) - 1]) sum = sum &
0xffffffff sum = (sum >> 16) + (sum & 0xffff) sum = sum + (sum
>> 16) answer = ~sum answer = answer & 0xffff answer = answer >> 8 | (answer << 8 & 0xff00) return answer def ultimo(str):
csum = 0
countTo = (len(str) / 2) * 2
count = 0
while count < countTo:
thisVal = ord(str[count+1]) * 256 + ord(str[count])
csum = csum + thisVal
csum = csum & 0xffffffff
count = count + 2
if countTo < len(str):
csum = csum + ord(str[len(str) - 1])
csum = csum & 0xffffffff
csum = (csum >> 16) + (csum & 0xffff)
csum = csum + (csum >> 16)
answer = ~csum
answer = answer & 0xffff
answer = answer >> 8 | (answer << 8 & 0xff00)
return answer
#GETCheck
#MACs adst='ffffffffffff' mac_destino = unhexlify(adst) ma = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) info =
fcntl.ioctl(ma.fileno(), 0x8927, struct.pack('256s', tarRed[:15]))
asrc = ''.join(['%02x:' % ord(char) for char in info[18:24]])[:-1]
sd = asrc.split(':') asrc = sd[0]+sd[1]+sd[2]+sd[3]+sd[4]+sd[5]
mac_origen = unhexlify(asrc)
#MACs cabEther = struct.pack('!6s6sh',mac_destino,mac_origen,pt)
#datosIP
#version = '4'
#IHL = '5' tipoServicio = unhexlify('00') longitudT = struct.pack('!BB',00,24) identificador = struct.pack('!BB',00,01)
flag_Pos = 0x4000 tiempoVida = 05 ptIP = unhexlify('01') SCC = 0
#IPs ip = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) ipO= str( socket.inet_ntoa(fcntl.ioctl(ip.fileno(),0x8915,struct.pack('256s',
tarRed[:15]))[20:24])) ipO1 = ipO.split('.') ipOr =
struct.pack("!BBBB",int(ipO1[0]),int(ipO1[1]),int(ipO1[2]),int(ipO1[3]))
ipD = raw_input('Ingrese la direccion IP destino: ') ipD1 =
ipD.split('.') ipDest =
struct.pack("!BBBB",int(ipD1[0]),int(ipD1[1]),int(ipD1[2]),int(ipD1[3]))
#IPs
#datosIP cabIP = struct.pack('!1s1s2s2shB1sH4s4s',unhexlify('45'),tipoServicio,longitudT,identificador,flag_Pos,tiempoVida,ptIP,SCC,ipOr,ipDest)
SCC = checksum(cabIP) cabIP =
struct.pack('!1s1s2s2shB1sH4s4s',unhexlify('45'),tipoServicio,longitudT,identificador,flag_Pos,tiempoVida,ptIP,socket.htons(SCC),ipOr,ipDest)
#datosIcmp tipo = 8 codigo = 0x00 check = 0x0000 identificador = int((id(1) * random.random()) % 65535) secuencia = 0x0000
#datos = 'qwertyasdfghzxcvbn0102030405060708091011121314151617181920' datos =
1 * 'Q'
#datosIcmp cabIcmp = struct.pack('!bbHHh',tipo,0,0,identificador,1) my_checksum = ultimo(cabIcmp + datos) cabIcmp =
struct.pack('!bbHHh',tipo,0,socket.htons(my_checksum),identificador,1)
cabIcmp = cabIcmp + datos tiempo = datetime.now() print tiempo for i
in range(15): s.send(cabEther + cabIP + cabIcmp,0)