python構(gòu)造IP報(bào)文實(shí)例
我就廢話不多說了,大家還是直接看代碼吧!
import socket
import sys
import time
import struct
HOST, PORT = "10.60.66.66", 10086
def make_forward_iphdr(source_ip = '1.0.0.1', dest_ip = '2.0.0.2', proto = socket.IPPROTO_UDP) :
# ip header fields
ip_ihl = 5
ip_ver = 4
ip_tos = 0
ip_tot_len = 0 # kernel will fill the correct total length
ip_id = 54321 #Id of this packet
ip_frag_off = 0
ip_ttl = 255
ip_proto = proto
ip_check = 0 # kernel will fill the correct checksum
ip_saddr = socket.inet_aton ( source_ip ) #Spoof the source ip address if you want to
ip_daddr = socket.inet_aton ( dest_ip )
ip_ihl_ver = (ip_ver << 4) + ip_ihl
# the ! in the pack format string means network order
ip_header = struct.pack('!BBHHHBBH4s4s', ip_ihl_ver, ip_tos, ip_tot_len, ip_id, ip_frag_off, ip_ttl, ip_proto, ip_check, ip_saddr, ip_daddr)
return ip_header
def make_forward_udphdr(src_port = 1024, dst_port = 10086) :
udp_header = struct.pack('!HHHH', src_port, dst_port, 0, 0)
return udp_header
# checksum functions needed for calculation checksum
def checksum(msg):
s = 0
# loop taking 2 characters at a time
for i in range(0, len(msg), 2):
w = ord(msg[i]) + (ord(msg[i+1]) << 8 )
s = s + w
s = (s>>16) + (s & 0xffff);
s = s + (s >> 16);
#complement and mask to 4 byte short
s = ~s & 0xffff
return s
def make_tcp_data(ip_header, src_port = 1024, dst_port = 10086, source_ip='1.0.0.1', dest_ip='2.0.0.2', user_data = 'test') :
tcp_source = src_port # source port
tcp_dest = dst_port # destination port
tcp_seq = 454
tcp_ack_seq = 0
tcp_doff = 5 #4 bit field, size of tcp header, 5 * 4 = 20 bytes
#tcp flags
tcp_fin = 0
tcp_syn = 1
tcp_rst = 0
tcp_psh = 0
tcp_ack = 0
tcp_urg = 0
tcp_window = socket.htons (5840) # maximum allowed window size
tcp_check = 0
tcp_urg_ptr = 0
tcp_offset_res = (tcp_doff << 4) + 0
tcp_flags = tcp_fin + (tcp_syn << 1) + (tcp_rst << 2) + (tcp_psh <<3) + (tcp_ack << 4) + (tcp_urg << 5)
# the ! in the pack format string means network order
tcp_header = struct.pack('!HHLLBBHHH' , tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags, tcp_window, tcp_check, tcp_urg_ptr)
source_address = socket.inet_aton(source_ip)
dest_address = socket.inet_aton(dest_ip)
placeholder = 0
protocol = socket.IPPROTO_TCP
tcp_length = len(tcp_header) + len(user_data)
psh = struct.pack('!4s4sBBH' , source_address , dest_address , placeholder , protocol , tcp_length);
psh = psh + tcp_header + user_data;
tcp_check = checksum(psh)
#print tcp_checksum
# make the tcp header again and fill the correct checksum - remember checksum is NOT in network byte order
tcp_header = struct.pack('!HHLLBBH' , tcp_source, tcp_dest, tcp_seq, tcp_ack_seq, tcp_offset_res, tcp_flags, tcp_window) + struct.pack('H' , tcp_check) + struct.pack('!H' ,tcp_urg_ptr)
# final full packet - syn packets dont have any data
packet = ip_header + tcp_header + user_data
return packet
補(bǔ)充知識:python做在域名作為關(guān)鍵字的POST報(bào)文集合分類
將報(bào)文按域名分成不同的集合,而后寫入excel,主要使用了字典數(shù)據(jù)結(jié)構(gòu)
輸入內(nèi)容:
[域名,post報(bào)文(一個(gè)域名有多條,在不同行),域名類型]
輸出內(nèi)容:
[域名,POST報(bào)文集合,域名類型]
#-*- encoding:UTF-8 -*-
import openpyxl
from openpyxl import load_workbook
from openpyxl import Workbook
import numpy as np
import pandas as pd
import re
strinfo = re.compile('[ ]+')
book=load_workbook('ex2.xlsx','utf-8')
sheet=book.worksheets[0]
rows=sheet.max_row
cols=sheet.max_column
Post={}
Type={}
for i in range(2,rows+1):#向字典里添加元素
dn=sheet.cell(i,1).value
pv=sheet.cell(i,2).value
tv=sheet.cell(i,3).value
if Post.get(dn)==None:#第一次遇到這個(gè)域名
Post[dn]=pv
Type[dn]=tv
else:
Post[dn]+='\n'+pv
wb=Workbook()
sh=wb.worksheets[0]#輸出表格
for i in range(2,rows+1):#從字典中取出內(nèi)容存入excel
dn=sheet.cell(i,1).value
if i==2:
Post[dn]=Post[dn].replace('/',' ').replace(':',' ')
Post[dn]=Post[dn].replace('(',' ').replace(')',' ')
Post[dn]=Post[dn].replace('*',' ').replace(';',' ')
Post[dn]=Post[dn].replace('\t',' ').replace('\n',' ')
Post[dn]=Post[dn].replace('$',' ').replace('@',' ')
Post[dn]=Post[dn].replace('=',' ').replace('&',' ')
Post[dn]=Post[dn].replace(',',' ').replace('?',' ')
Post[dn]=strinfo.sub(' ',Post[dn])
sh.append([dn,Post[dn],Type[dn]])
else:
if dn!=sheet.cell(i-1,1).value:
Post[dn]=Post[dn].replace('/',' ').replace(':',' ')
Post[dn]=Post[dn].replace('(',' ').replace(')',' ')
Post[dn]=Post[dn].replace('*',' ').replace(';',' ')
Post[dn]=Post[dn].replace('\t',' ').replace('\n',' ')
Post[dn]=Post[dn].replace('$',' ').replace('@',' ')
Post[dn]=Post[dn].replace('=',' ').replace('&',' ')
Post[dn]=Post[dn].replace(',',' ').replace('?',' ')
Post[dn]=strinfo.sub(' ',Post[dn])
sh.append([dn,Post[dn],Type[dn]])
else:
continue
replace('_x000D_','')
wb.save('out.csv')
以上這篇python構(gòu)造IP報(bào)文實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python3中數(shù)據(jù)校驗(yàn)機(jī)制詳解
在日常編碼環(huán)節(jié),很大比例的錯誤處理工作和參數(shù)的輸入有關(guān),所以這篇文章主要來和大家介紹一下Python3中的數(shù)據(jù)校驗(yàn)機(jī)制,感興趣的可以了解下2024-04-04
python讀取excel表格生成erlang數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了python讀取excel表格生成erlang數(shù)據(jù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
python實(shí)現(xiàn)類似ftp傳輸文件的網(wǎng)絡(luò)程序示例
這篇文章主要介紹了python實(shí)現(xiàn)類似ftp傳輸文件的網(wǎng)絡(luò)程序示例,需要的朋友可以參考下2014-04-04
Python生成驗(yàn)證碼、計(jì)算具體日期是一年中的第幾天實(shí)例代碼詳解
這篇文章主要介紹了Python生成驗(yàn)證碼、計(jì)算具體日期是一年中的第幾天,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-10-10
Python根據(jù)字典值對字典進(jìn)行排序的三種方法實(shí)例
Python中的字典是無序類型,沒有自己的排序方法,下面這篇文章主要給大家介紹了關(guān)于Python根據(jù)字典值對字典進(jìn)行排序的三種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09
成功解決ValueError:?Supported?target?types?are:('binary
本文給大家分享成功解決ValueError:?Supported?target?types?are:('binary',?'multiclass').?Got?'continuous'?instead.的錯誤問題,需要的朋友可以參考下2023-03-03
python中對數(shù)據(jù)進(jìn)行各種排序的方法
這篇文章主要介紹了python中對數(shù)據(jù)進(jìn)行各種排序的方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2019-07-07

