SHARE
TWEET

Untitled

a guest May 26th, 2019 145 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. void SetUserData()
  102. {
  103.  
  104.     cout << "-> Type Source MAC (XX:XX:XX:XX:XX:XX) :" << endl;
  105.     scanf("%x:%x:%x:%x:%x:%x", &sourceMac[0], &sourceMac[1], &sourceMac[2], &sourceMac[3], &sourceMac[4], &sourceMac[5]);
  106.  
  107.  
  108.     do
  109.     {
  110.         cout << "-> Write the room number: " << endl;
  111.         cin >> roomNumber;
  112.  
  113.         if(roomNumber >= 0 && roomNumber <= 255)
  114.         {
  115.             hasRoomAvailable = true;
  116.         }
  117.         else
  118.         {
  119.             hasRoomAvailable = false;
  120.             cout << "-> Select another room." << endl;
  121.         }
  122.     }
  123.     while(hasRoomAvailable == false);
  124.  
  125.     cout << "-> Type an alias: " << endl;
  126.  
  127.     fflush(stdin);
  128.     gets(userAlias);
  129.     fflush(stdin);
  130.  
  131.     if(strlen(userAlias) < USERALIASLENGTH)
  132.     {
  133.         for(int i = strlen(userAlias); i < USERALIASLENGTH; i++)
  134.         {
  135.             userAlias[i] = '\0';
  136.         }
  137.     }
  138.  
  139.     cout << endl;
  140. }
  141.  
  142.  
  143. bool CheckSourceMac()
  144. {
  145.     for (int i = 0; i < SOURCEMACLENGHT; i++)
  146.     {
  147.         if (sourceMac[i] < 0 || sourceMac[i]>255)
  148.         {
  149.             return false;
  150.         }
  151.     }
  152.  
  153.     return true;
  154. }
  155.  
  156. void ShowTypedData()
  157. {
  158.     cout << "-> Data typed: " << endl << endl;
  159.  
  160.     cout << "-> Origin MAC Address: " << endl;
  161.  
  162.     for(int i = 0; i < SOURCEMACLENGHT; i++)
  163.     {
  164.         printf("%x", sourceMac[i]);
  165.  
  166.         if(i + 1 == SOURCEMACLENGHT)
  167.         {
  168.             cout << endl;
  169.         }
  170.         else
  171.         {
  172.             cout << ":";
  173.         }
  174.     }
  175.  
  176.     cout << "-> Room Number: " << endl;
  177.  
  178.     cout << roomNumber << endl;
  179.  
  180.     cout << "-> Alias: " << endl;
  181.  
  182.     for(int i = 0; i < sizeof(userAlias); i++)
  183.     {
  184.         cout << userAlias[i];
  185.  
  186.         if(i + 1 == sizeof(userAlias))
  187.         {
  188.             cout << endl << endl;
  189.         }
  190.     }
  191. }
  192.  
  193.  
  194. void SendMessage(pcap_t *adhandle, int *sourceMac, int roomNumber, char *userAlias)
  195. {
  196.     char textMsg[1000];
  197.     int newMessageSize = 0;
  198.  
  199.     fflush(stdin);
  200.     gets(textMsg);
  201.     fflush(stdin);
  202.  
  203.     if (strlen(textMsg) < 37)
  204.     {
  205.         for (int i = strlen(textMsg); i <= 37; i++)
  206.         {
  207.             textMsg[i] = '\0';
  208.         }
  209.  
  210.         newMessageSize = 37;
  211.     }
  212.     else if (strlen(textMsg) > 993)
  213.     {
  214.         newMessageSize = 993;
  215.     }
  216.     else
  217.     {
  218.         newMessageSize = strlen(textMsg);
  219.     }
  220.  
  221.     u_char packet[newMessageSize + 23];
  222.  
  223.     for (int i = 0; i <= 5; i++)
  224.     {
  225.         packet[i] = 255;
  226.     }
  227.  
  228.     for (int i = 6, counter = 0; i <= 11; i++, counter++)
  229.     {
  230.         packet[i] = sourceMac[counter];
  231.     }
  232.  
  233.     packet[12] = 0x07;
  234.     packet[13] = 0x00;
  235.     packet[14] = roomNumber;
  236.  
  237.     for (int i = 15, counter = 0; i <= 27; i++, counter++)
  238.     {
  239.         packet[i] = userAlias[counter];
  240.     }
  241.  
  242.     for (int i = 0; i < newMessageSize; i++)
  243.     {
  244.         packet[i + 27] = (u_char)textMsg[i];
  245.     }
  246.  
  247.     /// Send down the packet.
  248.     if (pcap_sendpacket(
  249.         adhandle,       /// Adapter.
  250.         packet,         /// Buffer with the packet.
  251.         sizeof(packet)  /// Size.
  252.     ) != 0)
  253.     {
  254.         cout << stderr << "-> Error sending the packet: " << pcap_geterr(adhandle) << endl;
  255.     }
  256.  
  257.     for (int i = 15; i <= 22; i++)
  258.     {
  259.         cout << packet[i];
  260.     }
  261.  
  262.     cout << " >> ";
  263.  
  264.     for (int i = 23; i < sizeof(packet); i++)
  265.     {
  266.         cout << packet[i];
  267.     }
  268.  
  269.     cout << endl;
  270. }
  271.  
  272. void ListenMessages(struct pcap_pkthdr *header, const u_char *pkt_data)
  273. {
  274.     for (int i = 15; i <= 22; i++)
  275.     {
  276.         cout << pkt_data[i];
  277.     }
  278.  
  279.     cout << " >> ";
  280.  
  281.     for (int i = 23; i < header->len; i++)
  282.     {
  283.         cout << pkt_data[i];
  284.     }
  285.  
  286.     cout << endl;
  287. }
  288.  
  289.  
  290. void ChatSendRecieve()
  291. {
  292.  
  293.     cout << "-> Type the message: " << endl;
  294.  
  295.     while((res = pcap_next_ex(adhandle, &header, &pkt_data)) >= 0)
  296.     {
  297.         if(kbhit())
  298.             SendMessage(adhandle, sourceMac, roomNumber, userAlias);
  299.  
  300.         if (res == 0)
  301.             continue;
  302.  
  303.         for(int i = 0; i < SOURCEMACLENGHT; i++)
  304.         {
  305.             if(sourceMac[i] == pkt_data[i + SOURCEMACLENGHT])
  306.                 hasSameMac = true;
  307.  
  308.             else
  309.             {
  310.                 hasSameMac = false;
  311.                 break;
  312.             }
  313.         }
  314.  
  315.         if(pkt_data[12] == 07 && pkt_data[13] == 00 && pkt_data[14] == roomNumber && !hasSameMac)
  316.         {
  317.             ListenMessages(header, pkt_data); //We check here if the packet is from the same room number and doesn't have the same mac address.
  318.         }
  319.     }
  320.  
  321.     if(res == -1)
  322.     {
  323.         cout << "-> Error reading the packets: " << pcap_geterr(adhandle) << endl;
  324.     }
  325.  
  326.     pcap_close(fp);
  327. }
  328.  
  329.  
  330. int main()
  331. {
  332.     ListAllDevices();
  333.     SetUserData();
  334.     ShowTypedData();
  335.     ChatSendRecieve();
  336.  
  337.     return 0;
  338. }
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