python構(gòu)造IP報(bào)文實(shí)例
我就廢話不多說(shuō)了,大家還是直接看代碼吧!
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ǔ)充知識(shí):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í)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Pandas groupby apply agg 的區(qū)別 運(yùn)行自定義函數(shù)說(shuō)明
這篇文章主要介紹了Pandas groupby apply agg 的區(qū)別 運(yùn)行自定義函數(shù)說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03詳解Python 重學(xué)requests發(fā)起請(qǐng)求的基本方式
這篇文章主要介紹了詳解Python 重學(xué)requests發(fā)起請(qǐng)求的基本方式,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02- 我們知道python只定義了6種數(shù)據(jù)類型,字符串,整數(shù),浮點(diǎn)數(shù),列表,元組,字典。但是C語(yǔ)言中有些字節(jié)型的變量,在python中該如何實(shí)現(xiàn)呢?這點(diǎn)頗為重要,特別是要在網(wǎng)絡(luò)上進(jìn)行數(shù)據(jù)傳輸?shù)脑挕?/div> 2014-06-06
Python實(shí)現(xiàn)列表刪除重復(fù)元素的三種常用方法分析
這篇文章主要介紹了Python實(shí)現(xiàn)列表刪除重復(fù)元素的三種常用方法,結(jié)合實(shí)例形式對(duì)比分析了Python針對(duì)列表元素的遍歷、判斷、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下2017-11-11Python while、for、生成器、列表推導(dǎo)等語(yǔ)句的執(zhí)行效率測(cè)試
這篇文章主要介紹了Python while、for、生成器、列表推導(dǎo)等語(yǔ)句的執(zhí)行效率測(cè)試,本文分別用兩段程序測(cè)算出了各語(yǔ)句的執(zhí)行效率,然后總結(jié)了什么情況下使用什么語(yǔ)句優(yōu)先使用的語(yǔ)句等,需要的朋友可以參考下2015-06-06詳解Django的model查詢操作與查詢性能優(yōu)化
這篇文章主要介紹了詳解Django的model查詢操作與查詢性能優(yōu)化,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10最新評(píng)論