Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add more detail to suspicious scriptblock logging #21475

Open
wants to merge 1 commit into
base: release/v7.2.19
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -1426,29 +1426,34 @@ internal static void LogScriptBlockCreation(ScriptBlock scriptBlock, bool force)
}

string scriptBlockText = scriptBlock.Ast.Extent.Text;
string word = SuspiciousContentChecker.Match(scriptBlockText);

string message = @"Detected term: '" + word + @"'; " + scriptBlockText;

bool written = false;

// Maximum size of ETW events is 64kb. Split a message if it is larger than 20k (Unicode) characters.
if (scriptBlockText.Length < 20000)
if (message.Length < 20000)
{
written = WriteScriptBlockToLog(scriptBlock, 0, 1, scriptBlock.Ast.Extent.Text);
written = WriteScriptBlockToLog(scriptBlock, 0, 1, message);
}
else
{
// But split the segments into random sizes (10k + between 0 and 10kb extra)
// so that attackers can't creatively force their scripts to span well-known
// segments (making simple rules less reliable).
int segmentSize = 10000 + (new Random()).Next(10000);
int segments = (int)Math.Floor((double)(scriptBlockText.Length / segmentSize)) + 1;
int segments = (int)Math.Floor((double)(message.Length / segmentSize)) + 1;
int currentLocation = 0;
int currentSegmentSize = 0;

for (int segment = 0; segment < segments; segment++)
{
currentLocation = segment * segmentSize;
// are we at the end of the string, if so only get the rest of the string
currentSegmentSize = Math.Min(segmentSize, scriptBlockText.Length - currentLocation);

string textToLog = scriptBlockText.Substring(currentLocation, currentSegmentSize);
string textToLog = message.Substring(currentLocation, currentSegmentSize);
written = WriteScriptBlockToLog(scriptBlock, segment, segments, textToLog);
}
}
Expand Down