SHARE
TWEET

Untitled

a guest May 26th, 2019 431 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #define SOURCEMACLENGHT 6
  2. #define USERALIASLENGTH 12
  3.  
  4.  
  5.  
  6. #include "pcap.h"
  7. #include "conio.h"
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <iostream>
  11.  
  12. using namespace std;
  13.  
  14.  
  15.  
  16. pcap_if_t *alldevs;
  17. pcap_if_t *d;
  18. pcap_t *fp;
  19. pcap_t *adhandle;
  20. char errbuf[PCAP_ERRBUF_SIZE];
  21.  
  22. int roomNumber;
  23. int interfaceNumber;
  24. int sourceMac[SOURCEMACLENGHT];
  25. bool hasRoomAvailable;
  26. bool hasSameMac;
  27. char userAlias[USERALIASLENGTH];
  28.  
  29. int res;
  30. struct pcap_pkthdr *header;
  31. const u_char *pkt_data;
  32.  
  33.  
  34. void ListAllDevices()
  35. {
  36.     cout << "-> Type an interface number: " << endl << endl;
  37.  
  38.     if(pcap_findalldevs(&alldevs, errbuf) == -1)
  39.     {
  40.         cout << stderr << "-> Error in pcap_findalldevs: " << errbuf << endl;
  41.  
  42.         exit(1);
  43.     }
  44.  
  45.     int index = 1;
  46.  
  47.     for(d = alldevs; d; d = d -> next)
  48.     {
  49.         cout << index++ << ". " << d -> name;
  50.  
  51.         if(d -> description)
  52.         {
  53.             cout << "\t" << d -> description << endl;
  54.         }
  55.         else
  56.         {
  57.             cout << "-> No description available." << endl;
  58.         }
  59.     }
  60.  
  61.     if(index == 0)
  62.     {
  63.         cout << "-> No interfaces found! Make sure WinPcap is installed." << endl;
  64.     }
  65.  
  66.     cout << "-> Your choice: " << endl;
  67.     cin >> interfaceNumber;
  68.  
  69.     if(interfaceNumber < 1 || interfaceNumber > index)
  70.     {
  71.         cout << "-> Error. Interface number out of range." << endl;
  72.  
  73.         /// Free the device list.
  74.         pcap_freealldevs(alldevs);
  75.     }
  76.  
  77.     /// Jump to the selected adapter.
  78.     for(d = alldevs, index = 0; index < interfaceNumber - 1; d = d -> next, index++);
  79.  
  80.     /// Open the device.
  81.     if((adhandle = pcap_open_live(
  82.                                     d -> name,  /// Name of the device.
  83.                                     65536,      /// Portion of the packet to capture. 65536 guarantees that the whole packet will be captured on all the link layers.
  84.                                     1,          /// Promiscuous mode.
  85.                                     1000,       /// Read timeout. Authentication on the remote machine.
  86.                                     errbuf      /// Error buffer.
  87.                                     )) == NULL)
  88.     {
  89.         cout << stderr << "-> Error. Unable to open the adapter." << d -> name << " is not supported by WinPcap." << endl;
  90.  
  91.         /// Free the device list.
  92.         pcap_freealldevs(alldevs);
  93.     }
  94.  
  95.     /// At this point, we don't need any more the device list. Free it.
  96.     pcap_freealldevs(alldevs);
  97.  
  98.     cout << endl;
  99. }
  100.  
  101. bool CheckSourceMac()
  102. {
  103.     for (int i = 0; i < SOURCEMACLENGHT; i++)
  104.     {
  105.         if (sourceMac[i] < 0 || sourceMac[i]>255)
  106.         {
  107.             cout << "-> Not a valid MAC Address, enter a valid one. " << endl;
  108.             return false;
  109.         }
  110.     }
  111.  
  112.     return true;
  113. }
  114.  
  115. void SetUserData()
  116. {
  117.  
  118.     do
  119.     {
  120.         cout << "-> Type Source MAC (XX:XX:XX:XX:XX:XX) :" << endl;
  121.         scanf("%x:%x:%x:%x:%x:%x", &sourceMac[0], &sourceMac[1], &sourceMac[2], &sourceMac[3], &sourceMac[4], &sourceMac[5]);
  122.     } while(!CheckSourceMac());
  123.  
  124.  
  125.     do
  126.     {
  127.         cout << "-> Write the room number: " << endl;
  128.         cin >> roomNumber;
  129.  
  130.         if(roomNumber >= 0 && roomNumber <= 255)
  131.         {
  132.             hasRoomAvailable = true;
  133.         }
  134.         else
  135.         {
  136.             hasRoomAvailable = false;
  137.             cout << "-> Select another room." << endl;
  138.         }
  139.     }
  140.     while(hasRoomAvailable == false);
  141.  
  142.     cout << "-> Type an alias: " << endl;
  143.  
  144.     fflush(stdin);
  145.     gets(userAlias);
  146.     fflush(stdin);
  147.  
  148.     if(strlen(userAlias) < USERALIASLENGTH)
  149.     {
  150.         for(int i = strlen(userAlias); i < USERALIASLENGTH; i++)
  151.         {
  152.             userAlias[i] = '\0';
  153.         }
  154.     }
  155.  
  156.     cout << endl;
  157. }
  158.  
  159.  
  160.  
  161.  
  162. void ShowTypedData()
  163. {
  164.     cout << "-> Data typed: " << endl << endl;
  165.  
  166.     cout << "-> Origin MAC Address: " << endl;
  167.  
  168.     for(int i = 0; i < SOURCEMACLENGHT; i++)
  169.     {
  170.         printf("%x", sourceMac[i]);
  171.  
  172.         if(i + 1 == SOURCEMACLENGHT)
  173.         {
  174.             cout << endl;
  175.         }
  176.         else
  177.         {
  178.             cout << ":";
  179.         }
  180.     }
  181.  
  182.     cout << "-> Room Number: " << endl;
  183.  
  184.     cout << roomNumber << endl;
  185.  
  186.     cout << "-> Alias: " << endl;
  187.  
  188.     for(int i = 0; i < sizeof(userAlias); i++)
  189.     {
  190.         cout << userAlias[i];
  191.  
  192.         if(i + 1 == sizeof(userAlias))
  193.         {
  194.             cout << endl << endl;
  195.         }
  196.     }
  197. }
  198.  
  199.  
  200. void SendMessage(pcap_t *adhandle, int *sourceMac, int roomNumber, char *userAlias)
  201. {
  202.     char textMsg[1000];
  203.     int newMessageSize = 0;
  204.  
  205.     fflush(stdin);
  206.     gets(textMsg);
  207.     fflush(stdin);
  208.  
  209.     if (strlen(textMsg) < 37)
  210.     {
  211.         for (int i = strlen(textMsg); i <= 37; i++)
  212.         {
  213.             textMsg[i] = '\0';
  214.         }
  215.  
  216.         newMessageSize = 37;
  217.     }
  218.     else if (strlen(textMsg) > 993)
  219.     {
  220.         newMessageSize = 993;
  221.     }
  222.     else
  223.     {
  224.         newMessageSize = strlen(textMsg);
  225.     }
  226.  
  227.     u_char packet[newMessageSize + 23];
  228.  
  229.     for (int i = 0; i <= 5; i++)
  230.     {
  231.         packet[i] = 255;
  232.     }
  233.  
  234.     for (int i = 6, counter = 0; i <= 11; i++, counter++)
  235.     {
  236.         packet[i] = sourceMac[counter];
  237.     }
  238.  
  239.     packet[12] = 0x07;
  240.     packet[13] = 0x00;
  241.     packet[14] = roomNumber;
  242.  
  243.     for (int i = 15, counter = 0; i <= 27; i++, counter++)
  244.     {
  245.         packet[i] = userAlias[counter];
  246.     }
  247.  
  248.     for (int i = 0; i < newMessageSize; i++)
  249.     {
  250.         packet[i + 27] = (u_char)textMsg[i];
  251.     }
  252.  
  253.     /// Send down the packet.
  254.     if (pcap_sendpacket(
  255.         adhandle,       /// Adapter.
  256.         packet,         /// Buffer with the packet.
  257.         sizeof(packet)  /// Size.
  258.     ) != 0)
  259.     {
  260.         cout << stderr << "-> Error sending the packet: " << pcap_geterr(adhandle) << endl;
  261.     }
  262.  
  263.     for (int i = 15; i <= 22; i++)
  264.     {
  265.         cout << packet[i];
  266.     }
  267.  
  268.     cout << " >> ";
  269.  
  270.     for (int i = 23; i < sizeof(packet); i++)
  271.     {
  272.         cout << packet[i];
  273.     }
  274.  
  275.     cout << endl;
  276. }
  277.  
  278. void ListenMessages(struct pcap_pkthdr *header, const u_char *pkt_data)
  279. {
  280.     for (int i = 15; i <= 22; i++)
  281.     {
  282.         cout << pkt_data[i];
  283.     }
  284.  
  285.     cout << " >> ";
  286.  
  287.     for (int i = 23; i < header->len; i++)
  288.     {
  289.         cout << pkt_data[i];
  290.     }
  291.  
  292.     cout << endl;
  293. }
  294.  
  295.  
  296. void ChatSendRecieve()
  297. {
  298.  
  299.     cout << "-> Type the message: " << endl;
  300.  
  301.     while((res = pcap_next_ex(adhandle, &header, &pkt_data)) >= 0)
  302.     {
  303.         if(kbhit())
  304.             SendMessage(adhandle, sourceMac, roomNumber, userAlias);
  305.  
  306.         if (res == 0)
  307.             continue;
  308.  
  309.         for(int i = 0; i < SOURCEMACLENGHT; i++)
  310.         {
  311.             if(sourceMac[i] == pkt_data[i + SOURCEMACLENGHT])
  312.                 hasSameMac = true;
  313.  
  314.             else
  315.             {
  316.                 hasSameMac = false;
  317.                 break;
  318.             }
  319.         }
  320.  
  321.         if(pkt_data[12] == 07 && pkt_data[13] == 00 && pkt_data[14] == roomNumber && !hasSameMac)
  322.         {
  323.             ListenMessages(header, pkt_data); //We check here if the packet is from the same room number and doesn't have the same mac address.
  324.         }
  325.     }
  326.  
  327.     if(res == -1)
  328.     {
  329.         cout << "-> Error reading the packets: " << pcap_geterr(adhandle) << endl;
  330.     }
  331.  
  332.     pcap_close(fp);
  333. }
  334.  
  335.  
  336. int main()
  337. {
  338.     ListAllDevices();
  339.     SetUserData();
  340.     ShowTypedData();
  341.     ChatSendRecieve();
  342.  
  343.     return 0;
  344. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top