How do i get data of may network card?

How do i get data of may network card?

Page 1 of 1

1 Replies - 1349 Views - Last Post: 22 October 2009 - 07:30 AM Rate Topic: -----

#1 carmenjm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-October 09

How do i get data of may network card?

Posted 22 October 2009 - 06:27 AM

I need to get data of my NIC, because i need know the timing and the packet's exchange. this will be over C++, Java, Linux wherever..

Thank you
Is This A Good Question/Topic? 0
  • +

Replies To: How do i get data of may network card?

#2 UG Cyber  Icon User is offline

  • D.I.C Addict

Reputation: 32
  • View blog
  • Posts: 556
  • Joined: 24-July 08

Re: How do i get data of may network card?

Posted 22 October 2009 - 07:30 AM

View Postcarmenjm, on 22 Oct, 2009 - 05:27 AM, said:

I need to get data of my NIC, because i need know the timing and the packet's exchange. this will be over C++, Java, Linux wherever..

Thank you

You are better off just doing RAW sockets. Here is a sample code in linux / C++
#include <iostream>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <net/if.h>
#define __FAVOR_BSD
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <netinet/udp.h>
#include <string.h>
#include <arpa/inet.h>



using namespace std;

int CreateRawSocket()
{
    int tmpSock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
    return tmpSock;
}

char* itoa(int val, int base){

        static char buf[32] = {0};

        int i = 30;

        for(; val && i ; --i, val /= base)

                buf[i] = "0123456789abcdef"[val % base];

        return &buf[i+1];

}

void DisplayPacketInfo(char * Packet, int PkIndex)
{
    ethhdr * eth = (ethhdr*)Packet;
    int DataSize;
    int len = sizeof(Packet);
    if (eth->h_proto == htons(ETH_P_IP))   //is it an ip packet?
    {
        cout.width(20);
        cout.fill('.');
        cout << left << "Pkt Index    : " << right << PkIndex << endl;
        cout.width(20);
        cout.fill('.');
        cout << left << "Bytes recved : " << right << len << endl;

        ip * iph = (ip*)(Packet + sizeof(ethhdr));
        //Display desired ip info
        cout.width(30);
        cout.fill('.');
        cout << left << "Source ip    : " << right << inet_ntoa(iph->ip_src) << endl;
        cout.width(30);
        cout.fill('.');
        cout << left << "Dest ip      : " << right << inet_ntoa(iph->ip_dst) << endl;
        cout.width(30);
        cout.fill('.');
        cout << left << "Time To Live : " << right << (int)iph->ip_ttl << endl;
        cout.width(30);
        cout.fill('.');
        cout << left << "IP version   : " << right << iph->ip_v << endl;

        if (iph->ip_p == 6)   //is it a tcp packet?
        {
            cout.width(30);
            cout.fill('.');
            cout << left << "Protocol     : " << right << "TCP" << endl;
            tcphdr * tcph = (tcphdr*)(Packet + sizeof(ethhdr) + iph->ip_hl*4);
                    //Now time to display
            cout.width(40);
            cout.fill('.');
            cout << left << "Source Port  : " << right <<ntohs(tcph->th_sport) << endl;
            cout.width(40);
            cout.fill('.');
            cout << left << "Dest Port    : " << right << ntohs(tcph->th_dport) << endl;
            cout.width(40);
            cout.fill('.');
            cout << left << "Window       : " << right << tcph->th_win << endl;
            cout.width(40);
            cout.fill('.');
            cout << left << "Sequence     : " << right << tcph->th_seq << endl;
            cout.width(40);
            cout.fill('.');
            cout << left << "Flags        : ";
            if (tcph->th_flags == TH_SYN)
            {
                cout << "SYN packet" << endl;
            }
            else if (tcph->th_flags == 0x12)
            {
            cout << "SYN ACK packet" << endl;
            }
            else if (tcph->th_flags == TH_ACK)
            {
            cout << "ACK packet" << endl;
            }
            else
            {
                cout << "Unknown / Not Set" << endl;
            }
            char * Payload = (Packet + sizeof(ethhdr) + iph->ip_hl*4 + sizeof(tcphdr));
            if (len > (sizeof(Payload) + sizeof(ethhdr) + iph->ip_hl*4 + sizeof(tcphdr)))
            {
                cout << "Error dumping full packet!" << endl;
                getchar();
            }
            cout << "Packet Payload-----------------------------------------------------" << endl;
            cout << Payload << endl;
            cout << "End Packet Payload-------------------------------------------------" << endl << endl << endl;
            if (strstr("http", Payload))
            {
                getchar();
            }
        }
        else if (iph->ip_p == IPPROTO_UDP)
        {
            cout.width(30);
            cout.fill('.');
            cout << left << "Protocol    : " << right << "UDP" << endl;
            udphdr * udp = (udphdr*)(Packet + sizeof(ethhdr) + iph->ip_hl*4);
            cout.width(40);
            cout.fill('.');
            cout << left << "Source port : "  << right << ntohs(udp->uh_sport) << endl;
            cout.width(40);
            cout.fill('.');
            cout << left << "Dest port   : " << right << ntohs(udp->uh_dport) << endl << endl;
            char * pLoad = (Packet + sizeof(ethhdr) + (iph->ip_hl*4) + sizeof(udphdr));
            cout << "Payload--------------------------------------------------------------" << endl;
            cout << pLoad << endl;
            cout << "End Payload----------------------------------------------------------" << endl << endl << endl;
        }
        else
        {
            cout << "Not a tcp / udp packet" << endl;
        }
    }
    else if (eth->h_proto == 0x0806)
    {
        cout << "ARP" << endl;
    }
    else
    {
        cout << "eth->h_proto = " << eth->h_proto << endl;
    }
}


void BindToInterface(char * Interface, int sock)
{
    sockaddr_ll sll;
    ifreq ifr;
    int sllSize = sizeof(sockaddr_ll);

    strncpy((char*)ifr.ifr_name, Interface, IFNAMSIZ);
    if (ioctl(sock, SIOGIFINDEX, &ifr) < 0)
    {
        perror("ioctl:");
    }
    sll.sll_family = AF_PACKET;
    sll.sll_ifindex = ifr.ifr_ifindex;
    sll.sll_protocol = htons(ETH_P_IP);

    int one = 1;
    int * val = &one;
    if (setsockopt(sock, SOL_SOCKET, PACKET_MR_PROMISC, val, sizeof(one)) < 0)
    {
        perror("setsockopt:");
    }
    if (bind(sock, (sockaddr*)&sll, (socklen_t)sllSize) < 0)
    {
        perror("bind:");
    }
    return;
}

int main(int argc, char * argv[])
{
    if (argc <= 1)
    {
        cout << "Usage: ";
        cout << argv[0];
        cout << " <Device>" << endl;
        return 0;
    }
    int len;
    int PkIndex;
    char Packet[3072];

    int sock = CreateRawSocket();
    if (sock < 0)
    {
        cout << "Run it as root dumb ass..." << endl;
        return 1;
    }
    BindToInterface(argv[1], sock);
    cout << "Waiting for packet" << endl;
    while((len = recvfrom(sock, Packet, 3072, 0, NULL, NULL) > 0))
    {
        DisplayPacketInfo(Packet, PkIndex);
        PkIndex++;
    }
    perror("recvfrom:");
    return 0;
}



using RAW sockets gives you complete power over the packets. this may be good for ya
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1