python模拟发包_使用python扩展编写网络发包程序

这篇博客介绍了如何使用Python编写网络发包程序,通过WinPcap库获取设备列表,打开设备并发送自定义数据包。内容包括列出可用设备、打开设备和发送数据包的函数实现。
摘要由CSDN通过智能技术生成

1 #include 2 3 #include 4 #include 5 6 #include 7 #include 8 9 static pcap_t*gfp;10 11 static PyObject*list_device(PyObject*self, PyObject*args)12 {13 pcap_if_t*alldevs;14 pcap_if_t*d;15 int i=0;16 char errbuf[PCAP_ERRBUF_SIZE];17 18 /*Retrieve the device listfromthe local machine*/19 if(pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL/*authisnotneeded*/,&alldevs, errbuf)==-1)20 {21 fprintf(stderr,"Error in pcap_findalldevs_ex: %s\n", errbuf);22 exit(1);23 }24 25 /*Print the list*/26 for(d=alldevs; d!=NULL; d=d->next)27 {28 printf("%d. %s",++i, d->name);29 if(d->description)30 printf("(%s)\n", d->description);31 else32 printf("(No description available)\n");33 }34 35 if(i==0)36 {37 printf("\nNo interfaces found! Make sure WinPcap is installed.\n");38 returnNULL;39 }40 41 /*We don't need any more the device list. Free it */42 pcap_freealldevs(alldevs);43 44 returnPy_BuildValue("i",0);45 }46 47 48 static PyObject*open_device(PyObject*self, PyObject*args)49 {50 const char*name;51 char errbuf[PCAP_ERRBUF_SIZE];52 53 if(! PyArg_ParseTuple(args,"s",&name))54 returnNULL;55 /*Open the output device*/56 if( (gfp=pcap_open(name,//name of the device57 100,//portion of the packet to capture (only the first100bytes)58 PCAP_OPENFLAG_PROMISCUOUS,//promiscuous mode59 1000,//read timeout60 NULL,//authentication on the remote machine61 errbuf//error buffer62 ) )==NULL)63 {64 fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n", name);65 returnNULL;66 }67 returnPy_BuildValue("i",0);68 }69 70 71 static PyObject*send_packet(PyObject*self, PyObject*args)72 {73 int len=0;74 const char*pEth=NULL;75 76 int ret,i,j,k=0;77 78 if(! PyArg_ParseTuple(args,"s#",&pEth,&len)){79 printf("Parse arge error,pEth = %p,len = %d\n",pEth,len);80 returnNULL;81 }82 //if(!gfp){83 //fprintf(stderr,"\nInvalid fp,check if device have been opened");84 //returnNULL;85 //}86 assert(gfp);87 #if 088 for(i=len; i>16; i-=16){89 for(j=0; j<16; j++)90 fprintf(stderr,"%02x",(u_char)pEth[k++]);91 fprintf(stderr,"\n");92 }93 for(; i>0; i--)94 fprintf(stderr,"%02x",(u_char)pEth[k++]);95 #endif96 /*Send down the packet*/97 ret=pcap_sendpacket(gfp, pEth, len/*size*/);98 if(ret!=0)99 {100 fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(gfp));101 returnNULL;102 }103 104 returnPy_BuildValue("i",ret);105 }106 107 static PyMethodDef sendPacketMethods[]=108 {109 {"list_device", list_device, METH_VARARGS,"list all devices."},110 {"open_device", open_device, METH_VARARGS,"open the specific device."},111 {"send_packet", send_packet, METH_VARARGS,"send packet use mehtod in winpcap."},112 {NULL, NULL, 0, NULL}113 };114 115 PyMODINIT_FUNC initpsnd()116 {117 Py_InitModule("psnd", sendPacketMethods);118 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值