dakotav

IsaLogParseMain.cpp

Dec 11th, 2013
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.66 KB | None | 0 0
  1. /****************************************************
  2. ** ISA Log Parser                                  **
  3. ** Coded by: Dakota Vincent                        **
  4. ** This program takes the log files of the         **
  5. ** Enterasys hi-Path Wireless controllers, and the **
  6. ** Microsoft ISA Logs and parses them to determine **
  7. ** if there was any violations of approved sites   **
  8. ** and if so tells us who based on login and IP.   **
  9. ****************************************************/
  10.  
  11. #include <stdio.h>  //Lots of includes
  12. #include <iostream>
  13. #include <fstream>
  14. #include <string>
  15. #include <sstream>
  16. #include <algorithm>
  17. #include <iomanip>
  18.  
  19. using namespace std;
  20.  
  21. const char DELM=','; //delimiter in most cases a ','
  22.  
  23. void isaParse();
  24. void hipathParse();
  25. int reportStudents(string ipaddress);
  26. /*
  27. The array fields are
  28. 0 Event Id
  29. 1 Event Type (1 is Reg, 2 is Login, 3 is DeReg)
  30. 2 IP
  31. 3 MAC
  32. 4 Register Time
  33. 5 De-Register Time
  34. 6 Login Time
  35. 7 Username
  36. */
  37. string eventArry[3500][8][30];     // The HiPath event arry, contains all the wireless events.
  38.  
  39. int results[3500];                 // Array to hold the results of the StudentReport function
  40. string badwordarray[2000][20];     // Array of our Bad Words
  41. int sizeofbadword[1000];           // How many letters in each bad word
  42.  
  43. int main() {
  44.  
  45.     system("isa2logbackup.bat"); // Run batch file to create our generic files to process
  46.    
  47.     hipathParse(); // This function parses the HiPath Syslog
  48.      
  49.     isaParse();    // This funcetion parses the ISA 2 log
  50.    
  51.     //system("pause");
  52.          
  53.     return 0;
  54. }
  55.  
  56. void isaParse()
  57. {
  58.     //ISA Parsing function
  59.  
  60.     string line,specificline,linewrite,foundterm,ipaddr,compare1,compare2;  // String to hold stuff from ISA log
  61.     istringstream convert;
  62.  
  63.     ifstream myfile;
  64.     myfile.open("ISA2LOG.TXT"); // Open our input files.
  65.     ifstream badwords;
  66.     badwords.open("badwords.txt");
  67.  
  68.     ofstream outscan;
  69.     outscan.open("ISA2LOGPARSED.TXT"); // Open output file
  70.     outscan.clear();
  71.  
  72.     size_t found;                // Special varible to hold the location of our bad word when we search
  73.  
  74.     bool busted=false;           // Bool to decide if there was a bad word
  75.  
  76.     int badwordcount=0; // Count how many bad words
  77.     int count,count2,length1;
  78.     int studentcounter = 0;   // generic counters
  79.  
  80.     while(badwords.good())
  81.     {
  82.         getline(badwords,badwordarray[badwordcount][0]);                      // Read in our Bad Words to an 2 dimensional array, first dimension is the word second is the letters in the word
  83.         sizeofbadword[badwordcount]=badwordarray[badwordcount][0].size();     // Get the amount of characters in one bad word
  84.         badwordcount++;                                                       // Add one so we can get the size of each bad word
  85.     }
  86.  
  87.  
  88.     while(myfile.good())             // Read in from our ISA log
  89.     {                
  90.         busted=false;
  91.         getline(myfile,line);        // Read in one line from our log
  92.  
  93.         istringstream demline;       // Getline can only read streams
  94.         demline.str(line);           // so we will make a string a stream
  95.        
  96.         linewrite="";                // Clear out this variable, just to make sure
  97.  
  98.         for(int i=0;i<30;i++)
  99.         {  
  100.             getline(demline,specificline,DELM);  // Using our stream we just made parse it based on the delimiter.
  101.            
  102.             if(i==0||i==9||i==18||i==28||i==29) // Get the information we want, based on CSV positions. Print it to a file
  103.             {  
  104.                 if(i!=29)                       // Make sure that the Date shows up first and not last
  105.                 {
  106.                 linewrite += specificline;
  107.                 }
  108.                 if(i==18)                      // Test if there was a bad word in the URL
  109.                 {
  110.                     count=0;
  111.                    
  112.                     while(count<badwordcount)
  113.                     {
  114.                         foundterm="";
  115.                         count2=0;
  116.  
  117.                         while(count2<sizeofbadword[count])
  118.                         {
  119.                             foundterm+=badwordarray[count][count2];       // Building up our search term since a string array is a character array
  120.                             count2++;
  121.                         }
  122.  
  123.                         found = string::npos;                            // Make sure that found is set to not found
  124.                         found = specificline.find(foundterm);            // Our actual search statement
  125.  
  126.                         if (found!=string::npos)                         // If it is found print out what word, and set busted to true
  127.                         {
  128.                             outscan<<foundterm<<": ";
  129.                             busted=true;
  130.                         }
  131.  
  132.                         count++;
  133.                     }
  134.                 }
  135.                 if(i==0)
  136.                 {
  137.                     ipaddr=specificline;
  138.                 }
  139.             }
  140.         }
  141.         if(busted)                                        // Check and see if they was busted or not, then write the info to a file, and set busted to false
  142.         {
  143.             outscan<<specificline<<" "<<linewrite<<endl;
  144.             studentcounter = reportStudents(ipaddr);              //Run the reportStudent function to initilize our results array
  145.             for(int counter=1;counter<=studentcounter;counter++)
  146.             {
  147.                 count=results[counter]-1;                         // Match the results array to our event array
  148.                 if(count!=0)
  149.                 {
  150.                     for(int counter3=0;counter3<8;counter3++)
  151.                     {
  152.                         outscan<<"   ";
  153.                         length1=eventArry[count][counter3][0].size();
  154.                         for(int iai=0;iai<length1;iai++)
  155.                         {
  156.                         outscan<<eventArry[count][counter3][iai];      // Build up out output statement to report the offenders
  157.                         }
  158.                     }
  159.                 }   outscan<<endl;
  160.                 outscan<<endl;
  161.             }
  162.            
  163.             busted=false;
  164.         }
  165.     }
  166.     outscan.close();   // Close our files.
  167.     myfile.close();
  168.     badwords.close();
  169. }
  170. void hipathParse()
  171. {
  172.     string temp,line,specificline,linewrite,linewrite2,foundterm[5][35];
  173.     string holder,date,macaddr,ipaddr,username;  // String to hold stuff from our log
  174.  
  175.     ifstream hipathlog;                       // Open our Input files
  176.     hipathlog.open("hipathlog.txt");
  177.      
  178.     ofstream hipathoutput;                     // Open our output files
  179.     hipathoutput.open("hipathparsed.txt");
  180.  
  181.     int count,count2;    // Generic Counters
  182.     int eventid = 0;
  183.     int wordcount;
  184.  
  185.     bool busted;
  186.  
  187.     size_t found,grab,finish;                // Special varible to hold the location of our search term
  188.  
  189.     string temp1,temp2;
  190.     istringstream findin,findin2,findin3,findin4,findin5,findin6;   // String Streams to search for the Hipath events
  191.  
  192.     temp1="cumberland_guest";     // Make sure it is from guest wireless
  193.     findin.str(temp1);
  194.     getline(findin,temp2);
  195.     foundterm[0][0]=temp2;
  196.  
  197.     temp1="Cumberland Guest";   // Same as before just for other types of statements
  198.     findin2.str(temp1);
  199.     getline(findin2,temp2);
  200.     foundterm[1][0]=temp2;
  201.  
  202.     temp1="Client session state changed ( IP[";   // Look for a registration event
  203.     findin3.str(temp1);
  204.     getline(findin3,temp2);
  205.     foundterm[2][0]=temp2;
  206.  
  207.     temp1="successfully authenticated";  // Look for a login event
  208.     findin4.str(temp1);
  209.     getline(findin4,temp2);
  210.     foundterm[3][0]=temp2;
  211.  
  212.     temp1="de-registration succeeded";  // Look for a deregister event
  213.     findin5.str(temp1);
  214.     getline(findin5,temp2);
  215.     foundterm[4][0]=temp2;
  216.     wordcount=0;
  217.  
  218.     while(hipathlog.good()) // While we can read from the file
  219.     {
  220.        
  221.         wordcount++;
  222.         getline(hipathlog,line);
  223.         temp="";
  224.         for(int i=0;i<35;i++)
  225.         {
  226.             temp+=foundterm[0][i];
  227.         }
  228.         found = string::npos;                            
  229.         found = line.find(temp);
  230.  
  231.         busted = false;
  232.         if (found!=string::npos)                        
  233.         {
  234.             busted=true;
  235.             goto bust;
  236.         }
  237.  
  238.         temp="";
  239.         for(int i=0;i<35;i++)
  240.         {
  241.             temp+=foundterm[1][i];
  242.         }
  243.  
  244.         found = string::npos;
  245.         found = line.find(temp);
  246.         if (found!=string::npos)                        
  247.         {
  248.             busted = true; 
  249.             goto bust;
  250.         }
  251.  
  252.         bust:
  253.         if(busted)
  254.         {
  255.             istringstream demline;       // Getline can only read streams
  256.             demline.str(line);           // so we will make a string a stream
  257.        
  258.             for(int a=0;a<5;a++)
  259.             {  
  260.                 getline(demline,specificline,DELM);  // Using our stream we just made parse it based on the delimiter.
  261.                
  262.                
  263.  
  264.                 if(a==0||a==4) // Get the information we want, based on CSV positions. Print it to a file
  265.                 {
  266.                     if(a==0)
  267.                     {
  268.                         date = specificline;
  269.                     }
  270.  
  271.                     if(a==4)
  272.                     {
  273.                         count = 2;
  274.                             while(count<5)
  275.                             {
  276.                            
  277.                                 temp="";
  278.                                 for(int i=0;i<35;i++)
  279.                                 {
  280.                                     temp+=foundterm[count][i];
  281.                                 }
  282.  
  283.                                 found = string::npos;                            // Make sure that found is set to not found
  284.                                 found = specificline.find(temp);  // Our actual search statement
  285.                            
  286.                                 if (found!=string::npos)                        
  287.                                 {
  288.                                     switch(count)    // This switch statement decides what type of event was found in the HiPath Log
  289.                                     {                // It then places it in the EventArry with the useful information
  290.                                     case 2:
  291.                                         {
  292.                                             istringstream grabber;        // Create a string stream to hold the string to manipulate it into sub strings
  293.                                             grabber.str(specificline);
  294.  
  295.                                             found = string::npos;
  296.                                             found = specificline.find(']');   // Find the end of the IP Address
  297.  
  298.                                             grab = 59;           // 59 characters in is where the IP Address starts
  299.                                             grabber.seekg(grab);
  300.  
  301.                                             size_t poscount;
  302.                                             poscount=59;
  303.  
  304.                                             linewrite="";    // Clear out our temp varibles
  305.                                             macaddr="";
  306.                                             ipaddr="";
  307.  
  308.                                             while(poscount<=found)      // While the starting locatiopn is less than the location of the last ]
  309.                                             {
  310.                                                 ipaddr+=grabber.get();           // Build up the varible one character at a time
  311.                                                 poscount++;
  312.                                             }
  313.  
  314.                                             linewrite+=ipaddr;        // Build up the variables to output to a file
  315.                                             linewrite+=" to";
  316.                                             grabber.seekg(found+1);
  317.  
  318.                                             count2=0;  
  319.  
  320.                                             while(count2<23)
  321.                                             {
  322.                                                 macaddr+=grabber.get();   // Store the MAC address, same way as the IP address
  323.                                                 count2++;
  324.                                             }
  325.  
  326.                                            
  327.                                             linewrite+=macaddr;
  328.  
  329.                                             hipathoutput<<date<<" Assigned "<<linewrite<<endl;
  330.  
  331.                                             temp="";                           // This is our File output statement
  332.                                             std::stringstream out;
  333.                                             out<<eventid;
  334.                                             temp=out.str();
  335.  
  336.                                             eventArry[eventid][0][0] = temp;   // This is where we create what goes in the eventArry
  337.                                             eventArry[eventid][1][0] = "1";
  338.                                             eventArry[eventid][2][0] = ipaddr;
  339.                                             eventArry[eventid][3][0] = macaddr;
  340.                                             eventArry[eventid][4][0] = date;
  341.  
  342.                                             eventid++;
  343.  
  344.                                             break;
  345.                                         }
  346.  
  347.                                         case 3:
  348.                                         {
  349.                                             linewrite="";                // Clear out these variables, just to make sure
  350.                                             macaddr="";
  351.                                             username="";
  352.  
  353.                                             istringstream grabber;      // Build up our search line
  354.                                             grabber.str(specificline);
  355.  
  356.                                             found = string::npos;               // Find the end of the MAC Address
  357.                                             found = specificline.find(']');
  358.  
  359.                                             grab = 41;                  // Macs start 41 characters into the line
  360.                                             grabber.seekg(grab);
  361.  
  362.                                             size_t poscount;    
  363.                                             poscount=41;
  364.  
  365.                                             while(poscount<=found)
  366.                                             {
  367.                                                 linewrite+=grabber.get();   // While poscount is less than were we found the end of the MAC, build up the MAC Address
  368.                                                 poscount++ ;
  369.                                             }
  370.  
  371.                                             macaddr = linewrite;  // Store our temp varible, and build up or string that goes to the file
  372.  
  373.                                             linewrite+=" to ";
  374.                                             grabber.seekg(found);
  375.  
  376.                                             found = string::npos;
  377.                                             finish = string::npos;
  378.  
  379.                                             found = specificline.find("username");    // Search for the user that logged in
  380.                                             finish = specificline.find("has");
  381.                                             finish = finish-1;
  382.  
  383.                                             grabber.seekg(found);
  384.                                             poscount=found;
  385.  
  386.                                             while(poscount<=finish)          // Build up our username
  387.                                             {
  388.                                                 username+=grabber.get();
  389.                                                 poscount++;
  390.                                             }
  391.  
  392.                                             linewrite+=username;
  393.                                             hipathoutput<<date<<" Authenticated "<<linewrite<<endl;   // More file output
  394.                                            
  395.                                             temp="";                      // Write out to our file
  396.                                             std::stringstream out;  
  397.                                             out<<eventid;
  398.                                             temp=out.str();
  399.  
  400.                                             eventArry[eventid][0][0] = temp;  // Save our info to the EventArry
  401.                                             eventArry[eventid][1][0] = "2";
  402.                                             eventArry[eventid][3][0] = macaddr;
  403.                                             eventArry[eventid][6][0] = date;
  404.                                             eventArry[eventid][7][0] = username;
  405.  
  406.                                             eventid++;                                         
  407.                                             break;
  408.                                         }
  409.  
  410.                                         case 4:
  411.                                         {
  412.                                             linewrite="";                // Clear out these variables, just to make sure
  413.                                             macaddr="";
  414.                                             macaddr+="MAC";
  415.  
  416.                                             istringstream grabber;       // Build up our search line
  417.                                             grabber.str(specificline);
  418.  
  419.                                             found = string::npos;
  420.                                             finish = string::npos;
  421.                                             finish = specificline.find(']');       // Find the location of the mac address
  422.                                             found = specificline.find('[');
  423.                                             grabber.seekg(found);
  424.                                             while(found<=finish)      // Build up our MAC Address variable
  425.                                             {
  426.                                                 macaddr+=grabber.get();
  427.                                                 found++;
  428.                                             }
  429.                                             linewrite += macaddr;                           // Build up our file output line
  430.                                             linewrite+=" deregistered successfully.";
  431.  
  432.                                             hipathoutput<<date<<" MAC "<<linewrite<<endl;
  433.  
  434.                                             temp="";                       // Output to a file
  435.                                             std::stringstream out;
  436.                                             out<<eventid;
  437.                                             temp=out.str();
  438.  
  439.                                             eventArry[eventid][0][0] = temp;  // Save everything in our EventArry
  440.                                             eventArry[eventid][1][0] = "3";
  441.                                             eventArry[eventid][3][0] = macaddr;
  442.                                             eventArry[eventid][5][0] = date;
  443.  
  444.                                             eventid++;
  445.  
  446.                                             break;
  447.                                         }
  448.                                     }
  449.                                 }
  450.                             count++;
  451.                         }
  452.                     }
  453.                 }
  454.             }
  455.         }
  456.     }
  457.     hipathoutput.close();  // Close our files
  458.     hipathlog.close();
  459. }
  460. int reportStudents(string ipaddress)
  461. {
  462.     int a=0,b=0,holder=0;                                    // Create and initilize some varibles for processing Hipath and ISA
  463.     string line,line2,compare1,compare2,macaddr,eventId;
  464.     size_t found,length1,length2;
  465.     macaddr="";
  466.    
  467.     for(int i=0;i<3500;i++)
  468.     {
  469.         results[a]=0;  // Initilize the result as zero, this is checked in ISA and counts as no results
  470.  
  471.         found = string::npos;      // Search for a Registration event
  472.         line = eventArry[i][1][0];
  473.         found = line.find("1");
  474.  
  475.         if(found!=string::npos)
  476.         {
  477.             found = string::npos;
  478.             line = eventArry[i][2][0];
  479.             found = line.find(ipaddress);  // Search for the IP Address passed by the ISA Function
  480.  
  481.             if(found!=string::npos)
  482.             {
  483.                 length1=line.size();
  484.                 length2=int(ipaddress.size())+4;  // Check to see if the lengths match, because to the find function 192.168.1.1 will hit in 192.168.1.123
  485.  
  486.                 if(length1==length2)
  487.                 {
  488.                     macaddr="";
  489.  
  490.                     for(int iai=0;iai<eventArry[i][3][0].size();iai++)    // Build up the MAC Address character by character
  491.                     {
  492.                         macaddr+=eventArry[i][3][iai];
  493.                     }
  494.  
  495.                     macaddr=macaddr.substr(5,17);    // Take only the MAC Address part no [,],etc.
  496.                     results[a] = i+1;
  497.                     a++;
  498.                     b++;
  499.                 }
  500.             }
  501.         }
  502.  
  503.        
  504.        
  505.  
  506.         //Login Section
  507.         line="";
  508.         found = string::npos;
  509.         line = eventArry[i][1][0];
  510.         found = line.find("2"); // Two is a login event
  511.         if(found!=string::npos)
  512.         {
  513.             line="";
  514.             found = string::npos;
  515.             line = eventArry[i][3][0];
  516.             found = line.find(macaddr); // Find if the MAC Address is the one we found registered eariler above.
  517.             length1=macaddr.size(); // the Find function will always hit if you use a null string for search so make sure its not empty
  518.             if(length1!=0)
  519.             {
  520.                 if(found!=string::npos)
  521.                 {
  522.                     results[a] = i+1;  // Write to the results array
  523.                     a++;
  524.                     b++;
  525.            
  526.                 }
  527.             }
  528.         }
  529.     }
  530.     return b;
  531. }
Advertisement
Add Comment
Please, Sign In to add comment