首页 > 其他分享 >grad-design

grad-design

时间:2023-05-06 20:55:44浏览次数:42  
标签:libnet pcap len char design tcp ip grad

#include <iostream>
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "arpa/inet.h"
#include "pcap.h"
#include "libnet.h"
#include "netinet/ip.h"
#include "netinet/tcp.h"
#include "netinet/ether.h"
using namespace std;
#define HTTP_REQUEST "GET / HTTP/1.1\r\nHost: example.com\r\n\r\n"
void usage() {
    printf("Usage: ./http_request <interface>\n");
}
void got_packet(u_char*args,const pcap_pkthdr*header,const u_char*packet){
    int pkt_len=header->len;
    int min_len=*(int*)args;

    if(pkt_len>min_len){
        FILE *fp = fopen("capture.pcapng", "ab");
        if(fp==NULL){
            fprintf(stderr, "cannot open file");
            exit(1);
        }
        if (fwrite(packet, pkt_len, 1, fp) < 1) {
            fprintf(stderr, "write failed");
            fclose(fp);
            exit(1);
        }
         printf("捕获到长度为%d的数据包,已保存到文件capture.pcap中\n", pkt_len);
    }
}
void timeout_handler(int signo){

    printf("timeout!!!!"); exit(1);
}
uint32_t ack=0;
void packet_handler2(u_char *param,const pcap_pkthdr *header,const u_char *pkt_data) {
    pcap_dumper_t*dumper=(pcap_dumper_t*)param;
    pcap_dump((u_char *) dumper, header, pkt_data);

}
void packet_handler(u_char *param,const pcap_pkthdr *header,const u_char *pkt_data){
    iphdr* ip_hdr;tcphdr* tcp_hdr;uint ip_hdr_len;
    ip_hdr = (iphdr *) (pkt_data + sizeof(ether_header));
    printf("ether header len:%d\n", sizeof(ether_header));
    ip_hdr_len=ip_hdr->ihl*4;
    printf("ip len:%d", ip_hdr_len);
    tcp_hdr = (tcphdr *) (pkt_data + sizeof(ether_header) + ip_hdr_len);
    printf("ack:%x",tcp_hdr->th_seq);
    if (tcp_hdr->th_flags & TH_ACK) {
        ack= ntohl(tcp_hdr->th_seq) ;

    }
}

int main(int argc,char**argv) {


    system("iptables -A OUTPUT -p tcp --tcp-flags RST RST -j DROP");
//    system(" tcpdump -i eth0 -w send_http-$(date +\"%Y-%m-%d-%H-%M-%S\").pcapng "
//           "-c 6 host 10.11.250.175 & ");
//    sleep(1);

    int n = 5;
    char c[n];

    libnet_t *net = libnet_init(LIBNET_RAW4, "eth0", nullptr);

    if (argc != 2) {
        usage();
        return 1;
    }
    char errbuf[PCAP_ERRBUF_SIZE];

    if (net == NULL) {
        fprintf(stderr, "failed to init libnet%s\n", libnet_geterror(net));
        return 1;
    }
    char payload_str[999] = "GET  /yjsjwgl/ HTTP/1.1\r\n"
                            "Host: yjszsxsb.zstu.edu.cn\r\n\r\n";

    uint8_t *payload = (uint8_t *) payload_str;
    uint32_t payload_size = strlen(payload_str);
    cout << "pay size:" << payload_size << endl;
    libnet_ptag_t ip_tag = 0, tcp_tag = 0;
    char *dst_ip_str = "10.11.250.175";
    char *src_ip_str = "192.168.255.128";
    //char*src_ip_str="10.99.93.229";
    uint32_t src_ip = 0, dst_ip = 0;
    src_ip = libnet_name2addr4(net, src_ip_str, LIBNET_RESOLVE);
    dst_ip = libnet_name2addr4(net, dst_ip_str, LIBNET_RESOLVE);
    tcp_tag = libnet_build_tcp(12343, 80, 1, 0, TH_SYN, 14600, 0,
                               0, LIBNET_TCP_H, nullptr, 0, net, 0);
    ip_tag = libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H, 0, 0,
                               0, 64, IPPROTO_TCP, 0, src_ip, dst_ip, nullptr, 0, net, 0);


    pcap_t *desc;
    char err[PCAP_ERRBUF_SIZE];
    bpf_program fp;
    bpf_u_int32 network, mask;
    pcap_lookupnet("eth0", &network, &mask, nullptr);
    desc = pcap_open_live("eth0", BUFSIZ, 0, 10, nullptr);
    pcap_compile(desc, &fp, "tcp and  src host 10.11.250.175"
                            " and src port 80", 0, network);
    pcap_setfilter(desc, &fp);
    int bytes_sent = libnet_write(net);
    pcap_dispatch(desc, 1, packet_handler, NULL);

    net = libnet_init(LIBNET_RAW4, "eth0", nullptr);
    tcp_tag = libnet_build_tcp(12343, 80, 2, ack + 1,
                               TH_ACK | TH_PUSH, 14600, 0, 0, LIBNET_TCP_H, payload,
                               payload_size, net, 0);
    ip_tag = libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H,
                               0, 0, 0, 64, IPPROTO_TCP, 0, src_ip, dst_ip,
                               nullptr, 0, net, 0);
    //usleep(7500);
    char *dev;pcap_t *handle;
    bpf_program bpfProgram;char filter_exp[99]={0};
    bpf_u_int32 bpf_net;int min_len;char *host_ip="10.11.250.175";
    min_len=100;handle=pcap_open_live("eth0",BUFSIZ, 1, 1000, nullptr);
    snprintf(filter_exp, sizeof(filter_exp), "host %s and length>100 and length<10000", host_ip);
    pcap_compile(handle,&bpfProgram,filter_exp,0,network);
    pcap_setfilter(handle, &bpfProgram);
    pcap_dumper_t *dumper;
   dumper= pcap_dump_open(handle, "pcap_save.pcapng");
    bytes_sent = libnet_write(net);


    signal(SIGALRM, timeout_handler);
    alarm(2);

    pcap_loop(handle, -1,pcap_dump , (u_char *) dumper);
   pcap_close(handle);

    cout << "\nsend len:" << bytes_sent;
}

标签:libnet,pcap,len,char,design,tcp,ip,grad
From: https://www.cnblogs.com/xiaohao-coding/p/17378440.html

相关文章

  • spring cloud gradle mybatis
    一、引入依赖在build.gradle文件中的dependencies的对象中引入依赖implementation'org.mybatis.spring.boot:mybatis-spring-boot-starter:2.1.4'二、配置数据库及mybatis在application.yml文件中配置数据库spring:datasource:url:jdbc:mysql://192.168.0.1:3306/dbName......
  • spring cloud、gradle、父子项目、微服务框架搭建---搭建父子级多模块项目,并注册到Eu
    一、新建一个父级gradl模块1.在桌面或新建文件里面,右键,打开IDEA选择Flie>NEW>Module选择Gradle,选择ModuleSDK1.8,选择Java创建完成后,idea中会多出一个项目,如下图示例然后直接关闭当前idea,进入刚才创建好的文件下,再次打开项目可以直接删除src目录二、新建子级gradl模......
  • Material Design UI Widgets
     AndroidL开发者预览支持库提供两个新的Widgets,RecyclerView和CardView。使用这两个Widgets可以显示复杂的Listview和卡片布局,这两个Widgets默认使用Materialdesign。RecyclerView  RecyclerView是一个更高级柔性版本的Listview,RecyclerView是一个能包含很多视图的容器,它能......
  • Material Design WPF DialogHost 实现异步关闭
    在撰写一个按钮处理数据同步的事件时,处理前先弹出一个窗口,告知用户正在处理数据,处理数据后再关闭这个弹窗。 Button按钮的事件代码原先是这样,预想中是处理一些事情后,可以自动关闭弹窗。DialogHost_MySelf就是DialogHost。1this.DialogHost_MySelf.IsOpen=true;2/**......
  • The plugin distribution bundles IDE packages 'com.intellij.uiDesigner.core',
    错误描述:TheplugindistributionbundlesIDEpackages'com.intellij.uiDesigner.core','com.intellij.uiDesigner'.BundlingIDEpackagesisconsideredbadpracticeandmayleadtosophisticatedcompatibilityproblems.Considerexcludingthe......
  • Gradle安装配置教程
    一、安装前检查检查电脑上是否安装JDK,如果没有安装,请查看JDK安装教程:点击查看如果电脑上已经安装JDK,按Win+R键,输入cmd,然后点击确定输入java-version,点击回车,查看JDK安装信息,如果有下面提示信息,说明JDK安装成功二、Gradle下载打开官网下载界面:https://gradle.or......
  • python-Gradio 机器学习演示库
    python-GradioGradio是一个开源的Python库,用于构建机器学习和数据科学演示应用。有了Gradio,你可以围绕你的机器学习模型或数据科学工作流程快速创建一个简单漂亮的用户界面。Gradio适用于以下情况:为客户/合作者/用户/学生演示你的机器学习模型。通过自动共享链接快速部署你的......
  • Three.js#04#Responsive Design&Scenegraph
    参考https://threejs.org/manual/#en/responsive和https://threejs.org/manual/#en/scenegraph前者主要是说怎样创建一个响应式的three.js应用,就是在变化屏幕大小的时候,画面不会畸形。后者是再说,怎么组合小的组件变成一个大的组件(依赖于一个空组件object3D)下面是示例代码:index.......
  • Ant Design的基本使用
    title:08-AntDesign的基本使用publish:trueandt的介绍AntDesign是基于React实现,开发和服务于企业级后台产品。支持环境现代浏览器和IE9及以上(需要polyfills)。支持服务端渲染。ElectronElectron(原名为AtomShell)是GitHub开发的一个开源框架。它允许......
  • SystemVerilog for Design Edition 2 Chapter 2
    SystemVerilogforDesignEdition2Chapter2SystemVerilogDeclarationSpacesVerilogonlyhaslimitedplacesinwhichdesignerscandeclarevariablesandotherdesigninformation.SystemVerilogextendsVerilog’sdeclarationspacesinseveralways.These......