Click here to Skip to main content
15,885,875 members
Articles / Programming Languages / C#
Tip/Trick

Log USB Events to Splunk Or Any syslog Server

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
13 Jul 2016CPOL 13.9K   1   8  
Creating a Windows service to monitor usb events with splunk

Introduction

This is a project essay of a usb event logger to splunk instance, I have been introduced lately to SIEM and am enjoying working on some projects and this is one of them.

In order to run the solution, unzip both packages1 and packages2 inside LoggerForDirectories\packages.

Background

Use it if you want to monitor the activities on an organisation for usb copy events, it's a Windows service project written in C# with a setup project that logs the copied file along with the IP address, session domain, computer name, current CPU usage, available RAM.

Using the Code

This is the listener using System.IO.

C#
FileSystemWatcher watcher;
Stopwatch s = new Stopwatch();
var formatter = new MessageTemplateTextFormatter(
   "{Timestamp:HH:mm} [{Level}] ({ThreadId}) {Message}{NewLine}{Exception}",
   formatProvider: null);

a.Clear();
s.Start();
while (s.Elapsed < TimeSpan.FromSeconds(40))
{
    var drives = DriveInfo.GetDrives()
       .Where(drive => drive.IsReady && drive.DriveType == DriveType.Removable);
    if (drives != null)
    {
        foreach (var item in drives)
        {
            watcher = new FileSystemWatcher();
            watcher.Path = item.RootDirectory.ToString();
            watcher.NotifyFilter = NotifyFilters.LastAccess
                                    | NotifyFilters.LastWrite
                                    | NotifyFilters.FileName
                                    | NotifyFilters.DirectoryName;
            watcher.Filter = "*.*";
            watcher.IncludeSubdirectories = true;
            watcher.Created += new FileSystemEventHandler(OnCreate);
            watcher.EnableRaisingEvents = true;
        }
    }

OnCreate is called every time a copy event is detected.

And because the copy events is composed of many events I had to filter events based on files.

C#
public void OnCreate(object source, FileSystemEventArgs e)
    {
        if((a.Capacity-10) != a.Count)
        {
            if (a.Any())
            {
                if (e.Name != null)
                {
                        if (a.Contains(e.Name.ToString()))
                        {  /*Do nothing*/ }
                        else
                        { a.Add(e.Name.ToString()); }
                }
            }
            else
            {
                if (e.Name != null)
                {
                    a.Add(e.Name.ToString());
                }
            }
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Junior) Student
Tunisia Tunisia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --