python构造IP报文实例


Posted in Python onMay 05, 2020

我就废话不多说了,大家还是直接看代码吧!

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

补充知识:python做在域名作为关键字的POST报文集合分类

将报文按域名分成不同的集合,而后写入excel,主要使用了字典数据结构

输入内容:

[域名,post报文(一个域名有多条,在不同行),域名类型]

输出内容:

[域名,POST报文集合,域名类型]

#-*- 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:#第一次遇到?个域名
 Post[dn]=pv
 Type[dn]=tv
 else:
 Post[dn]+='\n'+pv
wb=Workbook()
sh=wb.worksheets[0]#输出表格
for i in range(2,rows+1):#从字典中取出内容存入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构造IP报文实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
详细解读Python中解析XML数据的方法
Oct 15 Python
Python字符串拼接的几种方法整理
Aug 02 Python
Python实现针对含中文字符串的截取功能示例
Sep 22 Python
Python探索之URL Dispatcher实例详解
Oct 28 Python
python保存二维数组到txt文件中的方法
Nov 15 Python
Python+OpenCV+pyQt5录制双目摄像头视频的实例
Jun 28 Python
解决TensorFlow GPU版出现OOM错误的问题
Feb 03 Python
python3.x中安装web.py步骤方法
Jun 23 Python
PyCharm vs VSCode,作为python开发者,你更倾向哪种IDE呢?
Aug 17 Python
python smtplib发送多个email联系人的实现
Oct 09 Python
Python 如何解决稀疏矩阵运算
May 26 Python
Python中非常使用的6种基本变量的操作与技巧
Mar 22 Python
python3通过udp实现组播数据的发送和接收操作
May 05 #Python
解决python使用list()时总是报错的问题
May 05 #Python
python requests.get带header
May 05 #Python
python中urllib.request和requests的使用及区别详解
May 05 #Python
python requests包的request()函数中的参数-params和data的区别介绍
May 05 #Python
关于Python解包知识点总结
May 05 #Python
python 使用事件对象asyncio.Event来同步协程的操作
May 04 #Python
You might like
php投票系统之增加与删除投票(管理员篇)
2016/07/01 PHP
PHP框架Laravel中实现supervisor执行异步进程的方法
2017/06/07 PHP
NiftyCube——轻松实现圆角边框
2007/02/20 Javascript
js注意img图片的onerror事件的分析
2011/01/01 Javascript
ASP.NET jQuery 实例16 通过控件CustomValidator验证RadioButtonList
2012/02/03 Javascript
jQuery插件EnPlaceholder实现输入框提示文字
2015/06/05 Javascript
js+html5操作sqlite数据库的方法
2016/02/02 Javascript
Jquery ajax请求导出Excel表格的实现代码
2016/06/08 Javascript
浅谈js中调用函数时加不加括号的问题
2016/07/28 Javascript
ES6中的数组扩展方法
2016/08/26 Javascript
超出JavaScript安全整数限制的数字计算BigInt详解
2018/06/24 Javascript
深入理解js A*寻路算法原理与具体实现过程
2018/12/13 Javascript
JS拖动选择table里的单元格完整实例【基于jQuery】
2019/05/28 jQuery
微信小程序swiper使用网络图片不显示问题解决
2019/12/13 Javascript
[41:52]DOTA2-DPC中国联赛 正赛 CDEC vs Dynasty BO3 第二场 2月22日
2021/03/11 DOTA
python访问纯真IP数据库的代码
2011/05/19 Python
Python类方法__init__和__del__构造、析构过程分析
2015/03/06 Python
浅谈对yield的初步理解
2017/05/29 Python
详解Python函数可变参数定义及其参数传递方式
2017/08/02 Python
详解python中executemany和序列的使用方法
2017/08/12 Python
Python爬虫之UserAgent的使用实例
2019/02/21 Python
python字符串替换第一个字符串的方法
2019/06/26 Python
通过实例了解python property属性
2019/11/01 Python
Pytorch to(device)用法
2020/01/08 Python
pytorch GAN伪造手写体mnist数据集方式
2020/01/10 Python
Python进程间通信multiprocess代码实例
2020/03/18 Python
Python Serial串口基本操作(收发数据)
2020/11/06 Python
css3设置box-pack和box-align让div里面的元素垂直居中
2014/09/01 HTML / CSS
HTML5页面中尝试调起APP功能
2017/09/12 HTML / CSS
意大利香水和彩妆护肤品购物网站:Ditano
2017/08/13 全球购物
巴西香水和化妆品购物网站:The Beauty Box
2019/09/03 全球购物
澳大利亚领先的时尚内衣零售商:Bras N Things
2020/07/28 全球购物
JAVA代码查错题
2014/10/10 面试题
2014年计算机专业个人自我评价
2014/01/19 职场文书
幼儿园中秋节活动方案2013
2014/01/29 职场文书
2014年银行客户经理工作总结
2014/11/12 职场文书