[Winpcap-bugs] wpcap?

Erlo Meister MeisterEI at tut.ac.za
Fri May 12 12:32:33 GMT 2006


To whom it may concern,

I have written a test program that will send a tcp packet.

This program uses a lot of sample code provided in wpdpack.

WinPcap_3_1 is currently installed on Win2k Professional SP4.

This program does send a tcp packet but unfortunately the tcp seqnum and tcp acknum fields are not sent as they where set in the program.

This occurs on numerous network adapters and when intercepted with ethereal it can be seen that the tcp seqnum and tcp acknum fields are not sent as they where set in the program.

I am still fairly new to c programming, so please excuse me if there is an obvious mistake in the program code.

/*
 * Copyright (c) 1999 - 2003
 * NetGroup, Politecnico di Torino (Italy)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Politecnico di Torino nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include "pcap.h"
#include "remote-ext.h"
#include <ws2tcpip.h>

#define IP 0x0800 // type: IP //
#define SYN 0x02

#pragma pack(push,1)

typedef struct eth_header
{
 unsigned char eth_dst[6];
 unsigned char eth_src[6];
 unsigned short eth_type;
}ETH_HEADER;

typedef struct ip_header
{
 unsigned char ip_ver_hlen; // version 4 bits + hlen 4 bits
 unsigned char ip_diffserv;
 unsigned short ip_tlen;
 unsigned short ip_id;
 unsigned short ip_flag_frag;
 unsigned char ip_ttl;
 unsigned char ip_protocol;
 unsigned short ip_cksum;
 unsigned long ip_src;
 unsigned long ip_dst;
}IP_HEADER;

typedef struct tcp_header
{
    unsigned short tcp_sport;
    unsigned short tcp_dport;
    unsigned long tcp_seqnum;
    unsigned long tcp_acknum;
    unsigned char tcp_hlen; // hlen 4 bits + reserved 4 bits
    unsigned char tcp_flags; // reserved 2 bits + control 6 bits
    unsigned short tcp_win;
    unsigned short tcp_cksum;
    unsigned short tcp_urgptr;
}TCP_HEADER;

#pragma pack(push)

// Calculate Checksum
unsigned short csum (unsigned short *buf, int nwords) {
        unsigned long sum=0;

  for( sum=0; nwords > 0; nwords-- )
                sum += *buf++;
        sum = (sum >> 16) + (sum & 0xffff);
        sum += (sum >> 16);
  return (u_short)~sum;
}

main() {

    pcap_if_t *alldevs;
    pcap_if_t *d;
    pcap_t *adhandle;
    char errbuf[PCAP_ERRBUF_SIZE];

 ETH_HEADER eth_header;
 IP_HEADER ip_header;
 TCP_HEADER tcp_header;

 char sendbuf[65535];

 unsigned char dst_mac[6] = {0x00,0x80, 0xAD, 0x77, 0x45, 0x10};
 unsigned char src_mac[6] = {0xAA,0xBB, 0xCC, 0xDD, 0xEE, 0xFF};

    int inum;
 int i = 0;

 unsigned short tcp_header_cksum[16];
 unsigned short ip_header_cksum[10];

 unsigned short pseudoheader_protocol;
 unsigned short tcp_tlen;


    if (pcap_findalldevs(&alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }

    /* Print the list */
    for(d=alldevs; d; d=d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description available)\n");
    }

    if(i==0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return -1;
    }

    printf("Enter the interface number (1-%d):",i);
    scanf("%d", &inum);

    if(inum < 1 || inum > i)
    {
        printf("\nInterface number out of range.\n");
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }

    /* Jump to the selected adapter */
    for(d=alldevs, i=0; i< inum-1 ;d=d->next, i++);

    /* Open the adapter */
    if ( (adhandle= pcap_open_live(d->name, // name of the device
                             65536,      // portion of the packet to capture.
                             1,          // promiscuous mode
                             1000,   // read timeout
                             errbuf   // error buffer
                             ) ) == NULL)
    {
        fprintf(stderr,"\nUnable to open the adapter. %s is not supported by WinPcap\n");
        /* Free the device list */
        pcap_freealldevs(alldevs);
        return -1;
    }

 // Fill in eth hdr //
 memcpy(eth_header.eth_dst, dst_mac, 6);
 memcpy(eth_header.eth_src, src_mac, 6);
 eth_header.eth_type = htons(IP);

 // Fill in ip hdr //
 ip_header.ip_ver_hlen = 0x45;
 ip_header.ip_diffserv = 0;
 ip_header.ip_tlen = htons(40); // 20 byte ip header + 20 byte tcp header
 ip_header.ip_id = htons(IP);
 ip_header.ip_flag_frag = 0x0;
 ip_header.ip_ttl = 0xff;
 ip_header.ip_protocol = 0x06;
 ip_header.ip_cksum = 0x00;
 ip_header.ip_src = inet_addr("192.168.0.254");
 ip_header.ip_dst = inet_addr("192.168.0.101");

  // Calculate the IP Header Checksum
  memset(ip_header_cksum, 0, 20);
  memcpy(ip_header_cksum, &ip_header, 20);
  ip_header.ip_cksum = csum( ip_header_cksum, 10 );

 // Fill in tcp hdr //
    tcp_header.tcp_sport = htons(1050);
    tcp_header.tcp_dport = htons(445);
    tcp_header.tcp_seqnum = htonl(0x01);
    tcp_header.tcp_acknum = htonl(0x01);
    tcp_header.tcp_hlen = 0x50; // 20 byte tcp header
    tcp_header.tcp_flags = SYN;
    tcp_header.tcp_win = htons(65535);
    tcp_header.tcp_cksum = 0x00;
    tcp_header.tcp_urgptr = 0x00;

  // Construct the tcp pseudo-header for checksum calculation

 pseudoheader_protocol = htons(0x06);
 tcp_tlen = htons(0x14);

 memset(tcp_header_cksum, 0, 32);
 memcpy(tcp_header_cksum, &tcp_header, 20);
 memcpy(&tcp_header_cksum[10], &ip_header.ip_src, 4);
 memcpy(&tcp_header_cksum[12], &ip_header.ip_dst, 4);
 memcpy(&tcp_header_cksum[14], &pseudoheader_protocol, 2);
 memcpy(&tcp_header_cksum[15], &tcp_tlen, 2);
 tcp_header.tcp_cksum = csum( tcp_header_cksum, 16 );

 memset(sendbuf,0,sizeof(sendbuf));
 memcpy(sendbuf, &eth_header, sizeof(eth_header));
 memcpy(sendbuf + sizeof(eth_header), &ip_header, sizeof(ip_header));
 memcpy(sendbuf + sizeof(eth_header) + sizeof(ip_header), &tcp_header, sizeof(tcp_header));

 printf("\nSent TCP SYN packet, no fragmentation\n");

    // Send down the packet //
    if (pcap_sendpacket(adhandle, sendbuf,sizeof(eth_header)+sizeof(ip_header)+sizeof(tcp_header)) != 0)
    {
        fprintf(stderr,"\nError sending the packet: \n", pcap_geterr(adhandle));
        return -1;
    }

 pcap_close(adhandle);

 return (0);
}

Regards

Erlo

 



------------------------------------------------------------------
Tshwane University of Technology
------------------------------------------------------------------
This email is sent and received in terms of the Electronic
Communications Policy of Tshwane University of Technology.
In line with this policy, this email is private, priviledged and
confidential. The full text of the Electronic Mail Disclaimer
can be seen on the TUT web site at 
http://www.tut.ac.za/goto/emaildisclaimer
or obtained by phoning (012) 382-5911

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.winpcap.org/pipermail/winpcap-bugs/attachments/20060512/f8dc2731/attachment.htm


More information about the Winpcap-bugs mailing list