____________________________________________________________________

The Perl Weekly Journal---By Ankit Fadia

____________________________________________________________________

`########### # ### ######### ## #`
`############ ### ##### ### ##### ### ##`
`##### #### ##### ####### ### ### #### ###`
`##### ##### ####### ######### ### # #### ####`
`#### ##### ####### ########### ### ### ####`
`######### ####### #### #### ### #######`
`########### ##### ##### ##### ### ## #######`
`############# ### ## ###### ######### ### #### #########`
`###### #### # #### ######## ##### ## ###### ### ###`
`###### #### ########## #### #### ######### ### #####`
`##### #### ########## ### ### ####### #### ######`
`############# ########## ## ## ##### #### ####`
`########### ######## ## ## ### ### ##```

`####### # # # #`
`########## ## ## ## ##`
`### ### ## ## ### ###`
`### # ### ### ##### #### Black Sun Research Facility`
`### ## ## ###### #### http://blacksun.box.sk`
`### ## ## ####### #### ASCII By : cyRu5`
`# ### ### ### #### #######`
`### #### #### #### ### #####`
`########### ########### ## ###`
`######### ####### # #`

You cannot become a good hacker unless you have some programming knowledge. Not only for Hacking, Perl is very much useful for developing security related and also normal but useful programs.In the Perl Weekly Journal I will be starting from the basics of Perl and then will move on to some cool advanced stuff.I am assuming that you do not have any previous programming experience, although a sound background in C, Basic or JavaScript will help you tremendously.

Perl : The Basics

Perl was born in 1987 and was developed by Larry Wall by fusing the Unix utility awk with a system administartion tool he had developed. Perl's development has been done on the lines of including all the useful and important aspects of other programming languages and remove the not so useful aspects. Now Perl is an interpreted language,that means that the Perl code is run as it is and it is not complied like other languages.When you first run a Perl program, it is first compiled into a bytecode, which is then converted into machine instructions.

Now first of all, before you can start writing your own Perl programs, you need ActivePerl the Perl Interpreter.You can download ActivePerl for Win32 from:

http://www.activestate.com/

Follow the links for the latest build and download it.It is around a 5MB download. After installing ActivePerl ensure that the file perl.exe is in your path statement.Although ActivePerl Build 509 sets the path automatically during setup, just make sure that your path statement contains reference to the file perl.exe by typing "set" at the command prompt(without quotes), now look for the "PATH" environment variable and make sure that it contains the line "c:\perl\bin" in that statement.Normally it would contain this line, but if it doesn' then open the file c:\autoexec.bat in Notepad and add the following line:

PATH=%PATH%;.;c:\perl\bin

Now save the file and reboot or update the environment for that session by running the file autoexec.bat by going to DOS and typing autoexec.bat

Note:Nt users will just have to update the current system environment by going to

Control Panel > System

Now let's start by the obligatory Hello World Program.Now to write Perl programs you do not need any special Perl Text Editor, NotePad would do just fine. So launch Notpad and type the following:

Print "Hello World\n"; #This prints Hello World on the Screen

Now save the file by the name "first.pl". You can replace first by any name of your choice but just remember that the file should have a .pl extension. Now go the DOS Prompt and then to the folder in which you had saved the above file and type:

C:\myfiles>first.pl

Note: Replace filename with the name of the file that you chose while saving.

If the above program does not work that is you get an error, then check your path statement or try to write perl filename.pl instead of just filename.pl

Now lets analyse the program,the word print calls the print function which takes the text from within the quotes and displays it on the screen.The "\n" symbolises a new line or the carriage return. Almost all lines in Perl end with a semicolon.

Scalars

Now let's make the above program a bit more complex by introducing a scalar.

$scalarvar= 'Hello World\n' ; #the Variable $scalarvar has the value Hello World\n

print "$scalarvar" ; #Prints value of Variable $scalarvar

Now scalars are declared by the $ sign followed by the Variable name. The first line feeds the text with the quotes into the scalar whose name is scalarvar.We know the scalarvar is a scalar because it is preceeded by the $ sign.

Now you must be wondering why I have used single quotes in the first line and double in the second. Now the reason behind this is the fact that Perl performs variable interpolation within double quotes this means that it replaces the variable name with the value of the variable. This will become more understandable with the following examples,

$scalarvar= 'Hello\n' ; # Variable $scalarvar has the value Hello\n

print '$scalarvar' ; # But as we use single quotes there is no variable interpolation and

function print prints $scalarvar on the screen.

Output will be:

$scalarvar

The following is an example of Variable Interpolation:

$scalarvar= 'Hello' ;

print "$scalarvar" ; # In this case Variable Interpolation takes place the the Print function

is fed the value of the variable $scalarvar.

Output will be

Hello

By now the difference between single quotes and double quotes would have become quite clear.

Interacting with User by getting Input

The Diamond Operator i.e < > is the Perl equivalent of the C function scanf and the C++ function cin. It basically grabs input from the user to make a program interactive.It will become more clear after the following example:

print 'Enter your Name:' ;

$username= <> ; #The User will enter a text which will be fed into the scalar

print 'Hi $username' ;

Output will be:

Enter your Name: Ankit

Hi Ankit

This program will print the text Enter your Name: on the screen and will wait for user input.

The text entered by the user will be fed into the scalar $username. Then the program will print Hi followed by the text entered by the User.

chomp( ) and chop( )

Now sometimes you need to manipulate strings and do this there are many functions available which can be used. So when do you need to use chop( ) and chomp( ) Consider the following situation……You need to write a program to print the name and age of the user which would be input by the User itself. Now consider the following code…

print "Enter your name:" ;

$name=<> ;

print "Enter your age:" ;

$age=<> ;

print "$name";

print "$age";

Output will be:

Enter your name:Ankit

Enter your age:14

Ankit

14

Now what happened here? Why did Perl print Ankit and 14 in different lines? There was no newline i.e. "\n" character in this program. Now what actaully happened lies in the fact that when the user is given the Input prompt that is when the user is needed to enter some input, Perl keeps on accepting input from the user as long as the user provides the ending operator( The ending operatort is Carriage Return or Enter). When the ending operator is provided by the user then Perl stops taking input and assigns the data input by the user to the variable specified including the Carriage Return.

This means that in the above program the value of the scalar $name is Ankit followed by carriage return which is equivalent to "Ankit\n".

So when we print the scalar $name then the value of $name is printed followed by carriage return or "\n".

To avoid this problem we use chop( ) and chomp( ).

The basic differnece between these two functions will become clearer with the below example:

$var1="Ankit";

chop($var1);

print $var1;

The Output will be:

Anki

In the below example:

$var1="Ankit";

chomp($var1);

print $var1;

The Output will be:

Ankit

Now the differnece between chop( ) and chomp( ) is that chop( ) will remove the last character of the string irrespective of the fact, what it is. While chomp( ) will remove the last charcter if and only if the last character is "/n".

This means that

$var1="Ankit\n";

chomp($var1);

print $var1;

and

$var1="Ankit\n";

chomp($var1);

print $var1;

will have the same effect as the last character here is "\n". So our problem of printing both the name and age of the user on the same line can be solved by the following snippet of code:

print "Enter your name:" ;

$name=<> ;

chomp($name);

print "Enter your age:" ;

$age=<> ;

chomp($age);

print "$name";

print "$age";

Output now would be:

Enter your name:Ankit

Enter your age:14

Ankit14

Operators

Perl too has the same basic operators found in other programming languages but it does have some additional operators too.

Binary Arithematic Operators

op1+op2 Addition

op1-op2 Substraction

op1*op2 Multiplication

op1/op2 Division

op1 % op2 Modulus

The Exponentiation Operator (**)

This is the raise to the power of operator,

For Example:

$var= 5;

$var1= $var ** 3;

print $var1 ;

Output will be:

125

The Unary Arithmatic Operators

+op and -op are used to change the sign of the operator.For example:

$var=4;

$var1= -4;

print var1;

Output will be:

-4

++op and --op are used to increase or decrease the value of the variable value before usage and op++ and op-- are used to increase or decrease the value of the variable after usage. Consider the following examples:

$var=4;

print ++$var;

This will print:

5

$var=4 ;

print $var++;

This will print 4 on the screen and the value of $var will become 5 after printing 4 on the screen.

$letter="a";

$letter++

Now $letter is b

$letters="xz";

$letters++;

Now $letters is ya

Other General Operators

The Concatenation Operator ( . )

$var="Hack" ;

$var = $var . 's';

Now $var become Hacks instead of Hack.

The " x" operator

$var = "ab" x 4;

print $var;

Will print abababab

Conditional Statements

First of all understand the differnece between = and ==

Now the = opertor assigns the variable on the left with the value on the right and is used to assign variable their values and the == operator compares and checks if the value on the left is equal to the value on the right and is used in the IF THEN ELSE statement.

Now Perl includes the followinf logical operators:

X > Y This is true if the value of x is greater than y

X < Y This is true if the value of y is greater than x

X <= Y This is true if X is equal to or smaller than y

X >= Y This is true if X is equal to or greater than y

The above operators are for numbers and for strings the following operators are used:

eq Is the alphanumerical equivalent to == and tests for strings whereas == tests for

numbers

x lt y Is true if the string x comes before string y in alphabetical order.

x gt y Is true if the string x comes after string y in alphabetical order.

x le y Is true if the string x comes before or is equal to string y.

x ge y Is true if the string x comes after or is equal to string y.

Now that you know the Logical operators lets move on the IF THEN statement. The basic syntax of an If then statement is:

if ( Condition ) { Body } executes body if condition evalutates to true.

IF you have understood the logical operaotr and the basic syntax then forming a conditional If Then statement is pretty simple.Anyway I would be providing one or two examples.

print" Enter your name:";

$name=< > ;

chomp $name ;

if ( $name eq 'Ankit') {

print "Hi Ankit";

}

This prints HI Ankit if the User's name is Ankit. Now lets make things a bit more interesting by inserting the else clause into the IF THEN statement and turning it into an IF THEN ELSE statement, then the above example would become:

print" Enter your name:";

$name=< > ;

chomp $name ;

if ( $name eq 'Ankit') {

print "Hi Ankit";

}

else {

print " You are not autorised to use this PC" ;

}

If the name input by the user is something other than Ankit then Perl executes the else statement and if the name input by the user is Ankit then it will execute the then statement i.e the command after the first {.

Assignment Operators

$var .= 's' is equivalent to $var = $var . 's'

$var x= 7 is equivalent to $var = $var x 7

$var += 7 is equivalent to $var = $var + 7

$var -= 7 is equivalent to $var = $var - 7

$var *= 7 is equivalent to $var = $var * 7

$var /= 7 is equivalent to $var = $var / 7

The ?: Operator

This is pretty much similar to the IF then Else Statement but is just it's shorthand.

For example;

$var=( $num == "5") ? "Ankit" : "End" ;

The Variable $var is assigned the value Ankit if $num== 5 else $var is assigned the value End

Well that wraps up the first Perl Issue. I hope you liked it. If you find any errors do please contact me.

Ankit Fadia

ankit@bol.net.in

To receive more tutorials on Hacking, Perl, C++ and Viruses/Trojans join my mailing list:

Send an email to programmingforhackers-subscribe@egroups.com to join it.

Visit my Site to view all tutorials written by me at: http://www.crosswinds.net/~hackingtruths