毕设

#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");
}

uint32_t ack=0;
void packet_handler(u_char *param,const pcap_pkthdr *header,const u_char *pkt_data){
//    uint8_t *payload;
//    uint32_t  payload_size;
//
//    payload=nullptr;
//    payload_size=0;
//    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);




   // cout<<*pkt_data<<endl;
    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) ;
//        printf("ack:%x,%u",ack,ack);
//        tcp_tag = libnet_build_tcp(12345, 80, 1112,ack+1, TH_ACK, 14600, 0, 0, LIBNET_TCP_H + payload_size, payload,
//                                   payload_size, net, 0);
//        ip_tag = libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H + payload_size, 0, 0, 0, 64, IPPROTO_TCP, 0, src_ip, dst_ip,
//                                   payload, payload_size, net, 0);
//        //usleep(7500);
//        libnet_write(net);

    }
}

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


    system("iptables -A OUTPUT -p tcp --tcp-flags RST RST -j DROP");
    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[9999]="GET/HTTP/1.1\n"
//                     "Host: yjszsxsb.zstu.edu.cn\n"
//                     "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0\n"
//                     "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8\n"
//                     "Accept-Language: en-US,en;q=0.5\n"
//                     "Accept-Encoding: gzip, deflate\n"
//                     "Connection: keep-alive\n"
//                     "Upgrade-Insecure-Requests: 1";

char payload_str[999]="GET/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;

//    payload=nullptr;
//    payload_size=0;


//    payload_size=strlen(HTTP_REQUEST);
//    payload=(uint8_t*)HTTP_REQUEST;
    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);
    //http://rscehr.zstu.edu.cn:8888/templates/index/hcmlogon.jsp;jsessionid=193E694BC96FF533291306172488CED4
    //libnet_ptag_t tcp_opt_tag=libnet_build_tcp_options(payload,payload_size,net,0);

    //----------==========--rst
//    tcp_tag = libnet_build_tcp(12346, 80,0, 0, TH_RST, 14600, 0, 0, LIBNET_TCP_H + payload_size, payload,
//                               payload_size, net, 0);
//    ip_tag = libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H + payload_size, 0, 0, 0, 64, IPPROTO_TCP, 0, src_ip, dst_ip,
//                               nullptr, 0, net, 0);
//    libnet_write(net);


        tcp_tag = libnet_build_tcp(12348, 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 *pcap = pcap_open_live(argv[1], 65535, 1, 1000, errbuf);
    pcap_t*desc;bpf_u_int32 network,mask;bpf_program fp;
    pcap_lookupnet("eth0",&network,&mask,errbuf);
    pcap_compile(pcap, &fp, "tcp", 0, network);
    pcap_setfilter(pcap,&fp);


    pcap_dumper_t *dumper = pcap_dump_open(pcap, "http_response.pcap");
    if(dumper==NULL){
        fprintf(stderr, "failed to open pcap dump file:%s\n", pcap_geterr(pcap));
        pcap_close(pcap);
        libnet_destroy(net);return 1;
    }*/
   /* pcap_pkthdr header;
    const u_char *packet;

    pcap_pkthdr*hdr;*/
    pcap_t*desc;char err[PCAP_ERRBUF_SIZE];
    bpf_program fp;bpf_u_int32 network,mask;
    desc = pcap_open_live("eth0", BUFSIZ, 0, 10, nullptr);
    pcap_lookupnet("eth0", &network, &mask, 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

    pcap_dispatch(desc, 1, packet_handler, NULL);
    //cout<<"ack:"<<ack;






//-----second packet sent;
    net=libnet_init(LIBNET_RAW4,"eth0", nullptr);
    tcp_tag = libnet_build_tcp(12348, 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);
  bytes_sent= libnet_write(net);
    cout<<"\nsend len:"<<bytes_sent;

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

    /*char* payload= "hello";uint32_t payload_size= sizeof(payload);*/

//    char payload_str[9999]="GET / HTTP/1.1\n"
//                           "Host: yjszsxsb.zstu.edu.cn\n"
//                           "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Firefox/102.0\n"
//                           "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8\n"
//                           "Accept-Language: en-US,en;q=0.5\n"
//                           "Accept-Encoding: gzip, deflate\n"
//                           "Connection: keep-alive\n"
//                           "Upgrade-Insecure-Requests: 1";
//    uint8_t *payload=(uint8_t *)payload_str;
//    uint32_t payload_size = strlen(payload_str)+1;

//    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);



//=======push request

//    tcp_tag = libnet_build_tcp(12343, 80,3, ack+1, TH_PUSH|TH_ACK, 14600, 0, 0, LIBNET_TCP_H + payload_size, (uint8_t *)payload,
//                               payload_size, net, 0);
//    if (tcp_tag == -1) {
//        fprintf(stderr, "libnet_build_tcp() failed: %s\n", libnet_geterror(net));
//
//    }
//    ip_tag = libnet_build_ipv4(LIBNET_IPV4_H + LIBNET_TCP_H + payload_size, 0, 0, 0, 64, IPPROTO_TCP, 0, src_ip, dst_ip,
//                               nullptr, 0, net, 0);
//    if (ip_tag == -1) {
//        fprintf(stderr, "libnet_build_ipv4() failed: %s\n", libnet_geterror(net));
//
//    }
//     bytes_sent = libnet_write(net);






    //send raw http;failed
//    pcap_t*handle;pcap_pkthdr header;
//    u_char *packet;
//    handle = pcap_open_offline("/home/get.pcapng", nullptr);
//    packet = const_cast<u_char *>(pcap_next(handle, &header));
//    u_int32_t  length;
//    length=header.len;
//    libnet_write_raw_ipv4(net, packet, length);





//    while ((packet=pcap_next(pcap,&header))!=NULL){
//        pcap_dump((u_char *) dumper, &header, packet);
//    }
//    pcap_dump_close(dumper);
//    pcap_close(pcap);
//    libnet_destroy(net);
//    printf("success");
    

}

posted on 2023-04-28 23:27  shenhshihao  阅读(29)  评论(0编辑  收藏  举报

导航