The Spanner logo
    • Home
    • Blog
      • Blog home
      • RSS
    • Login
    • Home
    • Blog
      • Blog home
      • RSS
    • Login
    The Spanner logo

    The Spanner
    Web security blog

    Made by Gareth Heyes
    Follow me on Twitter: @garethheyes

    Javascript for hackers!

    Hackvertor logo
    Shazzer logo
    My Github account
    Recent posts
    Introducing Feedworm: A Privacy-First RSS Reader That Lives in DevToolsSpeedy RSVP extensionAutoVaderHackvertor history and tag finderShadow Repeater v1.2.3 releaseBurp Hackvertor v2.1.24 releaseHacking roomsXSSing TypeErrors in SafarivalueOf: Another way to get thisMaking the Unexploitable Exploitable with X-Mixed-Replace on FirefoxThe curious case of the evt parameterCSS-Only Tic Tac Toe ChallengeRewriting relative urls with the base tag in SafariBypassing DOMPurify with mXSSNew IE mutation vectorHow I smashed MentalJSMentalJS DOM bypassAnother XSS auditor bypassXSS Auditor bypassBypassing the IE XSS filterUnbreakable filterMentalJS bypassesmXSSJava SerializationBypassing the XSS filter using function reassignmentRPOSandboxed jQueryX-Domain scroll detection on IE using focusEpic fail IEnew operatorDecoding complex non-alphanumeric JavaScriptHacking FirefoxDOM ClobberingBypassing XSS AuditorThe evolution of codeNon-Alpha PHP in 6-7 charsetTweetable PHP-Non AlphaMentalJS for PHPOpera x domain with video tutorialSandboxing and parsing jQuery in 100ms

    Overwriting native functions in javascript

    By Gareth Heyes (@hackvertor)

    Published 17 years 2 months ago • Last updated March 22, 2025 • ⏱️ 2 min read

    ← Back to articles

    I research a lot of Javascript as part of my job and I've been toying with the idea of a perfect native function overwrite. The idea is that you can still call the native function and have control over it but once it's been defined it cannot be modified only destroyed.

    My idea was to redefine the alert function and to remove it once it reaches a certain limit, in order to avoid infinite prompts. I think I've been successful but I'm sure someone will point out if I haven't. It works by storing the native function in a closure and holds the reference within it's parent. The child function is then returned with two private variables the native function call and a counter. Because they are private variables they shouldn't be available in the global scope and so only the parent of the the child function should have access to them.

    This code is based on the assumption that it is run first before any other Javascript, so you can modify it after it's been executed but you should no longer have access to it's native code.

    <pre lang="javascript"> window.alert = function(native) { var counter = 0; return function(str) { native(str); if(counter > 10) { window.alert = null; } counter++; } }(window.alert); </pre>

    ← Back to articles