54

Samy Kamkar's website, http://samy.pl, knows when the console is being opened and wipes the source/console when it does open.

enter image description here

How does this work?

6
  • 1
    Doesn't work for me in Opera. Must be browser-specific thing. You're on Chrome?
    – Bergi
    Commented Feb 12, 2017 at 22:18
  • Yes, Chrome 56 on Windows 10 Commented Feb 12, 2017 at 22:21
  • Works in my Opera (44), Firefox and even IE
    – Jake
    Commented Feb 12, 2017 at 22:22
  • IE? Holy smokes. EDIT: Works on Edge too Commented Feb 12, 2017 at 22:23
  • 1
    The code is right there. Uh, if you load that url with the right referrer and decode the whitespace.
    – Bergi
    Commented Feb 12, 2017 at 22:31

1 Answer 1

72

This took some digging. samy.pl has several levels of indirection and obfuscation on top of this code. It uses a different version of the detection code than the GitHub repository found by JohanP. The code in samy.pl, unlike the GitHub repository, can detect the devtools when they are not docked.

It does so by using a short script that executes differently depending on whether devtools is open or closed.

Example script

Here's a self-contained example; open it in a browser and notice how the output changes as the devtools are opened and closed (whether it is docked or not):

<!DOCTYPE html>
<html>
    <body>
        <pre id="output"></pre>
        <script type="text/javascript">
            var element = new Image;
            var devtoolsOpen = false;
            element.__defineGetter__("id", function() {
                devtoolsOpen = true; // This only executes when devtools is open.
            });
            setInterval(function() {
                devtoolsOpen = false;
                console.log(element);
                document.getElementById('output').innerHTML += (devtoolsOpen ? "dev tools is open\n" : "dev tools is closed\n");
            }, 1000);
        </script>
    </body>
</html>

How it works

The setInterval is executed every second. console.log always executes, whether the devtools are open or closed: The console object is always defined. However, the log method only writes output to the console when the devtools are open. If the devtools are closed, console.log is a no-op. That's the key that lets you detect if the devtools are open: detecting if the log operation is a no-op.

In the process of writing element to the console, it gets the id of the element. That calls the function attached with __defineGetter__. Therefore, console.log(element) only calls that function when the devtools are open and console.log is not a no-op. The flag is set in that function, giving us an updated view of the devtools state every second.

samy.pl uses some additional tricks to hide this: the console is also cleared every second, and this code is obfuscated with a whitespace (!) encoding.

9
  • 11
    Should we write a Chrome/Chromium bug report to prevent this behavior? I kind of don't like if people know I have that open. Commented Feb 12, 2017 at 22:49
  • 1
    Great answer. If you have time could you expand on the last sentence? and this code is obfuscated with a whitespace (!) encoding Thanks!
    – Jake
    Commented Feb 12, 2017 at 23:14
  • 11
    @Jake The obfuscation could be an answer in itself! The source code that includes this detection is encoded by converting each character to the binary representation of its ASCII character code (e.g. 'A' == '1000001'); then converting each 0 to a space and 1 to a tab. That string of whitespace is converted back to binary, then HTML, and executed with this JS (where s is the string of whitespace): s.replace(/.{7}/g,function(w){document.write(String.fromCharCode(parseInt(w.replace(/ /g,'0').replace(/ /g,'1'),2)))}). Commented Feb 12, 2017 at 23:25
  • 4
    @Jake I didn't look at load.js but figured this out from what's served at samy.pl. Setting a pre-execution breakpoint (in Chrome devtools, via Sources tab > Event Listener Breakpoints > Script > Script First Statement) let me step through the code as it executed, which was most valuable. That included the decoding and the obfuscation code, and gave me chances to interfere with or prevent some of it. Commented Feb 13, 2017 at 0:47
  • 5
    this is really cool, just had to figure out how to defeat it :) sorry samy. To defeat it, type console.log = function() {}, the browser will reload, then quickly press up and enter to run that again, and you can see the source. imgur.com/a/1dBPn
    – adgelbfish
    Commented Feb 14, 2017 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.