grad-design

发布时间 2023-05-06 20:55:53作者: shenhshihao
#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;
}