Click here to Skip to main content
15,885,244 members
Articles / Programming Languages / C#

C# Detect if Debugger is Attached

Rate me:
Please Sign up or sign in to vote.
4.27/5 (9 votes)
19 Oct 2013CPOL 50.7K   22   7
C# detect if debugger is attached

This method is used to detect if a running process has a debugger attached to it. It involves using CheckRemoteDebuggerPresent, imported from kernel32.dll via PInvoke.

* tested on Visual Studio's Debugger & OllyDbg

How To...

First, include the following two lines in your program (which will import CheckRemoteDebuggerPresent):

C#
[DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
static extern bool CheckRemoteDebuggerPresent(IntPtr hProcess, ref bool isDebuggerPresent);

Now, this method is pretty simple to use since it takes only 2 arguments:

  1. IntPtr hProcess = the target process' handle
  2. ref bool isDebuggerPresent = pointer that indicates the result

This method does all the 'hard work', so no further code is required: 

C#
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

public class DetectDebugger
{
    [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
    static extern bool CheckRemoteDebuggerPresent(IntPtr hProcess, ref bool isDebuggerPresent);

    public static void Main()
    {
        bool isDebuggerPresent = false;
        CheckRemoteDebuggerPresent(Process.GetCurrentProcess().Handle, ref isDebuggerPresent);

        Console.WriteLine("Debugger Attached: " + isDebuggerPresent);
        Console.ReadLine();
    }
}

Update

In order to avoid any confusion about Debugger.IsAttached and IsDebuggerPresent - sorry I didn't mention this earlier in the tip:

  • IsDebuggerPresent = works for any running process and detects native debuggers too
  • Debugger.IsAttached = works only for the current process and detects only managed debuggers. OllyDbg won't be detected by this.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Student
Romania Romania
Master's student @ ACS / UPB (Advanced Cybersecurity), Graduate Teaching Assistant, Junior Security Researcher. Also, webmaster of coding.vision

Comments and Discussions

 
QuestionUseless Pin
Member 84748665-Jun-17 0:27
Member 84748665-Jun-17 0:27 
GeneralMy vote of 3 Pin
Florian Rosmann17-Oct-13 22:27
Florian Rosmann17-Oct-13 22:27 
GeneralRe: My vote of 3 Pin
Dan Sporici18-Oct-13 1:24
Dan Sporici18-Oct-13 1:24 
Updated the article: I forgot to mention about the differences.

Debugger.IsAttached won't detect native debuggers like OllyDbg. Also it only works for the current process.


Anyway...I know it's quite short, I'll consider changing it to a tip.
GeneralRe: My vote of 3 Pin
Dan Colasanti18-Oct-13 3:01
professionalDan Colasanti18-Oct-13 3:01 
GeneralRe: My vote of 3 Pin
Giuseppe Tollini19-Oct-13 1:12
Giuseppe Tollini19-Oct-13 1:12 
QuestionFor current process IsDebuggerPresent is enough Pin
AnandChavali17-Oct-13 18:53
AnandChavali17-Oct-13 18:53 
GeneralThoughts Pin
PIEBALDconsult17-Oct-13 18:28
mvePIEBALDconsult17-Oct-13 18:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.