Tuesday, November 28, 2006

Hacking A Crime...Yet Some Do..!!!

Hacking !!!!!!!!!

Hacker is someone who creates and modifies computer software and computer hardware, including computer programming, administration, and security-related items. The term usually bears strong connotations, but may be either favorable or denigrating depending on cultural context (see the Hacker definition controversy). Common definitions include:
In computer programming, a hacker is a software designer and programmer who builds elegant, beautiful programs and systems. A hacker can also be a programmer who hacks or reaches a goal by employing a series of modifications to exploit or extend existing code or resources. For some, "hacker" has a negative connotation and refers to a person who "hacks" or uses kludges to accomplish programming tasks that are ugly, inelegant, and inefficient. This negative form of the noun "hack" is even used among users of the positive sense of "hacker".
In computer security, a hacker is a person who specializes in work with the security mechanisms for computer and network systems. While including those who endeavor to strengthen such mechanisms, it more often is used, especially in the mass media, to refer to those who seek access despite them.
In other technical fields, hacker is extended to mean a person who makes things work beyond perceived limits through their own technical skill, such as a hardware hacker, or reality hacker.
In hacker culture, a hacker is a person who has attained a certain social status and is recognized among members of the culture for commitment to the culture's values and a certain amount of technical knowledge.


Hacker: Highly skilled programmer

The positive usage of hacker is one who knows a (sometimes specified) set of programming interfaces well enough to program rapidly and expertly. This type of hacker is well-respected (although the term still carries some of the meaning of hack), and is capable of developing programs without adequate planning or where pre-planning is difficult or impossible to achieve. This zugzwang gives freedom and the ability to be creative against methodical careful progress. At their best, hackers can be very productive. The technical downside of hacker productivity is often in maintainability, documentation, and completion. Very talented hackers may become bored with a project once they have figured out all of the hard parts, and be unwilling to finish off the "details". This attitude can cause friction in environments where other programmers are expected to pick up the half finished work, decipher the structures and ideas, and bullet-proof the code. In other cases, where a hacker is willing to maintain their own code, a company may be unable to find anyone else who is capable or willing to dig through code to maintain the program if the original programmer moves on to a new job.
Additionally, there is sometimes a social downside associated with hacking. The stereotype of a hacker as having gained technical ability at a cost in social ability has historical basis in an uncomfortable amount of factual foundation in many individuals. While not universal, nor even restricted to hackers, the difficulty in relating to others and the often abrasive personalities of some hackers makes some of them difficult to work with or to organize into teams. On the other hand, it is not uncommon for hackers to thrive on social interaction.

Introduction

This is a short introduction to the art of programming, with examples written in the programming language Python. (If you already know how to program, but want a short intro to Python, you may want to check out my article Instant Python.) This article has been translated into Italian, Polish, Japanese, Serbian, Brazilian Portuguese, and Dutch, and is in the process of being translated into Korean.
This page is not about breaking into other people's computer systems etc. I'm not into that sort of thing, so please don't email me about it. really about, take a look at hackerethic.org.
-->Happy Hacker web site.
-->
Note: To get the examples working properly, write the programs in a text file and then run that with the interpreter; do not try to run them directly in the interactive interpreter - not all of them will work. (Please don't ask me on details on this)..


The Environment

To program in Python, you must have an interpreter installed. It exists for most platforms (including Macintosh, Unix and Windows). More information about this can be found on the Python web site. You also should have a text editor (like emacs, notepad or something similar).

What is Programming?

Programming a computer means giving it a set of instructions telling it what to do. A computer program in many ways resembles recipes, like the ones we use for cooking. For example [1]: Fiesta SPAM Salad
Ingredients:
Marinade:
1/4 cup lime juice
1/4 cup low-sodium soy sauce
1/4 cup water
1 tablespoon vegetable oil
3/4 teaspoon cumin
1/2 teaspoon oregano
1/4 teaspoon hot pepper sauce
2 cloves garlic, minced
Salad:
1 (12-ounce) can SPAM Less Sodium luncheon meat,
cut into strips
1 onion, sliced
1 bell pepper, cut in strips
Lettuce
12 cherry tomatoes, halved
Instructions:
In jar with tight-fitting lid, combine all marinade ingredients;
shake well. Place SPAM strips in plastic bag. Pour marinade
over SPAM. Seal bag; marinate 30 minutes in refrigerator.
Remove SPAM from bag; reserve 2 tablespoons marinade. Heat
reserved marinade in large skillet. Add SPAM, onion, and
green pepper. Cook 3 to 4 minutes or until SPAM is heated.
Line 4 individual salad plates with lettuce. Spoon hot salad
mixture over lettuce. Garnish with tomato halves. Serves 4.
Of course, no computer would understand this... And most computers wouldn't be able to make a salad even if they did understand the recipe. So what do we have to do to make this more computer-friendly? Well – basically two things. We have to (1) talk in a way that the computer can understand, and (2) talk about things that it can do something with.
The first point means that we have to use a language – a programming language that we have an interpreter program for, and the second point means that we can't expect the computer to make a salad – but we can expect it to add numbers, write things to the screen etc.


Hello...

There's a tradition in programming tutorials to always begin with a program that prints "Hello, world!" to the screen. In Python, this is quite simple: print "Hello, world!"
This is basically like the recipe above (although it is much shorter!). It tells the computer what to do: To print "Hello, world!". Piece of cake. What if we would want it to do more stuff? print "Hello, world!"
print "Goodbye, world!"
Not much harder, was it? And not really very interesting... We want to be able to do something with the ingredients, just like in the spam salad. Well – what ingredients do we have? For one thing, we have strings of text, like "Hello, world!", but we also have numbers. Say we wanted the computer to calculate the area of a rectangle for us. Then we could give it the following little recipe: # The Area of a Rectangle
# Ingredients:
width = 20
height = 30
# Instructions:
area = width*height
print area
You can probably see the similarity (albeit slight) to the spam salad recipe. But how does it work? First of all, the lines beginning with # are called comments and are actually ignored by the computer. However, inserting small explanations like this can be important in making your programs more readable to humans.
Now, the lines that look like foo = bar are called assignments. In the case of width = 20 we tell the computer that the width should be 20 from this point on. What does it mean that "the width is 20"? It means that a variable by the name "width" is created (or if it already exists, it is reused) and given the value 20. So, when we use the variable later, the computer knows its value. Thus, width*height
is essentially the same as 20*30
which is calculated to be 600, which is then assigned to the variable by the name "area". The final statement of the program prints out the value of the variable "area", so what you see when you run this program is simply 600
Note: In some languages you have to tell the computer which variables you need at the beginning of the program (like the ingredients of the salad) – Python is smart enough to figure this out as it goes along.


Feedback

OK. Now you can perform simple, and even quite advanced calculations. For instance, you might want to make a program to calculate the area of a circle instead of a rectangle: radius = 30
print radius*radius*3.14
However, this is not significantly more interesting than the rectangle program. At least not in my opinion. It is somewhat inflexible. What if the circle we were looking at had a radius of 31? How would the computer know? It's a bit like the part of the salad recipe that says: "Cook 3 to 4 minutes or until SPAM is heated." To know when it is cooked, we have to check. We need feedback, or input. How does the computer know the radius of our circle? It too needs input... What we can do is to tell it to check the radius: radius = input("What is the radius?")
print radius*radius*3.14
Now things are getting snazzy... input is something called a function. (You'll learn to create your own in a while. input is a function that is built into the Python language.) Simply writing input
won't do much... You have to put a pair of parantheses at the end of it. So input() would work – it would simply wait for the user to enter the radius. The version above is perhaps a bit more user-friendly, though, since it prints out a question first. When we put something like the question-string "What is the radius?" between the parentheses of a function call it is called passing a parameter to the function. The thing (or things) in the parentheses is (or are) the parameter(s). In this case we pass a question as a parameter so that input knows what to print out before getting the answer from the user.
But how does the answer get to the radius variable? The function input, when called, returns a value (like many other functions). You don't have to use this value, but in our case, we want to. So, the following two statements have very different meanings: foo = input
bar = input()
foo now contains the input function itself (so it can actually be used like foo("What is your age?"); this is called a dynamic function call) while bar contains whatever is typed in by the user.


Bigger Programs – Abstraction

If you want an overview of the contents of a book, you don't plow through all pages – you take a look at the table of contents, right? It simply lists the main topics of the book. Now – imagine writing a cookbook. Many of the recipes, like "Creamy Spam and Macaroni" and "Spam Swiss Pie" may contain similar things, like spam, in this case - yet you wouldn't want to repeat how to make spam in every recipe. (OK... So you don't actually make spam... But bear with me for the sake of example :)). You'd put the recipe for spam in a separate chapter, and simply refer to it in the other recipes. So – instead of writing the entire recipe every time, you only had to use the name of a chapter. In computer programming this is called abstraction.
Have we run into something like this already? Yup. Instead of telling the computer exactly how to get an answer from the user (OK - so we couldn't really do this... But we couldn't really make spam either, so there... :)) we simply used input - a function. We can actually make our own functions, to use for this kind of abstraction.
Let's say we want to find the largest integer that is less than a given positive number. For instance, given the number 2.7, this would be 2. This is often called the "floor" of the given number. (This could actually be done with built-in Python function int, but again, bear with me...) How would we do this? A simple solution would be to try all possibilities from zero: number = input("What is the number? ")
floor = 0
while floor <= number: floor = floor+1 floor = floor-1 print "The floor of", number, "is", floor Notice that the loop ends when floor is no longer less than (or equal to) the number; we add one too much to it. Therefore we have to subtract one afterwards. What if we want to use this "floor"-thing in a complex mathematical expression? We would have to write the entire loop for every number that needed "floor"-ing. Not very nice... You have probably guessed what we will do instead: Put it all in a function of our own, called "floor": def floor(number): result = 0 while result <= number: result = result+1 result = result-1 return result New things in this example... Functions are defined with the keyword def, followed by their name and the expected parameters in parentheses. If the function is to return a value, this is done with the keyword return (which also automatically ends the function. Now that we have defined it, we can use it like this: x = 2.7 y = floor(2.7) After this, y should have the value 2. It is also possible to make functions with more than one parameter: def sum(x,y): return x+y Exercise 2 Write a function that implements Euclid's method for finding a common factor of two numbers. It works like this: You have two numbers, a and b, where a is larger than b You repeat the following until b becomes zero: a is changed to the value of b b is changed to the remainder when a (before the change) is divided by b (before the change) You then return the last value of a Hints: Use a and b as parameters to the function Simply assume that a is greater than b The remainder when x is divided by z is calculated by the expression x % z Two variables can be assigned to simultaneously like this: x, y = y, y+1. Here x is given the value of y (that is, the value y had before the assignment) and y is incremented by one ..