JavaScript Unpack library for iOS

156 views Asked by At

Is there any Objective-C/Swift library to unpack JavaScript "packet" code like this?

eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(0(){4 1="5 6 7 8";0 2(3){9(3)}2(1)})();',10,10,'function|b|something|a|var|some|sample|packed|code|alert'.split('|'),0,{}))

I found online tools like http://matthewfl.com/unPacker.html but nothing for iOS.

Thanks!

Test 1

let function = "eval(function(p,a,c,k,e,r){e=String;if(!''.replace(/^/,String)){while(c--)r[c]=k[c]||c;k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('(0(){4 1=\"5 6 7 8\";0 2(3){9(3)}2(1)})();',10,10,'function|b|something|a|var|some|sample|packed|code|alert'.split('|'),0,{}))"

let toTest = NSString(format: "document.write('<textarea id=\"output\" cols=\"120\" rows=\"10\">');document.write(%@);document.write('</textarea>');document.getElementById(\"output\").value;", NSString(string: function).substringWithRange(NSRange(location: 5, length: count(function) - 6)))

This code returns: (0(){4 1="5 6 7 8";0 2(3){9(3)}2(1)})(); but this is wrong...

1

There are 1 answers

1
Nils Ziehn On

You could just use

- (NSString *)stringByEvaluatingJavaScriptFromString:(NSString *)script

On a UIWebView like this:

NSString *packedFunction = @"......";

[webview stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"document.write('<textarea id="output" cols="120" rows="10">');document.write(%@);document.write('</textarea>');document.getElementById("output").value;",[packedFunction subStringWithRange:NSMakeRange(5,packedFunction-1)]]];