Windows API : 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.

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;
}

Additional Resources

Subscribe to our Newsletter and don't miss important updates