Javascript obfuscation help - javascript

I need some help to understand how this code was obfuscated. The code is:
<a id="suggest" href="#" ajaxify="/ajax/social_graph/invite_dialog.php?class=FanManager&node_id=108463912505356" class=" profile_action actionspro_a" rel="dialog-post">Suggest to Friends</a>
And the obfuscation is:
\x3c\x61\x20\x69\x64\x3d\x22\x73\x75\x67\x67\x65\x73\x74\x22\x20\x68\x72\x65\x66\x3d\x22\x23\x22\x20\x61\x6a\x61\x78\x69\x66\x79\x3d\x22\x2f\x61\x6a\x61\x78\x2f\x73\x6f\x63\x69\x61\x6c\x5f\x67\x72\x61\x70\x68\x2f\x69\x6e\x76\x69\x74\x65\x5f\x64\x69\x61\x6c\x6f\x67\x2e\x70\x68\x70\x3f\x63\x6c\x61\x73\x73\x3d\x46\x61\x6e\x4d\x61\x6e\x61\x67\x65\x72\x26\x61\x6d\x70\x3b\x6e\x6f\x64\x65\x5f\x69\x64\x3d\x31\x30\x38\x34\x36\x33\x39\x31\x32\x35\x30\x35\x33\x35\x36\x22\x20\x63\x6c\x61\x73\x73\x3d\x22\x20\x70\x72\x6f\x66\x69\x6c\x65\x5f\x61\x63\x74\x69\x6f\x6e\x20\x61\x63\x74\x69\x6f\x6e\x73\x70\x72\x6f\x5f\x61\x22\x20\x72\x65\x6c\x3d\x22\x64\x69\x61\x6c\x6f\x67\x2d\x70\x6f\x73\x74\x22\x3e\x53\x75\x67\x67\x65\x73\x74\x20\x74\x6f\x20\x46\x72\x69\x65\x6e\x64\x73\x3c\x2f\x61\x3e","\x73\x75\x67\x67\x65\x73\x74
Now I used unescape on the above obfuscated code to read it. What I want to know is what exactly was used to obfuscate the code like that? Basically, I need to customize the readable code to the same obfuscation.
Any help would be greatly appreciated.

If you are using unicode characters above 255, you will need some special handling. You will also need to make sure the hex codes are padded with 0s correctly, or the function will break for characters below 16 (such as \n and \t):
function obfuscate(str) {
var escaped = [];
for (var i = 0; i < str.length; i++) {
var c = str.charCodeAt(i);
var cs = "0000" + c.toString(16);
if (c < 256) {
cs = "\\x" + cs.substr(-2);
} else {
cs = "\\u" + cs.substr(-4);
}
escaped.push(cs);
}
return escaped.join('');
}
var ob = obfuscate("Hello world!");
alert(ob);

You shouldn't use obfuscation for anything serious, but it can be fun to play around with:
var readable = '<a id="suggest" href="#" ajaxify="/ajax/social_graph/invite_dialog.php?class=FanManager&node_id=108463912505356" class=" profile_action actionspro_a" rel="dialog-post">Suggest to Friends</a>';
Array.prototype.map.call(readable,
function(c){
return "\\x" + c.charCodeAt().toString(16);
}).join("");

Type the following code in the address bar:
javascript:alert("\x3c\x61\x20\x69\x64\x3d\x22\x73\x75\x67\x67\x65\x73\x74\x22\x20\x68\x72\x65\x66\x3d\x22\x23\x22\x20\x61\x6a\x61\x78\x69\x66\x79\x3d\x22\x2f\x61\x6a\x61\x78\x2f\x73\x6f\x63\x69\x61\x6c\x5f\x67\x72\x61\x70\x68\x2f\x69\x6e\x76\x69\x74\x65\x5f\x64\x69\x61\x6c\x6f\x67\x2e\x70\x68\x70\x3f\x63\x6c\x61\x73\x73\x3d\x46\x61\x6e\x4d\x61\x6e\x61\x67\x65\x72\x26\x61\x6d\x70\x3b\x6e\x6f\x64\x65\x5f\x69\x64\x3d\x31\x30\x38\x34\x36\x33\x39\x31\x32\x35\x30\x35\x33\x35\x36\x22\x20\x63\x6c\x61\x73\x73\x3d\x22\x20\x70\x72\x6f\x66\x69\x6c\x65\x5f\x61\x63\x74\x69\x6f\x6e\x20\x61\x63\x74\x69\x6f\x6e\x73\x70\x72\x6f\x5f\x61\x22\x20\x72\x65\x6c\x3d\x22\x64\x69\x61\x6c\x6f\x67\x2d\x70\x6f\x73\x74\x22\x3e\x53\x75\x67\x67\x65\x73\x74\x20\x74\x6f\x20\x46\x72\x69\x65\x6e\x64\x73\x3c\x2f\x61\x3e","\x73\x75\x67\x67\x65\x73\x74")
and it will get decoded.

Related

javascript regex for ip address with asterisk masking

I would like to make a regular expression for an IP address with asterisk(*)
which matches these below:
The digit 127.0 could be any number between 0 to 255.
**[TRUE]**
127.*.*.*
127.0.*.*
127.0.0.*
**[FALSE]**
127.*.*.1
127.*.0.1
127.0.*.1
What I have made until now is...and of course, failed to make it out.
I totally got lost..
_regex = function(value) {
var _match = /^(?:(\d|1\d\d|2[0-4]\d|25[0-5]))\.(?:(\*|\d|1\d\d|2[0-4]\d|25[0-5]))\.(\*|(?:\d{1,2}(?:.\d{1,3}?)))\.(\*|(?:\d{1,3}(?:\*?)))$
if(_match.test(value)){
//do something;
}
}
If you give me any chance to learn this, would be highly appreciated.
Thanks.
I think what you are looking for is a negative look ahead to make sure no number follows an asterisk.
Like so: (\*(?!.*\d))
working example:
var ips = [
'127.*.*.*',
'127.0.*.*',
'127.0.0.*',
'127.*.*.1',
'127.*.0.1',
'127.0.*.1'
];
var regex = /^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|(\*(?!.*\d)))(\.|$)){4}$/;
for(var i = 0; i < ips.length; i++){
console.log(ips[i] + ': ' + regex.test(ips[i]));
}

Making secret language useing charAt

I need to make a textbox with in there a word when i click on a button that word needs to convert into numbers useing charAt and that number needs to get +2 and than converted back into words and that word needs to get alerted i dont know what to do i find this really hard i made a function that is useless but i just want to show you what i did please help :)
function codeer(){
var woord2 = document.getElementById("woord")
var woordterug = woord2.charAt(0)
var woord234 = document.getElementById("woord");
var woord23 = woord234.charAt(str.length+2);
}
You could get the char code with String#charCodeAt from the character add two and build a new string with String.fromCharCode.
function codeer() {
var woord = document.getElementById("woord").value,
coded = '',
i;
for (i = 0; i < woord.length; i++) {
coded += String.fromCharCode(woord.charCodeAt(i) + 2);
}
console.log(coded);
}
<input id="woord" /> <button onclick="codeer()">cooder</button>
You should search the internet for a JavaScript rot13 example. In that code, you just need to replace the 13 with a 2, and it should work.

how to do encryption with "java script" and decryption with and "java" [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to encrypt the "url" in java script by using below method
var s='';
for(var i=0;i<origUrl.length;i++)
{
var c=origUrl.charCodeAt(i);
if(c != ' ')
{
c=String.fromCharCode(c + 47);
if( c > '~')
{
c=String.fromCharCode(c - 94);
}
}
s=s.concat(c);
}
and for decryption in java I am using below code
public static String rotate(String value)
{
int length = value.length();
String result = "";
for (int i = 0; i < length; i++)
{
char c = value.charAt(i);
// Process letters, numbers, and symbols -- ignore spaces.
if (c != ' ')
{
c += 47;
if (c > '~')
c -= 94;
}
result=result+c;
}
return result.toString();
}
Both code has same logic but it is not working for me, means when I am passing java script generated string to server side and doing decryption, I am not getting the correct string which I am expecting.
on other hand if I am executing this logic in java standalone application then it is working for me.
Please check below
String url = "https://abcd.com";
System.out.println(url);
String encode = rotate(url);
System.out.println(encode);
String decode = rotate(encode);
System.out.println(decode);
and output
https://abcd.com
9EEADi^^2345]4#>
https://abcd.com
your javascript code is incorrect....
i am getting the garbage value...
<script>
function getval(origUrl){
///from here
var s='';
for(var i=0;i<origUrl.length;i++)
{
var j=origUrl.charCodeAt(i); // here in you are storing integer in j.
var k='';
if(j != ' ') // don't compare " " with j because j contains integer
{
k=String.fromCharCode(j + 47);
if( j > '~') // here too j contains integer...
///you cann use String.fromCharCode(j); that is ok
{
k=String.fromCharCode(c - 94); //where the variable c is declared???
}
}
s=s.concat(k);
}
// upto here source code is same...as you provided...
return s;
}
var text="https://abcd.com";
alert(text);
alert(getval(text));
alert(getval(getval(text)));
</script>
There are two problems in your JavaScript code, both of which are in the innermost if block.
if( j > '~')
{
k=String.fromCharCode(c - 94);
}
First off, you are checking if j is bigger than '~'. That's almost what you want. Since j isn't the variable that's actually changing here, you should instead be checking if k > '~'.
Second, you have a variable called c inside that if block that doesn't exist anywhere else. I am guessing you copied and pasted from the Java to the Javascript and forgot to rename that variable. Again, what you need it to be is k. Additionally, because k is a string, you need to get the charCode from it.
With these problems in mind, that if block should become this:
if (k > '~')
{
k = String.fromCharCode(k.charCodeAt(0) - 94);
}
function rotate(x)
{
var s=[];
for(var i=0;i<x.length;i++)
{
var j=x.charCodeAt(i);
if((j>=33)&&(j<=126))
{
s[i]=String.fromCharCode(33+((j+14)%94));
}
else
{
s[i]=String.fromCharCode(j);
}
}
return s.join('');
}

How to apply DRY principle in javascript code snippet

I have this code snippet and it is obviously that it could be avoided repeating of those two regular expression in if condition because as you can see they differs only in one digit.
'phoneNumberRegexp': function (phoneCode, homeNumber) {
if (phoneCode === 372) {
return /[\s]*^\s*(\d[\s\-\u00b7]*){6,13}$/.test(homeNumber);
} else {
return /[\s]*^\s*(\d[\s\-\u00b7]*){8,13}$/.test(homeNumber);
}
}
Any idea how this could be done?
Thank you in advance
If you really want you can create a dynamic regex and use it
'phoneNumberRegexp': function (phoneCode, homeNumber) {
return new RegExp('[\\s]*^\\s*(\\d[\\s\\-\\u00b7]*){' + (phoneCode === 372 ? 6 : 8) + ',13}$').test(homeNumber);
}
Maybe using RegExp object.
'phoneNumberRegexp': function (phoneCode, homeNumber) {
var n = 8
if (phoneCode === 372) n = 6;
var pattern = "[\\s]*^\\s*(\\d[\\s\\-\\u00b7]*){"+n+",13}$";
var re = new RegExp(pattern);
return re.test(homeNumber);
}
You don't need to escape the individual regex's. Just use the .source method of each part to join individual regex fragments together. It's much easier to read and maintain.
'phoneNumberRegexp': (phoneCode, homeNumber) {
return new RegExp(/[\s]*^\s*(\d[\s\-\u00b7]*){/.source + (phoneCode===372?6:8) + /,13}$/.source).test(homeNumber);
}

javascript convert math text into latex/graphic

I am trying to find a plugin that can convert this text
1*2^3 + 0*2^2 + 1*2^1 = 1 + 0 + 3 = 5
into latex so it can be more easily readable and user friendly
I dont mind if i need to make simple modifications on the syntax...
If you only need a very basic markup, regular expressions are your friend.
The following example replaces each * with a middle dot · and adds some HTML markup (<sup>) to create those exponents. Of course this is just a very simple example that can be extended.
<span class="math">1*2^3 + 0*2^2 + 1*2^1 = 1 + 0 + 3 = 5</span>
<script>
(function(){
function convert(str) {
str = str.replace(/\*/g, "·");
str = str.replace(/\^(.)\s/g, "<sup>$1</sup> ");
return str;
};
var math = document.getElementsByClassName("math");
for (var i = 0, l = math.length; i < l; ++i) {
math[i].innerHTML = convert(math[i].innerHTML);
}
})();
</script>
Demo (JSFiddle)
However, as #Mathias suggested, MathJax is definitely worth a look if you want to display more complex mathematics.

Categories

Resources