JavaScript Reversing For Newbies 
by The Sandman
4th February 1999 
here
 
Title
 
JavaScript Example 1.1
A Simple "Hello World" Function
Written by The Sandman 
 
Rating
( X )Beginner   ( )Intermediate   ( )Advanced   ( )Expert 
 
Introduction
Greetings Reverser's, 

Welcome to the next JavaScript example in this series of two, where we will be continuing on from where we left off in the last tutorial. 

The more I look into this language the better I like it.. Yes, it has many limitations but if you look at it from an educational tool, then it's got everything we could possibly want from it!  This is primarily a thinking language, where the reverser must think quite deeply before making his move. Anyone here like chess?, what a game. Simple and straight forward rules, but with an almost unlimited number of moves. 

Common themes of code run through the majority of password protected web pages that you can find on the web, hardly surprising since many of these pages are merely modified versions of the original script. Find the original script and compare the differences between them to find how best to tackle it. It doesn't take much JavaScript code to create an almost impossible-to- break protected web page, where for example,  the 'protection' is based on a simple XORing of the User's password using a number based the current day/month. Easy to code, hard to reverse.  What's so frustrating for the reverser in this case, is that they can understand and follow the whole protection routine yet, finding the real password to unlocking the page can still be *almost* impossible!. 
 

Tools
Required
  
1 Pen.  
1 sheet of A4 paper.  
An open mind.  
 
About this
tutorial
  
We began with a basic idea to display the words "Hello World" on our web page and used the in-built JavaScript function write() to write these words directly to our web page. Hardly Earth shattering, so for this tutorial we will expand on this concept further and see if we can improve our original routine using a few more JavaScript functions. 
 
 
 
 

routine. 
 

T
H
E
 
E
S
S
A
Y
  
Key Colours:  
JavaScript Objects Names marked in Blue  
JavaScript Properties marked  in Red  
JavaScript Methods marked in purple  

Let's see the JavaScript routine we have created so far.. 

   

Our Script so far Comments 
<script> 
  
<!-- 
  
document.write("Hello World")  
  
//--> 
  
</script>
Start of our script code 
  
Hide script from non JS Browsers. 
  
Script to execute 
  
unhide our script code 
  
Finish executing script
   

Can you see it's obvious limitation?. That's right, we would have to place this whole script in our web page every time we wanted to use it. Hardly what you would call efficient.  Another obvious limitation is that it can only display the words "Hello World", which is okay if you want your web page filled with these words but who wants to do this?.  It would be much better perhaps to make this routine accept any text string we give it. Fortunately, JavaScript is primarily built on the concept of functions which can be used to correct these limitations. 

Functions can be thought of as 'mini-programs', where each function will perform a specific task. Each function can have the facility to have information passed to it where it will then process and return the results back to wherever it was originally called from.  That's right, we can execute functions from anywhere in our web page and have that function be given data to work on. 

Before we go any further, lets explore if you will, the general concept of variables and how the operate in the JavaScript environment. 

Variables act and behave like the PC's internal Registers, which you see in Softice. Variables are in general, used to hold text or numeric data. Unlike in many other computer languages such as Pascal, variables in JavaScript are not 'declared variables' meaning that we don't have to first initialize our variables to accept a particular type of data first before we can use it.  Therefore Variables in JavaScript are 'loose data' types, they can hold any type of data such as string or numeric's by just assigning it a value. 

Next, variables come in two varieties, either Global or Local.  

Global variables can be 'seen' and 'changed' anywhere within the script code, this is very useful if you have several functions that need to know the state or value of a particular variable before it can continue with it's own pre-define task. An example of a Global variable might be a variable that holds the User's typed in password, where this password then has to go through several stages of decryption before it can be checked to see if it matches the one the script is expecting.  

Local variables are just that, they are defined within functions and are local to that function only. Therefore no other function can 'see' or change any local variables that have been defined another function. These type of variables are used for holding data temporarily while the function manipulates it in some way. The important thing to remember about Local variables is that once the function has finished then the local variable will be destroyed and whatever data is held will also be lost, until that is, when that same function is executed again, where the local variables will be created again. 

As with function names, variable names are also case sensitive, so the variable name Pword is treated separately and unique in it's own right from the variable named pword. 

Here is a simple overview of the scope/range of three variables within a script. 

  <script> 

  variable1="This is a Global variabl----------------------------->  
                                                                  :
  Function1() {                                                   :
  variable2="Local variable used within Function1 only"------->   :
                                                              :   : 
  <.............Scope/Range of variable 2.....................<   :
  }                                                               : 
                                                                  : 
  Function2() {                                                   : 
  variable3="Local variable used within Function2 only"------->   : 
                                                              :   :
                                                              :   : 
                                                              :   : 
  <...................Scope/Range of variable 3...............<   : 
                                                                  :
  <-------------------Scope/Range of variable 1-------------------< 
  </script> 
 

Can you see that variable 1 encompasses the whole of the script and that any and all functions declared within the script has full access to it.  Also, note how variables 2 & 3 cover only the function they are created in. Once outside of their functions and variables 2 & 3 die and no longer exist. 

Local variables exist ONLY in the function they were created in. 
Global variables exits everywhere within the script. 

With the variables fundamentals out of the way lets get back to our 'hello World' example.. 

Turning our original routine into a function that we can call anywhere within our web page is relatively easy to carry out. 

We start by creating a function and giving it a name. 

Lets call our new function Hworld. 

With this we can now transfer almost our whole script routine directly inside this function, which, as already mentioned can be thought of as a mini-program. 
 
 

Our New Function Comments 
<script> 
  
<!-- 
 

function Hworld () 
 
 
 
 
 
 
 
 

document.write("Hello World")  

 
 

//--> 
  
</script>

Start of our script code  
   
Hide script from non JS Browsers.  
   
() means this function does not need any data to be passed to it in order for it to carry out it's task. 

{ signifies start of code belonging to a function. 
   
 

Write 'Hello World' directly to our web page 

} signifies end of code belonging to the function 

unhide our script code  
   
Finish executing script

   

If you were to place this script into your web page as shown above then nothing will happen. This is because script functions can only be executed if explicitly called from anywhere within our web page. The benefit of this means we don't now have to add all this code into our web page each time we want to use it. We can now simply execute it by specifying it's name. This is exactly like the Assembler Call sub-routine then return when completed approach. 

Here is the code we need to be able to execute our newly created function. 
 

Executing our Function Comments 
<script> 
  
<!-- 
  
Hworld()  

//--> 
   
</script>

Start of our script code  
   
Hide script from non JS Browsers.  
   
Call our function, note the use of capitals  
   
unhide our script code  
   
Finish executing script
   
Lets try this out... 
 
 
Results from  executing our function.
   
 
Congratulations! You have just created your first JavaScript function..:) 

Okay, there's no time to rest, we still have one more thing to do to make this function more versatile and one that *might* be even useful to us perhaps.. 

At present our JavaScript function requires no information to be passed to it in order for it to carry out it's task, which means it will always display the same two words each time it is executed. Lets change this so that we can tell the function to display whatever text we wish to use.  

Here are the two changes we can make to our function.. 

Change 1.  function Hworld()                              to        function Hworld(Msg) 
Change 2.  document.write("Hello World")        to       document.write(Msg)   

Can you see what we have done?.  

1. We have changed our function so that it now requires data (in this case some text) from us before it will run. From now on any data we pass to our function will be stored temporarily in the variable Msg. 

2. Next, we have altered our write method to now write the text string stored within the variable Msg. 
 

With the new changes in place here's our final revision for our function. 
 
 

Our New Function Comments 
<script>  
   
<!--  
   

function Hworld (Msg)  
  
  

document.write(Msg)    
 

}   
   

//-->  
   
</script>

Start of our script code  
   
Hide script from non JS Browsers.  
   

Notice our function uses a variable to hold any data we may send to it. 

Write the contents of variable Msg directly to our web page 

} signifies end of code belonging to the function 

unhide our script code  
   
Finish executing script

   
All that is left is to alter our calling script so that it will pass information to our function rather than just executing it.. 
 
 
 
Executing our Function Comments 
<script> 
  
<!-- 
  

Hworld("place your text in here")  

//--> 
   
</script>

Start of our script code  
   
Hide script from non JS Browsers.  
   
Call our function, note we are sending a text string to our function 
   
unhide our script code  
   
Finish executing script
   
Will it work?. Lets find out by testing it out here.. 

 

Results from executing our function.
   
 

  
In the next tutorial we shall be exploring Alert box's, FOR loops and a little maths thrown in for good measure. 
 
 

Ob duh
I wont even bother explaining you that you should BUY your software if you intend to use it for a longer period than the allowed one. Should you want to STEAL software instead, you don't need to crack its protection scheme at all: you'll find it on most Warez sites, complete and already regged, farewell. 
Final Notes
Original format of this tut by (c) flipper (upg) 12/30/97 All rights reversed. 
way out
 
 

Page  by The Sandman  
Page Created: 6th February 1999