OutputDebugString

This Windows API is often used by developers for debugging purpose. It will display a text to the attached debugger. This API is also used by Malware to open a communication channel between one or multiple processes.

It is possible to use OutputDebugString in addition of GetLastError / SetLastError to detect debugger presence.

U0117

Code Snippets

program OutputDebugString;

{$APPTYPE CONSOLE}

uses
  WinAPI.Windows,
  System.SysUtils;

var AErrorValue : Byte;

begin
  try
    randomize;

    AErrorValue := Random(High(Byte));

    SetLastError(AErrorValue);

    OutputDebugStringW('TEST');

    if (GetLastError() = AErrorValue) then
      WriteLn('Debugger detected using OutputDebugString() technique.')
    else
      WriteLn('No debugger detected using OutputDebugString() technique.');
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.
#include <stdio.h>
#include <Windows.h>

int main()
{
	SetLastError(0);
	
        // Send string to the debugger
	OutputDebugStringA("Hello friend");

	if (GetLastError() != 0)
	{
		printf("Debugger detected!!\n");
	}
        system("pause");
	return 0;
}

Detection Rules

rule:
  meta:
    name: check for OutputDebugString error
    namespace: anti-analysis/anti-debugging/debugger-detection
    author: michael.hunhoff@fireeye.com
    scope: function
    mbc:
      - Anti-Behavioral Analysis::Debugger Detection::OutputDebugString [B0001.016]
    examples:
      - Practical Malware Analysis Lab 16-02.exe_:0x401020
  features:
    - and:
      - api: kernel32.SetLastError
      - api: kernel32.GetLastError
      - api: kernel32.OutputDebugString

Additional Resources

Subscribe to our Newsletter and don't miss important updates