Skip to content

Commit

Permalink
Merge pull request #109 from Neo23x0/refactor-logging
Browse files Browse the repository at this point in the history
Refactor logging
  • Loading branch information
Neo23x0 authored Nov 14, 2020
2 parents bf13e1f + 1180e22 commit 20ccfc6
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 8 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,12 @@ An entry is generated by every blocking event in the `Application` eventlog.

![Eventlog](https://raw.githubusercontent.com/Neo23x0/Raccine/main/images/eventlog2.png)

The IDs that Raccine generates

- EventId 1 - Setup activity
- EventId 2 - Malicious activity detected
- EventId 3 - Benign activity detected

## Simulation Mode

Since version 0.10.0, Raccine can be installed in "simulation mode", which activates all triggers, logs all actions but doesn't kill anything. This mode should be used in environments in which backup solutions or other legitimate software for a reasonable amount of time to check if Raccine would interfere with other software. The idea is to install Raccine in simulation mode, let it log for a week or month and then check the logs to see if it would have blocked legitimate software used in the organisation.
Expand Down
6 changes: 3 additions & 3 deletions install-raccine.bat
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ COPY RaccineRulesSync.exe "%ProgramFiles%\Raccine\"
COPY Raccine%ARCH%.exe "%ProgramFiles%\Raccine\Raccine.exe"
COPY yara\yara%ARCHITECTURE_SUFFIX%.exe "%ProgramFiles%\Raccine\"
COPY yara\yarac%ARCHITECTURE_SUFFIX%.exe "%ProgramFiles%\Raccine\"

:: YARA Rules
MKDIR "%ProgramFiles%\Raccine\yara"
MKDIR "%ProgramFiles%\Raccine\yara\in-memory"
Expand All @@ -148,8 +147,9 @@ ECHO Creating empty log file ...
echo. 2>"%ProgramData%\Raccine\Raccine_log.txt"
icacls "%ProgramData%\Raccine\Raccine_log.txt" /grant Users:F
ECHO Registering Eventlog Events
eventcreate.exe /L Application /T Information /id 1 /so Raccine /d "Raccine Setup: Registration of Event ID 1" 2> nul
eventcreate.exe /L Application /T Information /id 2 /so Raccine /d "Raccine Setup: Registration of Event ID 2" 2> nul
eventcreate.exe /L Application /T Information /id 1 /so Raccine /d "Raccine Setup: Registration of Event ID 1 - Used for Informational Messages" 2> nul
eventcreate.exe /L Application /T Information /id 2 /so Raccine /d "Raccine Setup: Registration of Event ID 2 - Used for Malicious Actitivty" 2> nul
eventcreate.exe /L Application /T Information /id 3 /so Raccine /d "Raccine Setup: Registration of Event ID 3 - Used for Benign Activity" 2> nul
:: Registry Settings
REG.EXE ADD HKLM\Software\Raccine /v ShowGui /t REG_DWORD /d 1 /F
REG.EXE ADD HKLM\Software\Raccine /v ScanMemory /t REG_DWORD /d 1 /F
Expand Down
2 changes: 1 addition & 1 deletion robot-tests/robot-tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Foreach ($Cmd in $GoodCmds) {

# Eventlog
$Result = Get-EventLog -LogName Application -Message *Raccine* -Newest 1
If ( $Result.Message -Match $Cmd ) {
If ( $Result.Message -Match $Cmd -and $Result.Message -Match 'malicious') {
Write-Host $Result.Message
Write-Host "Error: Eventlog entry of detection found"
exit 1
Expand Down
1 change: 1 addition & 0 deletions source/Raccine/raccine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ int wmain(int argc, WCHAR* argv[])
std::wstring message;
// Eventlog
message = L"Raccine detected benign activity:\r\n" + sCommandLine + L"\r\n(simulation mode)";
WriteEventLogEntryWithId(message, RACCINE_EVENTID_BENIGN_ACTIVITY);
// Log to the text log file
sListLogs.append(logFormat(sCommandLine, L"Raccine detected benign activity (simulation mode)"));
}
Expand Down
1 change: 1 addition & 0 deletions source/RaccineLib/Raccine.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
// Log Config and Flags
#define RACCINE_DEFAULT_EVENTID 1
#define RACCINE_EVENTID_MALICIOUS_ACTIVITY 2
#define RACCINE_EVENTID_BENIGN_ACTIVITY 3

#define RACCINE_DATA_DIRECTORY L"%PROGRAMDATA%\\Raccine"
#define RACCINE_YARA_DIRECTORY L"%PROGRAMFILES%\\Raccine\\yara"
Expand Down
7 changes: 7 additions & 0 deletions source/RaccineLib/Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,4 +534,11 @@ std::wstring getFileName(const std::wstring& s)
return(s);
}

int removeNewLines(std::wstring& str)
{
std::replace(str.begin(), str.end(), L'\r', L' ');
std::replace(str.begin(), str.end(), L'\n', L' ');
return 0;
}

}
2 changes: 2 additions & 0 deletions source/RaccineLib/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,6 @@ DWORD getCurrentSessionId();

std::wstring getUserSid();

int removeNewLines(std::wstring& str);

}
17 changes: 13 additions & 4 deletions source/RaccineLib/raccine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,16 @@ void WriteEventLogEntryWithId(const std::wstring& pszMessage, DWORD dwEventId)

LPCWSTR lpszStrings[2] = { pszMessage.c_str() , nullptr };

// Select an eventlog message type
WORD eventType = EVENTLOG_INFORMATION_TYPE;
if (dwEventId == RACCINE_EVENTID_MALICIOUS_ACTIVITY) {
eventType = EVENTLOG_WARNING_TYPE;
}

constexpr PSID NO_USER_SID = nullptr;
constexpr LPVOID NO_BINARY_DATA = nullptr;
ReportEventW(hEventSource, // Event log handle
EVENTLOG_INFORMATION_TYPE, // Event type
ReportEventW(hEventSource, // Event log handle
eventType, // Event type
0, // Event category
dwEventId, // Event identifier
NO_USER_SID, // No security identifier
Expand Down Expand Up @@ -205,7 +211,7 @@ std::wstring logFormat(const std::wstring& cmdLine, const std::wstring& comment)
{
const std::string timeString = getTimeStamp();
const std::wstring timeStringW(timeString.cbegin(), timeString.cend());
std::wstring logLine = timeStringW + L" DETECTED_CMD: '" + cmdLine + L" COMMENT: " + comment + L"\n";
std::wstring logLine = timeStringW + L" DETECTED_CMD: '" + cmdLine + L"' COMMENT: " + comment + L"\n";
return logLine;
}

Expand Down Expand Up @@ -242,9 +248,12 @@ void logSend(const std::wstring& logStr)
return; // bail out if we can't log
}
}
// Replace new line characters
std::wstring logString = logStr;
utils::removeNewLines(logString);

if (logFile != nullptr) {
fwprintf(logFile, L"%s", logStr.c_str());
fwprintf(logFile, L"%s\n", logString.c_str());
fflush(logFile);
fclose(logFile);
logFile = nullptr;
Expand Down

0 comments on commit 20ccfc6

Please sign in to comment.