Advertisement
Guest User

Untitled

a guest
Dec 24th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.13 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Diagnostics.Eventing.Reader;
  5. using System.Linq;
  6. using System.Net.Http;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Web.Script.Serialization;
  12.  
  13. namespace RDP_Bruteforce_Protect
  14. {
  15.     class Program
  16.     {        
  17.         [DllImport("Kernel32")]
  18.         public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);      
  19.         public delegate bool HandlerRoutine(CtrlTypes CtrlType);
  20.  
  21.         public enum CtrlTypes
  22.         {
  23.             CTRL_C_EVENT = 0,
  24.             CTRL_BREAK_EVENT,
  25.             CTRL_CLOSE_EVENT,
  26.             CTRL_LOGOFF_EVENT = 5,
  27.             CTRL_SHUTDOWN_EVENT
  28.         }        
  29.         private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
  30.         {
  31.             Console.ForegroundColor = ConsoleColor.DarkYellow;
  32.             Console.WriteLine(" Disposing Event-Log-Watcher");
  33.             Console.ForegroundColor = ConsoleColor.White;
  34.  
  35.             // Stop listening to events
  36.             watcher.Enabled = false;
  37.  
  38.             if (watcher != null)
  39.             {
  40.                 watcher.Dispose();
  41.             }
  42.  
  43.  
  44.             Thread.Sleep(1000);
  45.             Environment.Exit(0);
  46.             return true;
  47.         }
  48.  
  49.         static void Main(string[] args)
  50.         {
  51.             Console.ForegroundColor = ConsoleColor.White;
  52.  
  53.             SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
  54.             subscribeAsync();
  55.         }
  56.  
  57.         static EventLogWatcher watcher = null;
  58.         public static void subscribeAsync()
  59.         {            
  60.             try
  61.             {                
  62.                 EventLogQuery subscriptionQuery = new EventLogQuery(
  63.                     "Security", PathType.LogName, "*[EventData[(Data[@Name='LogonType']='10') or (Data[@Name='LogonType']='7')] and System[(EventID='4624')]] or *[EventData[(Data[@Name='LogonType']='3') or (Data[@Name='LogonType']='7')] and System[(EventID='4625')]]");
  64.                 watcher = new EventLogWatcher(subscriptionQuery);
  65.  
  66.                 watcher.EventRecordWritten +=
  67.                     new EventHandler<EventRecordWrittenEventArgs>(EventLogEventRead);
  68.  
  69.                 watcher.Enabled = true;
  70.  
  71.                 Console.WriteLine(" --------------------------------------");
  72.                 Console.WriteLine(" ---- LISTENING RDP LOGIN ATTEMPTS ----");
  73.                 Console.WriteLine(" --------------------------------------");
  74.                
  75.  
  76.                 while (true)
  77.                 {
  78.                     // keep running
  79.                 }
  80.             }
  81.             catch (EventLogReadingException e)
  82.             {
  83.                 Console.WriteLine("Error reading the log: {0}", e.Message);
  84.             }
  85.             finally
  86.             {
  87.                 // Stop listening to events
  88.                 watcher.Enabled = false;
  89.  
  90.                 if (watcher != null)
  91.                 {
  92.                     watcher.Dispose();
  93.                 }
  94.             }
  95.             Console.ReadKey();
  96.         }
  97.  
  98.         static Errors ErrorHandler = new Errors();
  99.         static HttpClient httpClient = new HttpClient();        
  100.         public static async void EventLogEventRead(object obj,
  101.             EventRecordWrittenEventArgs arg)
  102.         {            
  103.             if (arg.EventRecord != null)
  104.             {
  105.                 String[] xPathRefs = new String[] { };
  106.  
  107.                 if (arg.EventRecord.Id == 4624)
  108.                 {
  109.                     xPathRefs = new String[] {
  110.                         "Event/System/TimeCreated/@SystemTime",
  111.                         "Event/EventData/Data[@Name='TargetUserName']",
  112.                         "Event/EventData/Data[@Name='IpAddress']",
  113.                         "Event/EventData/Data[@Name='LogonType']"
  114.                     };
  115.                 }
  116.                 else
  117.                 {
  118.                     xPathRefs = new String[] {
  119.                         "Event/System/TimeCreated/@SystemTime",
  120.                         "Event/EventData/Data[@Name='TargetUserName']",
  121.                         "Event/EventData/Data[@Name='IpAddress']",
  122.                         "Event/EventData/Data[@Name='LogonType']",
  123.                         "Event/EventData/Data[@Name='FailureReason']",
  124.                         "Event/EventData/Data[@Name='WorkstationName']"
  125.                     };
  126.                 }
  127.  
  128.                 // Place those strings in an IEnumberable object
  129.                 IEnumerable<String> xPathEnum = xPathRefs;
  130.                 // Create the property selection context using the XPath reference
  131.                 EventLogPropertySelector logPropertyContext = new EventLogPropertySelector(xPathEnum);
  132.  
  133.                 IList<object> logEventProps = ((EventLogRecord)arg.EventRecord).GetPropertyValues(logPropertyContext);
  134.  
  135.                 string country = string.Empty;
  136.                 try
  137.                 {
  138.                     var json = await httpClient.GetStringAsync("https://api.ipgeolocationapi.com/geolocate/" + logEventProps[2]);
  139.                     var jsonDeserialized = new JavaScriptSerializer().Deserialize<dynamic>(json);
  140.                     country = jsonDeserialized["name"];
  141.                 }
  142.                 catch (Exception e)
  143.                 {
  144.                     //Console.WriteLine(e);
  145.                     country = "-";
  146.                 }
  147.                
  148.                
  149.  
  150.                 Console.WriteLine(" Time:\t\t" + logEventProps[0]);
  151.                 Console.WriteLine(" User:\t\t" + logEventProps[1]);
  152.                
  153.                 if (arg.EventRecord.Id == 4624)
  154.                 {
  155.                     Console.Write(" Status:\t");
  156.                     Console.ForegroundColor = ConsoleColor.DarkGreen;
  157.                     Console.WriteLine("LOGIN SUCCEED");
  158.                     Console.ForegroundColor = ConsoleColor.White;
  159.                 }
  160.                 else
  161.                 {
  162.                     Console.Write(" Status:\t");
  163.                     Console.ForegroundColor = ConsoleColor.DarkRed;
  164.                     Console.WriteLine("LOGIN FAILED");
  165.                     Console.ForegroundColor = ConsoleColor.White;
  166.                     Console.WriteLine(" Reason:\t" + ErrorHandler.FailureReasons[logEventProps[4].ToString()]);
  167.                     if (Environment.MachineName == logEventProps[5].ToString())
  168.                     {
  169.                         Console.WriteLine(" Remote-PC:\tNOT A REMOTE MACHINE");
  170.                     }
  171.                     else
  172.                     {
  173.                         Console.WriteLine(" Remote-PC:\t" + logEventProps[5]);
  174.                     }                    
  175.                 }              
  176.                 Console.WriteLine(" IP:\t\t" + logEventProps[2]);
  177.                 Console.WriteLine(" Country:\t" + country);
  178.                 Console.WriteLine(" Logon-Type:\t" + logEventProps[3]);
  179.                 Console.WriteLine(" --------------------------------------");
  180.             }
  181.             else
  182.             {
  183.                 Console.WriteLine("The event instance was null.");
  184.             }
  185.         }
  186.  
  187.  
  188.  
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement