Home    Training    Downloads    Tutorials    Arbitary    Get Fate    Proxy Info
 
Training session 17: Programming Basics
Difficulty: Medium
Learn the absolute basics of programming if you know nearly nothing
Creator: m101


It seems that every few hours i am on nearly any irc server, i have some kid come in the channel and ask either how to learn to hack, or how to learn to program. Essentially, programming is an extremely important part of hacking. I would really like to see someone who has absolutely no experience in programming create a buffer overflow exploit for the latest ftp program on the market, it would be dificult, if not impossible. This tutorial is not written for people who already understand a language, but more for those who have no idea and are one of those 'kids' that really want to learn to hack.

A program is a set of specific instructions to do a specific task, for example a program can be molded around a real life situation to explain how it can be 'automated':

1. Get beer
2. Open beer
3. Drink beer

This is a very simple set of instructions on how to have a good time. No step can be performed before you have done all the steps before it. So for example you cant open a beer if you havent got one to open. Computers work on much the same basis, everything can be done in simple steps. A program is made using the following types of commands:

Sequence
Itteration
Choice
Input/Output
Variables

Ok, if you are reading this, you probably have no idea what i am talking about so i will try to explain each type of command.

Sequence is just a set of instructions like our little list to drink a beer. It is exectuted line by line. 1,2,3

Itteration is looping. So if you were drinking your beer you would need to keep it in your mouth for a while, so we would loop it. Just think of it as repeating an instructions a set ammount of times. Drinking five beers is an itteration 5 times.

Choice is as it says, it is a descision that determines what a program will do. It would say for example, if the is empty get another one, if beer isnt empty keep drinking. This is basically what choice does.

Input is just information taken from the keyboard/mouse. When you type something in and the program acts on it, it is a mix of choice and input. Output is anything that you see. So this file is actually a form of ouput. Drinking beer is input, the hangover is output.

Variables are just something that holds something else. You gut is a variable in our program, it holds the ammount of beer we have drunken. It can increase, or decrease if we spew.

Now to look at how we would do something in a programming language:

Basic:
print "m101 0wnz"

C++:
#include
int main()
{
cout << "m101 0wnz";
return 0;
}

Pascal:
PROGRAM printer;
begin
writeln('m101 0wnz');
end.

All of these programs do exactly the same thing, they print 'm101 0wnz' to the screen, just the code to do it diferent. Although basic is obviously the most simple, it is definately not the most advanced, remember, by making something simpler, you are forced to remove functionality.

Now for a simple loop:

Basic:
for i=1 to 10
print "m101 0wnz"
next i

C++:
#include
int i;
int main()
{
for(i=1;i<10;i++)
{
cout << "m101 0wnz" << endl;
}
return 0;
}

Pascal:
program printstuff;
var i:integer;
begin
for i:=1 to 10 DO
begin
writeln('m101 0wnz');
end;
end.

All those lines of code merely print 'm101 0wnz' to the screen 10 times. If we were to write the same code without itteration it would look like the following:

Basic:
print "m101 0wnz"
print "m101 0wnz"
print "m101 0wnz"
print "m101 0wnz"
print "m101 0wnz"
print "m101 0wnz"
print "m101 0wnz"
print "m101 0wnz"
print "m101 0wnz"
print "m101 0wnz"

C++:
#include
int main()
{
cout << "m101 0wnz" << endl;
cout << "m101 0wnz" << endl;
cout << "m101 0wnz" << endl;
cout << "m101 0wnz" << endl;
cout << "m101 0wnz" << endl;
cout << "m101 0wnz" << endl;
cout << "m101 0wnz" << endl;
cout << "m101 0wnz" << endl;
cout << "m101 0wnz" << endl;
cout << "m101 0wnz" << endl;
return 0;
}

Pascal:
program printstuff;
begin
writeln('m101 0wnz');
writeln('m101 0wnz');
writeln('m101 0wnz');
writeln('m101 0wnz');
writeln('m101 0wnz');
writeln('m101 0wnz');
writeln('m101 0wnz');
writeln('m101 0wnz');
writeln('m101 0wnz');
writeln('m101 0wnz');
end.

Itteration is just a smaller way to write alot of commands as you should have noticed. Choice is where programming becomes a little more interesting:

Basic:
if a=1 then print "m101 0wnz"

C++:
#include
int a;
int main()
{
if(a==1)
{
cout << "m101 0wnz";
}
return 0;
}

Pascal:
program choice;
var a:integer;
begin
if (a=1) then
begin
writeln('m101 0wnz');
end;
end.

As you should notice, the program bascially checks to see whether 'a=1' and if it is prints "m101 0wnz". Input and output is what allows the user to interact with the computer:

Basic:
input a$
print a$

C++:
#include
#include
char a[50];
int main()
{
gets(a);
cout << a;
return 0;
}

Pascal:
program inputoutput
var a:string[50];
begin
readln(a);
writeln(a);
end.

All that program did was take input, and print it to the screen. Programs are based around variables, there are diferent types of variables, but they are all similar in use. An example of this is if we want to print a number, or we wanted to print a word:

Basic:
a=3
b$="m101 0wnz"
print a
print b$

C++:
#include
#include
int a;
char b[50];
int main()
{
a=3;
strcpy(b,"m101 0wnz");
cout << a << endl << b;
return 0;
}

Pascal:
program variable
var a:integer;
var b:string[50];
begin
a:=3;
b:="m101 0wnz";
writeln(a);
writeln(b);
end.

As you can see, every langauge has similar but very diferent ways of handling instructions. For example, if you had the string "m101 0wnz" and you only wanted to print the third character, the code to print it would look like the following in each language:

Basic:
a$="m101 0wnz"
print right$(left$(a$,3),1)

C++:
#include
#include
char a[50];
int main()
{
strcpy(a,"m101 0wnz");
cout << a[3];
return 0;
}

Pascal:
program charactertrace
var a:string[50];
begin
a:="m101 0wnz";
writeln(a[3]);
end;

Basic has now become more dificult to find our byte than it was in both Pascal and C++. The two other languages require more preparation to do a simple command, but the simplicity of performing a more complex function becomes merely 'a[3]'. You dont necessarily have to understand the last example, just pickup on how each language is diferent, and better in certain situations.

Challenges:

1. First challenge is to write a program that will print an output of:

1
2
3
4
5
6
7
8
9
10

This can be done in any language you choose, remember to look at the previous examples if you get stuck. Try to fit the answer in as little code as possible. Here is the answer:

Basic:
for i=1 to 10
print i
next i

C++:
#include
int i;
int main()
{
for(i=1;i<10;i++)
{
cout << i << endl;
}
return 0;
}

Pascal:
program printstuff;
var i:integer;
begin
for i:=1 to 10 DO
begin
writeln(i);
end;
end.

Hopefully your program will work first time, if it doesnt, dont give up.

Next challenge is to make a program that takes a number, then asks for a guess, and if the number is the same, congratulates the person. The answer is:

Basic:
a=213
input b
if a=b then print "congrats"

C++:
#include
char a[50];
char b[50];
int main()
{
strcpy(a,"213");
gets(b);
if((strcmp(a,b)==0)
{
cout << "congrats";
}
return 0;
}

Pascal:
program test
var a,b:integer;
begin
a:=213;
readln(b);
if (a=b) then
begin
writeln('congrats');
end;
end.

Next challenge is to make a program that will print "m101 0wnz" in reverse, this can be a pretty trivial task to think about. Here is a hint, create a loop that will just print a single character of the string each time the loop goes around. The answer is:

Basic:
a$="m101 0wnz"
for i=1 to 10
print right$(left$(a$,9-i),1);
next i

C++:
#include
int i;
char a[50];
int main()
{
strcpy(a,"m101 0wnz");
for(i=9;i>1;i--)
{
cout << a[i];
}
return 0;
}

Pascal:
program printstuff;
var i:integer;
var a:string[50];
begin
a:="m101 0wnz";
for i:=1 to 9 DO
begin
write(a[9-i]);
end;
end.

If you couldnt complete one, or all of the challenges it really doesnt matter, just take the code and fiddle with it, change values, make it do stupid things. Do not be afraid to be wrong, always give it a try. Look at other peoples code, and study programming references to discover the finer points of programming, and how to add things like colour.

Name

URL or Email

Message