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
    Pure-CSS 3D world collision detection How to write a Hackvertor tagIntroducing 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 PHP

    Code mutation experiments

    By Gareth Heyes (@hackvertor)

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

    ← Back to articles

    As a little hobby I've been really into code mutation and getting computers to write their own code, well at least that's the goal anyway. What really interests me is if you can give a computer a really small amount of code but yet get it to construct something itself. I think this is pretty much how AI will work. The problem I see is the halting problem, in order for a computer to understand code it needs to know what it's output should be but when the output has many paths and multiple outputs depending on inputs then it is suddenly very complex and even impossible to know the correct output. Let's ignore this problem for now though, if we assume that a program knows what it's output should be and it's required operation what can we do?

    Minifying

    Minifying can be done by simply checking the syntax and making sure the output is the same when removing a character. Then continue until you cannot remove any more characters. The demo is in sorta slow motion since I use a timeout of 500 milliseconds but it helps to see how it works.

    Updated... I've updated the code to be aware of global variable leakage works currently only work on IE and Firefox. It will remove multi-line comments now (sometimes completely) and leave var statements because removing them will produce globals.

    Mutate minify demo

    The code only takes randomized character positions and removes them, you could of course just traverse from top to bottom but this is only a experiment and the code is of course a little rough.

    Obfuscation

    If we changed the parameters a little and instead of removing characters we replace them then we can obfuscate the code by constantly changing it's source but retain the same output. This works by choosing two random points in the code and random characters. What was interesting here was the code "discovered" that it could create single line comments, strings and replace variables. Sometimes copying the code and executing somewhere else wouldn't return the same output either because as it mutated it would rely on existing global variables.

    Mutate obfuscate demo

    Mutation

    Finally we have the mutation demo which takes some input and modifies it and appends it with valid syntax. It checks to see if the result was the same as last time and the result is positive. At the moment this is a bit dumb since the code generated is random and of no value, I'm looking into genetic code to see if I can make it return more meaningful and interesting output and if it can have a basic understanding on how the source works.

    Mutate demo

    Update...

    Mutating a function value

    Can we use randomized values to change a function and return a different value? Yeah :) This one took a bit of thinking about. I have a few characters that are valid JavaScript. I build a loop through some maximum mutations, pick a random position. Check the syntax and bail if invalid if not I then test the output for the expected result. The idea is the function returns something like "2" but you want it to return "4" and the mutations will continue until it finds the expected value.

    Mutate function value demo

    ← Back to articles