Tuesday, February 9, 2010

Python - A Real Beginners Guide

PYTHON

I. Intro
II. My First Program
III. Variables, Numbers and Strings
IV. String Manipulation
V. Operators
VI. Arrays/Lists
VII. Loops and Conditionals
a. The IF/ELIF/ELSE Statements
b. The WHILE Statement
VIII Bye bye! Good luck!

+---------------------------------------+
I. Intro
+---------------------------------------+
PYTHON! According to Python.org, this is what Python is...:

"Python is a dynamic object-oriented programming language that can be
used for many kinds of software development. It offers strong support
for integration with other languages and tools, comes with extensive
standard libraries, and can be learned in a few days. Many Python
programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code"

More simply however, Python is an easy-to-read, highly compatible,
oft-used programming language that is powerful and quick. It is often
compared to languages such as Perl, Ruby, Java, etc

ABOUT THIS GUIDE:
- This guide is intended for COMPLETE beginners to programming
languages, and is suggested to most as a first language, as it is as
mentioned before, an easy-to-read and simple language :)

REQUIREMENTS FOR THIS GUIDE:
- Having the latest version of Python installed on your computer. The
installers can be found at http://www.python.org/download/


+---------------------------------------+
II. My First Program
+---------------------------------------+
FOR the first program, we will be creating a small program that writes
"Hello World!" on screen.

Here is the code:
CODE :
__________________________________________________________________________
>>>print("Hello World!")
__________________________________________________________________________

BREAKDOWN:
print() - the typical function (functions will be covered more later
on) to write sentences and variables to the screen.

NOTE: It should also be noted that when using the print function, you
must remember that when you try print multiple things, e.g.
>>>print("Lol", "and", "hi"), a space will immediately be placed
between each part.

NOTE2: Instead of using print(), we also can type the string, number,
or variable and press enter in Python Shell to print the value of it
(remember that you will have to wrap strings in quotation marks if you
decide to use this method).


+---------------------------------------+
III. Variables, Numbers and Strings
+---------------------------------------+
VARIABLES are ways of holding information inside a word, to be able to
call the information back later in a program. The way to assign a value is..:
CODE :
__________________________________________________________________________
>>>#This is a comment line... Comment lines in Python are always
>>>#Preceded by a #
>>>#A variable can either hold a string (words) or a number
>>>varName = "variable"
>>>varName1 = 2009
__________________________________________________________________________

Variables can be changed later in the program. They can also hold a
formula or function. In addition, you can assign the same values to
several variables at once. For examples...

CODE :
__________________________________________________________________________
>>>#This variable holds a total of 25
>>>varFormula = 5*5
>>>
>>>#These variables both hold a value of 45
>>>varX = varY = 40+5
>>>
>>>#This variable holds a function that finds out the length of a
>>>#string or other value
>>>varLength = len("Hello World!")
>>>
>>>#The len() function does not work with numbers!
>>>#Using print(varLength) will output the length of "Hello World!"
>>>#which is 12.
>>>
>>>#Remember that when using the len() function, it counts every
>>>#character, including the space, and not just letters.
__________________________________________________________________________

+---------------------------------------+
IV. Word Indexing
+---------------------------------------+
BEING able to control strings is a vital part of programming. We
already know a couple of basic functions that allow us to manipulate or use strings, i.e. print() and len(). Another useful feature of Python is word indexing: being able to pick out certain letters in strings.

Here is an example of how to use word indexing:

CODE :
__________________________________________________________________________
>>>Hello = "Hello World!" #Establishes a variable...
>>>
>>>Hello[0] #Writes the first letter of the variable
"H"
>>>
>>>Hello[1:] #Writes all letters after the first letter
"ello World!"
>>>
>>>Hello[:5] #Writes all letters up to the sixth letter
"Hello"
>>>
>>>Hello[3:7] #Writes letters between the third and eighth letters
"lo W"
>>>
>>>Hello[3:-1] #Writes letters between position 3 and -1
"lo World"
__________________________________________________________________________

Yes, strangely enough, the first letter is indexed as [0]... Here is a
little table to illustrate index positions.
CODE :
__________________________________________________________________________
+---+---+---+---+
| A | B | C | D | = String
+---+---+---+---+
| 0 | 1 | 2 | 3 | = Positive indices
+---+---+---+---+
|-3 |-2 |-1 | ? | = Negative indices
+---+---+---+---+
__________________________________________________________________________

As you will see, there is absolutely no way of selecting a whole string using negative numbers... Of course, there are other ways of doing that.



+---------------------------------------+
V. Operators
+---------------------------------------+
OPERATORS are VERY important in Python... And sound much more
complicated than they really are. Operators are simply mathematical
symbols that do stuff for programming languages. Here is the table of
operators and how they work:
CODE :
__________________________________________________________________________
MATHEMATICAL OPERATORS - These produce a value
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|+..............|Addition.......|a + 5 = 10.............|
+---------------+---------------+-----------------------+
|-..............|Subtraction....|a - 5 = 0..............|
+---------------+---------------+-----------------------+
|*..............|Multiplication.|a * a = 25.............|
|**.............|Powers.........|a **3 = 25*25*25 = 125.|
+---------------+---------------+-----------------------+
|/..............|Division.......|a / a = 1..............|
|//.............|Rounds to floor|a // 0.3 = 16..........|
|%..............|Gives remainder|a % 2 = 1..............|
+---------------+---------------+-----------------------+

ASSIGNMENT OPERATORS - These give values to a variable
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|=..............|Assigns a value|a = 5..................|
+---------------+---------------+-----------------------+
|-=.............|Subtraction....|a-=10 is the same as...|
|...............|assigner.......|a = a - 10.............|
+---------------+---------------+-----------------------+
|*=.............|Multiplication.|a *= 10 is the same as.|
|...............|assigner.......|a = a * 10.............|
+---------------+---------------+-----------------------+
|**=............|Power assigner.|a **= 2 is the same as.|
|...............|...............|a = a ** 2.............|
+---------------+---------------+-----------------------+
|/=.............|Division.......|a /= 10................|
|...............|assigner.......|a = a / 10.............|
+---------------+---------------+-----------------------+
etc...etc...etc...etc...etc...etc...etc...etc...etc...etc

COMPARISON OPERATORS - These evaluate the truth of a statement
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|==.............|Is equal to....|a == 5.........TRUE....|
+---------------+---------------+-----------------------+
|!=.............|Not equal to...|a != 5.........FALSE...|
+---------------+---------------+-----------------------+
|>..............|More than......|a > a..........FALSE...|
|<..............|Less than......|a < 10.........TRUE....|
+---------------+---------------+-----------------------+
|>=.............|More than......|a >= a.........TRUE....|
|...............|or equal to....|a >= 6.........FALSE...|
+---------------+---------------+-----------------------+

BOOLEAN OPERATORS - These are used to link COMPARISON OPERATORS
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|and............|Both expression|a==5 and a>1...........|
|...............|must be true...|Evaluates to true......|
+---------------+---------------+-----------------------+
|or.............|Either expressi|a>6 or a<3.............|
|...............|on must be true|Evaluates to false.....|
+---------------+---------------+-----------------------+
|in.............|Checks if value|arr=["lol", "rofl"]....|
|...............|is in an array.|"lol" in arr...TRUE....|
+---------------+---------------+-----------------------+
__________________________________________________________________________

These are the main operators that you will need when programming in
Python.


+---------------------------------------+
VI. Arrays/Lists
+---------------------------------------+
ARRAYS/lists can be indexed exactly like words, but can store multiple
strings, numbers and variables. Also, you can append arrays: changing
them as you see fit. Here is the correct way of starting an array and changing it.
[/code]
>>>#Starts an array, 5 "elements" long
>>>arr = ["H", "e", "l", "l", "o"]
>>>
>>>arr[0] #Writes the first element in the array
"H"
>>>
>>>#Here are 3 functions that can change lists
>>>#These will be explained at the end of this chapter
>>>
>>>arr.append("!")
>>>arr # <- Prints the array
["H", "e", "l", "l", "o", "!"]
>>>
>>>arr.insert(1, "a")
>>>arr
["H", "a", "l", "l", "o", "!"]
>>>
>>>arr.extend(["Wo", "rld", "!"])
>>>arr
["H", "a", "l", "l", "o", "!", "Wo", "rld", "!"]
>>>
>>>arr.remove("!")
>>>arr
["H", "a", "l", "l", "o", "Wo", "rld", "!"]
[/code]
BREAKDOWN:
arrName.append(value):
........arrName.........- the array to be changed.
........append(value)...- the function to be used. Only takes one
................argument though. I.e, you can not add two elements to
................the array.

arrName.insert(pos,value):
........arrName.........- the array to be changed.
........insert(pos,v...)- function adds an element at the indicated
................position.

arrName.extend([value1, value2...])
........arrName.........- the array to be changed.
........extend([val...])- function adds multiple elements onto the
................end of a list. The elements to be added must be
................inside square brackets. This function only takes
................one argument - i.e. a list ([]).

arrName.remove(value):
........arrName.........- the array to be changed.
........remove(value)...- removes the first instance of the value
................inputted.

As mentioned before, arrays can be indexed the same way as words. Python also allows you to pick a specific letter/range of letters out
words in an array...

>>>arr = ["Hello", "World", "!"]
>>>arr[0][3:] #Picks first word, letters between 3 to end
"lo"

Also, a useful function for both single strings and arrays:

>>>arr = ["Hello, "World", "!"]
>>>arr.index("Hello") #Displays pos of value in array
0
>>>word = "Hello!"
>>>word.index("H") #Displays pos of first occurrence in array
0

Arrays can also be changed by putting them into formulas, e.g.

CODE :
__________________________________________________________________________
>>>arr = ["Hello", "World", "!"]
>>>arr = arr + [":P"]
>>>arr
["Hello", "World", "!", ":P"]
>>>
>>>arr = [90, 91] * 2
>>>arr
[90, 91, 90, 91]
__________________________________________________________________________

In conclusion to this section, arrays are a very much needed tool of a
serious programmer...


+---------------------------------------+
VII. Loops and Conditionals
+---------------------------------------+
+---------------------------------------+
VII.a. The IF/ELSE/ELIF Statements
+---------------------------------------+
IFS, elifs and elses help us develop a sense of control to our
programs... Without these, programs would be, in one word, pretty useless... *(...?)*

In this section, we will also create a whole program, that will
eventually be able let the user of the program input a number, and have the program count down from their number to 0.
Here is a summary of each term, and how they work, IF, ELIF, and ELSE,
including the syntax.

CODE :
__________________________________________________________________________
#For this part of the program, we need to make sure that the input
#is no more than 9, and no less than 1
#----------------------------------------------------IF---------
>>>numArray = [1,2,3,4,5,6,7,8,9]
>>>#^ Sets up a number array,
>>>#so that we can check if the
>>>#input is between 1:9
>>>
>>>a = int(input("Enter a number 1-9: "))
>>> #This line of programming says that the program will
>>> #output "Enter a number 1-9", asking the user to
>>> #"input" an "integer" (whole number) between 1 and 9
>>>
Enter a number 1-9: #Input goes here
>>>if a in numArray: #Checks if input is in numArray
... print("Countdown initializing!")
__________________________________________________________________________

So far, the program lets the user input a number to the program and the program checks if that number is between 1 to 9, and if it is, the
program prints "Countdown initializing!". Unfortunately, if the number
is more than 9, or less than 1, nothing happens... That is BAD. So we
need to sort this out. Introducing ELIF...
CODE :
__________________________________________________________________________
#----------------------------------------------------ELIF------
>>>numArray = [1,2,3,4,5,6,7,8,9]
>>>#^ Sets up a number array,
>>>#so that we can check if the
>>>#input is between 1:9
>>>
>>>a = int(input("Enter a number 1-9: "))
>>> #This line of programming says that the program will
>>> #output "Enter a number 1-9", asking the user to
>>> #"input" an "integer" (whole number) between 1 and 9
>>>
Enter a number 1-9: #Input goes here
>>>if a in numArray: #Checks if input is in numArray
... print("Countdown initializing!")
...elif a > 9: #Checks if a is more than 9
... print("Your number is too high...")
__________________________________________________________________________

Ah, great! Now our program tells the user off for entering a number too high... But what if the input is less than 1? Well, seeing as we have covered 1-9, and anything above 9, we can now introduce "else"...
CODE :
__________________________________________________________________________
#----------------------------------------------------ELSE------
>>>numArray = [1,2,3,4,5,6,7,8,9]
>>>#Sets up a number array,
>>> #so that we can check if the
>>> #input is between 1:9
>>>
>>>a = int(input("Enter a number 1-9: "))
>>> #This line of programming says that the program will
>>> #output "Enter a number 1-9", asking the user to
>>> #"input" an "integer" (whole number) between 1 and 9
>>>
Enter a number 1-9: #Input goes here
>>>if a in numArray: #Checks if input is in numArray
... print("Countdown initializing!")
...elif a > 9: #Checks if a is more than 9
... print("Your number is too high...")
...else:
... print("Your number is a bit too low!")
__________________________________________________________________________

WONDERFUL! Our program now works. However, it is a bit plain, let us go on to WHILE.

+---------------------------------------+
VII.b. The WHILE Statement
+---------------------------------------+
SO, our program up to now is pretty good. We still need to make the
countdown though. And on top of that, we need the input to repeat, in
case the number that the user enters a number that is not between
1-9... This is where the WHILE statement comes into play. The WHILE
statement will repeat and repeat til the condition that makes it run
changes.

Here is the modified script, using the WHILE loop.
CODE :
__________________________________________________________________________
#----------------------------------------------------WHILE-----
>>>numArray = [1,2,3,4,5,6,7,8,9] #Sets up number array
>>>
>>>case = 0
>>>#^ This variable will allow us to switch from
>>>#one loop to the next... Read on
>>>
>>>while case == 0: ###Checks if case == 0 and then runs script
... a = int(input("Enter a number 1-9: "))
... if a in numArray: #checks if input is in numArray
... print("Countdown initializing...!")
... case += 1 #Tells case to increase by 1;
...
... else:
... pass
>>>#"pass" tells the program to do nothing
>>>#And go to the beginning of the loop again
>>>
>>>#The above loop will repeat til the user enters a valid number
>>>#then case will change to 1 and run the below script
>>>
>>>
>>>
>>>while case == 1: #Checks case == 1, i.e, it checks if the
>>> #number in the first loop is valid
... if a == 0:
... case +=1 #Passes the program to the next part
... else:
... print(a,"more loop(/s) til the bang!")
... a -= 1 #Tells the program to take 1 away from input
>>>
>>>#The above loop continues to repeat til a == 0, when it finally
>>>#carries you to the last part of the program...:
>>>
>>>if case == 2:
>>> print("BANG!")
__________________________________________________________________________

The above program can be broken down into four simple parts:
1. a preparation of the variables for the program to handle

2. loop one: this loop makes the user enter a number between
one to nine, and then goes to loop two, carrying across the
"a" integer input variable.

3. loop two: this loop counts down starting from "a", which
the user specified. When the loop turns variable "a" to 0, the
program changes case to 2, and sends it across to the final
part.

4. This part simply ends the program, printing 'BANG!' to the
screen.


+---------------------------------------+
VIII. Bye Bye! Good Luck!
+---------------------------------------+
SO, yeah, that's the very very very basics of Python. And I can only
hope that this all didn't sound like a complete load of donkey doodar
to you.

If this does not help you at all, many apologies. Here is a site that
might help you if you wanna take it slower:
http://docs.python.org/3.1/tutorial/ :)
Best of luck to all of you :)

BYE BYE!! <3
Code2004 // Connor

Post-script: please remember that ">>>" and "..." mean that this was written in Python Shell. DO NOT use them in a script, because it wont work xD!

Python - A Real Beginners Guide

PYTHON

I. Intro
II. My First Program
III. Variables, Numbers and Strings
IV. String Manipulation
V. Operators
VI. Arrays/Lists
VII. Loops and Conditionals
a. The IF/ELIF/ELSE Statements
b. The WHILE Statement
VIII Bye bye! Good luck!

+---------------------------------------+
I. Intro
+---------------------------------------+
PYTHON! According to Python.org, this is what Python is...:

"Python is a dynamic object-oriented programming language that can be
used for many kinds of software development. It offers strong support
for integration with other languages and tools, comes with extensive
standard libraries, and can be learned in a few days. Many Python
programmers report substantial productivity gains and feel the language encourages the development of higher quality, more maintainable code"

More simply however, Python is an easy-to-read, highly compatible,
oft-used programming language that is powerful and quick. It is often
compared to languages such as Perl, Ruby, Java, etc

ABOUT THIS GUIDE:
- This guide is intended for COMPLETE beginners to programming
languages, and is suggested to most as a first language, as it is as
mentioned before, an easy-to-read and simple language :)

REQUIREMENTS FOR THIS GUIDE:
- Having the latest version of Python installed on your computer. The
installers can be found at http://www.python.org/download/


+---------------------------------------+
II. My First Program
+---------------------------------------+
FOR the first program, we will be creating a small program that writes
"Hello World!" on screen.

Here is the code:
CODE :
__________________________________________________________________________
>>>print("Hello World!")
__________________________________________________________________________

BREAKDOWN:
print() - the typical function (functions will be covered more later
on) to write sentences and variables to the screen.

NOTE: It should also be noted that when using the print function, you
must remember that when you try print multiple things, e.g.
>>>print("Lol", "and", "hi"), a space will immediately be placed
between each part.

NOTE2: Instead of using print(), we also can type the string, number,
or variable and press enter in Python Shell to print the value of it
(remember that you will have to wrap strings in quotation marks if you
decide to use this method).


+---------------------------------------+
III. Variables, Numbers and Strings
+---------------------------------------+
VARIABLES are ways of holding information inside a word, to be able to
call the information back later in a program. The way to assign a value is..:
CODE :
__________________________________________________________________________
>>>#This is a comment line... Comment lines in Python are always
>>>#Preceded by a #
>>>#A variable can either hold a string (words) or a number
>>>varName = "variable"
>>>varName1 = 2009
__________________________________________________________________________

Variables can be changed later in the program. They can also hold a
formula or function. In addition, you can assign the same values to
several variables at once. For examples...

CODE :
__________________________________________________________________________
>>>#This variable holds a total of 25
>>>varFormula = 5*5
>>>
>>>#These variables both hold a value of 45
>>>varX = varY = 40+5
>>>
>>>#This variable holds a function that finds out the length of a
>>>#string or other value
>>>varLength = len("Hello World!")
>>>
>>>#The len() function does not work with numbers!
>>>#Using print(varLength) will output the length of "Hello World!"
>>>#which is 12.
>>>
>>>#Remember that when using the len() function, it counts every
>>>#character, including the space, and not just letters.
__________________________________________________________________________

+---------------------------------------+
IV. Word Indexing
+---------------------------------------+
BEING able to control strings is a vital part of programming. We
already know a couple of basic functions that allow us to manipulate or use strings, i.e. print() and len(). Another useful feature of Python is word indexing: being able to pick out certain letters in strings.

Here is an example of how to use word indexing:

CODE :
__________________________________________________________________________
>>>Hello = "Hello World!" #Establishes a variable...
>>>
>>>Hello[0] #Writes the first letter of the variable
"H"
>>>
>>>Hello[1:] #Writes all letters after the first letter
"ello World!"
>>>
>>>Hello[:5] #Writes all letters up to the sixth letter
"Hello"
>>>
>>>Hello[3:7] #Writes letters between the third and eighth letters
"lo W"
>>>
>>>Hello[3:-1] #Writes letters between position 3 and -1
"lo World"

Yes, strangely enough, the first letter is indexed as [0]... Here is a
little table to illustrate index positions.
CODE :

+---+---+---+---+
| A | B | C | D | = String
+---+---+---+---+
| 0 | 1 | 2 | 3 | = Positive indices
+---+---+---+---+
|-3 |-2 |-1 | ? | = Negative indices
+---+---+---+---+

As you will see, there is absolutely no way of selecting a whole string using negative numbers... Of course, there are other ways of doing that.



+---------------------------------------+
V. Operators
+---------------------------------------+
OPERATORS are VERY important in Python... And sound much more
complicated than they really are. Operators are simply mathematical
symbols that do stuff for programming languages. Here is the table of
operators and how they work:
CODE :

MATHEMATICAL OPERATORS - These produce a value
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|+..............|Addition.......|a + 5 = 10.............|
+---------------+---------------+-----------------------+
|-..............|Subtraction....|a - 5 = 0..............|
+---------------+---------------+-----------------------+
|*..............|Multiplication.|a * a = 25.............|
|**.............|Powers.........|a **3 = 25*25*25 = 125.|
+---------------+---------------+-----------------------+
|/..............|Division.......|a / a = 1..............|
|//.............|Rounds to floor|a // 0.3 = 16..........|
|%..............|Gives remainder|a % 2 = 1..............|
+---------------+---------------+-----------------------+

ASSIGNMENT OPERATORS - These give values to a variable
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|=..............|Assigns a value|a = 5..................|
+---------------+---------------+-----------------------+
|-=.............|Subtraction....|a-=10 is the same as...|
|...............|assigner.......|a = a - 10.............|
+---------------+---------------+-----------------------+
|*=.............|Multiplication.|a *= 10 is the same as.|
|...............|assigner.......|a = a * 10.............|
+---------------+---------------+-----------------------+
|**=............|Power assigner.|a **= 2 is the same as.|
|...............|...............|a = a ** 2.............|
+---------------+---------------+-----------------------+
|/=.............|Division.......|a /= 10................|
|...............|assigner.......|a = a / 10.............|
+---------------+---------------+-----------------------+
etc...etc...etc...etc...etc...etc...etc...etc...etc...etc

COMPARISON OPERATORS - These evaluate the truth of a statement
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|==.............|Is equal to....|a == 5.........TRUE....|
+---------------+---------------+-----------------------+
|!=.............|Not equal to...|a != 5.........FALSE...|
+---------------+---------------+-----------------------+
|>..............|More than......|a > a..........FALSE...|
|<..............|Less than......|a < 10.........TRUE....|
+---------------+---------------+-----------------------+
|>=.............|More than......|a >= a.........TRUE....|
|...............|or equal to....|a >= 6.........FALSE...|
+---------------+---------------+-----------------------+

BOOLEAN OPERATORS - These are used to link COMPARISON OPERATORS
...note: a = 5 for the examples...
+---------------+---------------+-----------------------+
|SYMBOL.........|FUNCTION.......|EXAMPLES ON FUNCTION...|
+---------------+---------------+-----------------------+
|and............|Both expression|a==5 and a>1...........|
|...............|must be true...|Evaluates to true......|
+---------------+---------------+-----------------------+
|or.............|Either expressi|a>6 or a<3.............|
|...............|on must be true|Evaluates to false.....|
+---------------+---------------+-----------------------+
|in.............|Checks if value|arr=["lol", "rofl"]....|
|...............|is in an array.|"lol" in arr...TRUE....|
+---------------+---------------+-----------------------+

These are the main operators that you will need when programming in
Python.


+---------------------------------------+
VI. Arrays/Lists
+---------------------------------------+
ARRAYS/lists can be indexed exactly like words, but can store multiple
strings, numbers and variables. Also, you can append arrays: changing
them as you see fit. Here is the correct way of starting an array and changing it.
[/code]
>>>#Starts an array, 5 "elements" long
>>>arr = ["H", "e", "l", "l", "o"]
>>>
>>>arr[0] #Writes the first element in the array
"H"
>>>
>>>#Here are 3 functions that can change lists
>>>#These will be explained at the end of this chapter
>>>
>>>arr.append("!")
>>>arr # <- Prints the array
["H", "e", "l", "l", "o", "!"]
>>>
>>>arr.insert(1, "a")
>>>arr
["H", "a", "l", "l", "o", "!"]
>>>
>>>arr.extend(["Wo", "rld", "!"])
>>>arr
["H", "a", "l", "l", "o", "!", "Wo", "rld", "!"]
>>>
>>>arr.remove("!")
>>>arr
["H", "a", "l", "l", "o", "Wo", "rld", "!"]
[/code]
BREAKDOWN:
arrName.append(value):
........arrName.........- the array to be changed.
........append(value)...- the function to be used. Only takes one
................argument though. I.e, you can not add two elements to
................the array.

arrName.insert(pos,value):
........arrName.........- the array to be changed.
........insert(pos,v...)- function adds an element at the indicated
................position.

arrName.extend([value1, value2...])
........arrName.........- the array to be changed.
........extend([val...])- function adds multiple elements onto the
................end of a list. The elements to be added must be
................inside square brackets. This function only takes
................one argument - i.e. a list ([]).

arrName.remove(value):
........arrName.........- the array to be changed.
........remove(value)...- removes the first instance of the value
................inputted.

As mentioned before, arrays can be indexed the same way as words. Python also allows you to pick a specific letter/range of letters out
words in an array...

>>>arr = ["Hello", "World", "!"]
>>>arr[0][3:] #Picks first word, letters between 3 to end
"lo"

Also, a useful function for both single strings and arrays:

>>>arr = ["Hello, "World", "!"]
>>>arr.index("Hello") #Displays pos of value in array
0
>>>word = "Hello!"
>>>word.index("H") #Displays pos of first occurrence in array
0

Arrays can also be changed by putting them into formulas, e.g.

CODE :

>>>arr = ["Hello", "World", "!"]
>>>arr = arr + [":P"]
>>>arr
["Hello", "World", "!", ":P"]
>>>
>>>arr = [90, 91] * 2
>>>arr
[90, 91, 90, 91]


In conclusion to this section, arrays are a very much needed tool of a
serious programmer...


+---------------------------------------+
VII. Loops and Conditionals
+---------------------------------------+
+---------------------------------------+
VII.a. The IF/ELSE/ELIF Statements
+---------------------------------------+
IFS, elifs and elses help us develop a sense of control to our
programs... Without these, programs would be, in one word, pretty useless... *(...?)*

In this section, we will also create a whole program, that will
eventually be able let the user of the program input a number, and have the program count down from their number to 0.
Here is a summary of each term, and how they work, IF, ELIF, and ELSE,
including the syntax.

CODE :

#For this part of the program, we need to make sure that the input
#is no more than 9, and no less than 1
#----------------------------------------------------IF---------
>>>numArray = [1,2,3,4,5,6,7,8,9]
>>>#^ Sets up a number array,
>>>#so that we can check if the
>>>#input is between 1:9
>>>
>>>a = int(input("Enter a number 1-9: "))
>>> #This line of programming says that the program will
>>> #output "Enter a number 1-9", asking the user to
>>> #"input" an "integer" (whole number) between 1 and 9
>>>
Enter a number 1-9: #Input goes here
>>>if a in numArray: #Checks if input is in numArray
... print("Countdown initializing!")

So far, the program lets the user input a number to the program and the program checks if that number is between 1 to 9, and if it is, the
program prints "Countdown initializing!". Unfortunately, if the number
is more than 9, or less than 1, nothing happens... That is BAD. So we
need to sort this out. Introducing ELIF...
CODE :

#----------------------------------------------------ELIF------
>>>numArray = [1,2,3,4,5,6,7,8,9]
>>>#^ Sets up a number array,
>>>#so that we can check if the
>>>#input is between 1:9
>>>
>>>a = int(input("Enter a number 1-9: "))
>>> #This line of programming says that the program will
>>> #output "Enter a number 1-9", asking the user to
>>> #"input" an "integer" (whole number) between 1 and 9
>>>
Enter a number 1-9: #Input goes here
>>>if a in numArray: #Checks if input is in numArray
... print("Countdown initializing!")
...elif a > 9: #Checks if a is more than 9
... print("Your number is too high...")

Ah, great! Now our program tells the user off for entering a number too high... But what if the input is less than 1? Well, seeing as we have covered 1-9, and anything above 9, we can now introduce "else"...
CODE :

#----------------------------------------------------ELSE------
>>>numArray = [1,2,3,4,5,6,7,8,9]
>>>#Sets up a number array,
>>> #so that we can check if the
>>> #input is between 1:9
>>>
>>>a = int(input("Enter a number 1-9: "))
>>> #This line of programming says that the program will
>>> #output "Enter a number 1-9", asking the user to
>>> #"input" an "integer" (whole number) between 1 and 9
>>>
Enter a number 1-9: #Input goes here
>>>if a in numArray: #Checks if input is in numArray
... print("Countdown initializing!")
...elif a > 9: #Checks if a is more than 9
... print("Your number is too high...")
...else:
... print("Your number is a bit too low!")

WONDERFUL! Our program now works. However, it is a bit plain, let us go on to WHILE.

+---------------------------------------+
VII.b. The WHILE Statement
+---------------------------------------+
SO, our program up to now is pretty good. We still need to make the
countdown though. And on top of that, we need the input to repeat, in
case the number that the user enters a number that is not between
1-9... This is where the WHILE statement comes into play. The WHILE
statement will repeat and repeat til the condition that makes it run
changes.

Here is the modified script, using the WHILE loop.
CODE :

#----------------------------------------------------WHILE-----
>>>numArray = [1,2,3,4,5,6,7,8,9] #Sets up number array
>>>
>>>case = 0
>>>#^ This variable will allow us to switch from
>>>#one loop to the next... Read on
>>>
>>>while case == 0: ###Checks if case == 0 and then runs script
... a = int(input("Enter a number 1-9: "))
... if a in numArray: #checks if input is in numArray
... print("Countdown initializing...!")
... case += 1 #Tells case to increase by 1;
...
... else:
... pass
>>>#"pass" tells the program to do nothing
>>>#And go to the beginning of the loop again
>>>
>>>#The above loop will repeat til the user enters a valid number
>>>#then case will change to 1 and run the below script
>>>
>>>
>>>
>>>while case == 1: #Checks case == 1, i.e, it checks if the
>>> #number in the first loop is valid
... if a == 0:
... case +=1 #Passes the program to the next part
... else:
... print(a,"more loop(/s) til the bang!")
... a -= 1 #Tells the program to take 1 away from input
>>>
>>>#The above loop continues to repeat til a == 0, when it finally
>>>#carries you to the last part of the program...:
>>>
>>>if case == 2:
>>> print("BANG!")

The above program can be broken down into four simple parts:
1. a preparation of the variables for the program to handle

2. loop one: this loop makes the user enter a number between
one to nine, and then goes to loop two, carrying across the
"a" integer input variable.

3. loop two: this loop counts down starting from "a", which
the user specified. When the loop turns variable "a" to 0, the
program changes case to 2, and sends it across to the final
part.

4. This part simply ends the program, printing 'BANG!' to the
screen.


+---------------------------------------+
VIII. Bye Bye! Good Luck!
+---------------------------------------+
SO, yeah, that's the very very very basics of Python. And I can only
hope that this all didn't sound like a complete load of donkey doodar
to you.

If this does not help you at all, many apologies. Here is a site that
might help you if you wanna take it slower:
http://docs.python.org/3.1/tutorial/ :)
Best of luck to all of you :)

BYE BYE!! <3
Code2004 // Connor

Post-script: please remember that ">>>" and "..." mean that this was written in Python Shell. DO NOT use them in a script, because it wont work xD!

Social Engineering

I am writing this article, because social engineering is almost a necessity for any hacker. You would be surprised what valuable information people will give away to a complete stranger. I have not seen any articles on this topic so far, so I am going to do my best and hopefully teach you all something you did not know before.

Introduction
Social Engineering is the art of manipulating a person into revealing sensitive information. Social Engineering is the best hacking tool you can use, in my opinion. Similar to using a computer program to make another system spew out amounts of valuable information about the machine, that an attacker can later use. Think of it as "people hacking". When hacking into system you find a weakness or vulnerability that you can exploit, to gain access to restricted information. Social engineering is taking advantage of a persons weakness and getting them to disclose confidential information. All it takes is a large amount a confidence and basic knowledge of human nature and social behavior patterns. Social engineering does not just apply to computer security, it can apply to nearly any situation.


Understanding Human Nature
When it comes to social engineering there are typically only a handful of “tools” you can use. Some of which are; A basic understanding of human nature, cognitive biases, and psychological fallacies. People generally have social patterns and behaviors that can easily be exploited. Everyone has these flaws, it is a matter of finding out what works with the particular person. There are literally hundreds of these fallacies, and nearly everyone is guilty of them. This is just a few that really stand out to me. Maybe I will cover more in a future article. Some of the most popular human social patterns include:

*The Bandwagon Effect-This is the tendency to follow patterns of another persons, or a groups behavior. Generally everyone has heard the term "jump on the bandwagon", It simply means to do as others do. This particular bias plays a very important roll in social engineering and can be taken advantage of quite easily. Also known as conformity.

*Illusion of Control-This is the illusion that a human believes that they can control the outcome of certain situation, when it is clearly out of their hands. Think of someone who is gambling who believes they can really control the outcome of the numbers they roll. Some people truly believe that they can control the outcome of an event as if to predict the future. Prayer or belief in the paranormal could also be thrown into this category.

*Stereotyping-Stereotyping is judging a person by their distinguished characteristics. Everyone is clearly guilty of this at some point. Every time you meet someone for the first time, you almost always inadvertently judge them. You judge them by their clothes, their hairstyle and just their general appearance. However, stereotyping can sometimes be accurate as I will explain later on in the article.

*The Ostrich Effect-This is act of ignoring the negative situation that is going on. Think of someone that is over-optimistic about financial issues and pretending everything is fine. This particular fallacy is performed by almost anyone in a negative situation.

*Consistency bias-This is known as incorrectly remembering your past thoughts or actions in a given situation. This can be greatly taken advantage of. A new employee may not know how to answer a question, or how they answered it in the past. Therefore possibly disclosing valuable information.


Basic Techniques

You are not going to want to use every technique at once, find one that fits a particular situation and play the part well. Most social engineering can be done over the phone. It is quite simple to call up a company while imitating a person of authority and retrieving sensitive information. Help desks and customer service are very likely to this method of attack.

Be Polite
The best thing you can do is always be polite, never blow your cover by acting rude. Remember, you are sometimes taking advantage of someones good nature. So getting on their bad side is not a good start. Remember to speak up and be firm, but do not be rude. For example, call up a company you are interested in, and politely ask questions. Act as if you truly want to learn about how their system works, or what tools they use. Do not blatantly ask for something that you know is restricted information. You have to keep talking to them, while sounding knowledgeable and interested. Ask to speak to a manager, or someone in charge. Working your way up to someone that knows it all. Write down the names of employees pretend you are interested in that particular field of work, ask what type of education and things you will need to learn. The goal here is to persuade them from a psychological point of view.

Pretend to be ignorant
You obviously do not want the target to know much about you, so you want to be as discrete as possible. You do not want them to become concerned with a question you may have asked. Playing dumb is also another technique that can be used. Pretend to know nothing whatsoever and create a fake problem to ask customer service about. Keep them on the phone long enough and keep asking questions. Give them a fake name and phony problem. Ask for their name and figure out where they stand in the company. You know how annoying it is when you call a company and they keep redirecting you to someone else. They have thousands of calls each day, chances are they will not remember you. In all honesty they probably could not care less, they just want to get rid you and have someone else help you.

Be Curious, without giving it away
Write down a list of things you want to figure out with a certain phone call. Whether it be a certain name, phone number or just a piece of information that helps put together a piece of the puzzle. Ask for names, and to speak to certain people. Make sure you do your homework first and have a general knowledge about the company. If you do not know what to say beforehand you will sound like a fumbling idiot and your confidence level will decrease.

Pretending to be someone of higher authority
This applies the the bandwagon effect and also false memory. Tell a client that is lower in the chain that you are someone who you are not. Tell them you are an employee (in this case it would be a good idea to have a list of employees that you found on the company website or through the yellow pages.) Ask to speak to so and so, who is higher up in the company than she is. Tell them you need a phone number, or whatever it may be you are searching for. That is why I think it is a good idea to have a goal of what you are truly after. This method is known as reverse social engineering. This requires a bit of research and preparation to pull off, but with proper execution and very well be one of the best methods.

Other Techniques
These techniques are aimed to physical access to a specific company. Be careful with these though, they could land you in some pretty tough situations that may be harder to talk your way out of. Just remember that social engineering can be applied to nearly any given situation.

Dumpster Diving
As silly as this may sound, dumpster diving as an effective way of gaining valuable information about a company. You would be surprised what kinds of things they may have thrown away. Perhaps a trashed company computer with the hard rive still in it. Or possibly company phone books, organizational charts, memos, company policy manuals, calendars of meetings, events and vacations, system manuals, printouts of sensitive data or login names and passwords, printouts of source code, disks and tapes, company letterhead and memo forms, and outdated hardware. I will not go into great detail of how to dumpster dive, but I am sure you get the picture. Bottom line is that valuable things can be found in a company dumpster.

Tailgating
The art of following an authorized person into an area where you are not authorized. This is where your acting skills can come in handy. Pretend to be the repair man they called last week. Come ready with all your tools, hardhat white t-shirt and jeans and play the part. When really you just want physical access to something a normal civilian would not have rights to access. This technique takes some serious dedication, but in the end very much worth the effort. This requires doing your best to blend in. Maybe pretending to be just another employee on a smoke break. They will eventually finish and go back inside. That would be your cue to follow them inside, thus giving you physical access. Whatever your doing play the part, and do it with confidence.

Shoulder Surfing
Seems easy enough, right? It is as simple as it sounds, peering over someones shoulder to see what they are typing. Be careful not to get caught with this one, by making it obvious you are trying to view what they are typing. I am sure all of you have exercised some form of this at one point. I do not think I need to go into great detail on this, just be smooth about things.


People Watching
This is by far my favorite method. Keep in mind that social engineering does not always involve tricking people. Like I said before, it is all about understanding human nature. For some odd reason, I enjoy watching people. Whenever I go to a mall, airport or somewhere where I can sit down in public, I love to watch people. (In a non-rapist/stalker sort of way) I like to nonchalantly eavesdrop and just hear about their lives and what they have to say. I know you have all done it, at one time or another you have listened in on someones conversation and heard something they probably did not want you to hear. Everyone judges other people by the way they look or talk. It is one of the cognitive biases I listed called Stereotyping. A great way to practice your social engineering skills is to sit down and judge people. Not in a rude way, but try to figure out their life based on their appearance and social patterns. Pick out someone and see think about what they are wearing, what they are talking about, how they carry themselves and try to imagine what kind of life they lead.


Conclusion
This is just the tip of the iceberg when it comes to social engineering. There is much more to cover, but I hope you all learned something. Overtime you will become better at reading and understanding human nature. You will develop your own style of social engineering. There are many more methods that I left out, but these are great to start with. Knowing how to social engineer is a great way to prevent yourself from getting tricked by others. For example, the police use social engineering and forms of manipulation constantly. Others may disagree, but overall I feel this is an important topic to cover and I enjoyed writing this article. This is my first article, so let me know what you thought and I will keep them coming.

MacOs X and UNIX | Basic Tutorial

Getting Started on MacOs

You can skip this if you know how to access your unix command-line
- Open: Applications -> Utilites (shortcut: cmd-shift-U)
- Run Terminal.app

Browsing

The standards for Unix filesystems are as follows
/ -The root directory / The directory divider symbol
~/ -Your home folder
./ -The current directory
../ -Up one directory
* -The wildcard symbol
Directory: For those who only ever use the Finder or Filesystem GUI, a directory is a folder... baisically. Really a directory is just a pointer to any memory address.

Commands

cd -Change to the given directory
ls -List the contents of the active directory
rm -Erase a memory link (delete a file)
srm -Securely erase a link, by writing all zeros (like rm but it actually wipes the memory instead of just removing the pointer.)
mkdir -Make a new directory in current location. (Like New Folder)
mv- Copy files from one place to another

Excercises and Examples
Try these out to see if you understand:
Remove the quotations when typing commands.

1.

-Type ls, this should list the contents of your home folder, which is of course a directory. From here on all folders will be referred to as directories. If you do not see your home directory type cd ~/.
-Type touch test.noway. The touch command updates the last modified date of a file or creates a new one if the given filename does not exist. I used the .foobar extension to make sure you didnt accidentally change a file you had which happened to be named test.
-Switch to the finder and go to your home folder. You should now see a file called test.foobar.
-You just created that file well move it in exercise 2

2.

-Type ls, this should list the contents of your home folder. If you do not see your home directory type cd ~/.
-Type mkdir foo a new folder will appear named foo.
-Press the up arrow key to recall mkdir foo
Type mkdir foo/bara new folder will appear in foo named bar.
You can use the arrow-keys to scroll through command history at any time.
-Type touch test.noway this will create an empty file if you didn not do the first lesson.
-Type mv test.noway foo/bar test.noway is now in ~/foo/bar/
-Type mv ~/foo/bar/test.nowway ~/Pictures test.noway is now in your pictures folder.
-Type cd ~/Pictures ls will show you that you are in your pictures folder.
-Type mv test.noway ../ test.noway should move up one directory into you home folder.
-Type pwd you should see the path of your active directory.
-Type cd ../ your active directory should move up one to ~/. You can use pwd to test this.
-Type srm test.noway this will securely remove test.noway. Your computer may make funny clicking noises, but that is normal.
-Type rm foo You will receive an error message. Unix cannot by default delete a directory if there is something in it. Either delete ~/foo/bar/ or type rm -R this is what is known as a flag, switch, or dip-switch. -R will cause rm to load the hierarchy for deletion by reading all the pointers.

Help

Unix is notoriously poorly documented on the internet. Mainly becuase everything you need to know can be accessed via man
man a command - Access help for command.
man -k or apropos a string (search text) - When you know what you need but do not know what it is called.
Use space and arrows to navigate man. Press q when you done.
The OS manual can be accessed via info bash assuming the top bar of the terminal window says bash, which is the default.

FAQ

1. What is * for?
This is the wildcard character. Use it when you have given the computer enough information to figure out what you would type.
e.g.

cd ~/Pi* = cd ~/Pictures
rm ~/Pi*/* = rm ~/Pictures/ everthing in pictures.
rm ~/Pi*/a* = rm ~/Pictures/ everthing in pictures beginning with the letter a
rm ~/Pi*/a*b = rm ~/Pictures/ everthing in pictures beginning with the letter a and ending in b
rm ~/Pi*/*.jpeg = rm ~/Pictures/ every .jpeg in pictures

2. What is ./ for?
Some programs need you to use this to differentiate between other commands and the file youre modifying.

3. Error: You do not have sufficient privileges, access denied ?
Use man to lookup chmod and chgroup
chmod 777 file - your file can be accessed by everyone
chmod 755 file - your file can be acessed by you

4. I need to be an admin, but I am not under this account?
Use sudo at the begining of your command. Type admin password when prompted
Use login to change users in your terminal, but not MacOs.

5. What do I use if the man page says to use a | character?
The | character is referred to as the pipe character. It is used to pass parameters to the input of the command you are using.

Web Interaction Using Python

Introduction


In a number of the HTS programming missions you are asked to interact with the site from a program that you have written, as opposed to using a webbrowser. There are plenty of other applications for web interaction, however. I have written a few python scripts to download various data from websites (e.g. http://python.pastebin.com/f268e6319 )

I will cover two ways of getting data from a website (and in fact, sending data too). If there are any problems with the article, leave a comment.

All examples have been written in Python 2.6. There are quite a few differences between 2.6 and 3.0, but the only ones that should apply in the code snippets in this article involve the print function.

In Python 2.6 a simple hello world is this:
CODE :
__________________________________________________________________________
print "Hello World"
__________________________________________________________________________

In Python 3.0 it looks like this:
CODE :
__________________________________________________________________________
print("Hello World")
__________________________________________________________________________

It's a good idea, and I will switch to 3.0 when it is finally worn in, but for the moment I'm sticking with 2.6.
If there are problems with any of the code running as 3.0, try using the 2to3 script (It came preinstalled with Xubuntu for me.. not sure about on windows etc).

Anyway, now that's all covered, on with the article.

The Url Libraries

First of all we will start with a tutorial on the URL libraries. These are urllib and urllib2.

Let's immediately get started with some code.
CODE :
__________________________________________________________________________
import urllib2
url = "http://example.com"
website = urllib2.urlopen(url)
print website.read()
__________________________________________________________________________

Pretty simple code really, and for a lot of things it's all you need to know. It fetches the website "http://example.com" and stores the data as an instance on which we use the read() function to return the data retrieved from the site. Here are the functions:
instance.read() This returns the data retrieved from the site.
instance.info() This returns the HTTP message from the server, it has a lot of useful information in it including cookie info and server type.
instance.geturl() Returns the URL that was requested - seems pointless but we'll cover it in a second and you'll see why there is a point.
instance.getcode() Returns the HTTP status code. (e.g. 404, 200)

It's worth messing around with those a bit, rather than just taking my word for what they do.
I'll now just show a use of the geturl() function:
CODE :
__________________________________________________________________________
import urllib2
url = "http://google.com" # After google, try 'http://example.com'
website = urllib2.urlopen(url)
if url == website.geturl():
print "Website not redirected."
else:
print "Website redirected you."
__________________________________________________________________________

Why you'd want to do that, I don't know, but there's bound to be a use for it sometime. But that is one application of the geturl() function anyway.

Let's do a HTTP POST request now. They're pretty easy really, but can look a little complicated, so don't worry.
Before you look at the code, you might want to set up a server (or get some webspace) so you can test this out. A little PHP script like below will do the trick:
CODE :
__________________________________________________________________________
echo $_POST['test'];
?>
__________________________________________________________________________

And before anyone says anything about XSS - get lost - it's a testpage that will be up for 10 minutes on a server that noone cares about. But if you really are that bothered, you can use strip_tags() around that. (I say this because I can tell there'll be someone who will try and pipe up a clever comment).

Now then, we'll be introducing a new module for this (though it isn't strictly necessary, it's the best way I reckon). I will import the single function as we don't need any other functions from the module.

Okay, let's go:
CODE :
__________________________________________________________________________
import urllib2
from urllib import urlencode # new module and function

url = "http://localhost/test.php"
data = {'test':'lolwut'}
# you can add as much info as you want to this dictionary
# "test" is the label for the data, so that PHP script above
# should display "lolwut".

encoded_data = urlencode(data)
# remember that this is from that imported module, normally you'd
# use this: urllib.urlencode(data) if you used a normal import.

website = urllib2.urlopen(url, encoded_data)
print website.read() # That was pretty easy, right?
__________________________________________________________________________

Pretty straightforward, right?
Let's go onto HTTP Basic Authentication. This is more tricky. Here's the skeleton code for opening more advanced things, including HTTP authentication.
CODE :
__________________________________________________________________________
import urllib2

url = "http://example.com"

openerDirective1 = ...
openerDirective2 = ...

opener = urllib2.build_opener(openerDirective1, openerDirective2)

urllib2.install_opener(opener)

website = urllib2.urlopen(url)
__________________________________________________________________________

Okay, that's a lot more complicated. Note the "openerDirective"s. They are basically a way of adding headers to the urlopen requests.
You can have numerous opener directives, or just the one. You build them into an opener using the build_opener() function then install it, using install_opener(). After that, you can request a site and it will include the headers that you have specified.

Let's look at creating a HTTP Basic Authentication header.

CODE :
__________________________________________________________________________
authDirective = urllib2.HTTPBasicAuthHandler()
realm = "Webmail"
url = "http://example.com/webmail/"
username = "leethaxxer"
password = "letmein"
authDirective.add_password(realm, url, username, password)
__________________________________________________________________________

Then, we just build the opener and install it like we did in the skeleton code. Here:
CODE :
__________________________________________________________________________
opener = urllib2.build_opener(authDirective)
urllib2.install_opener(opener)
__________________________________________________________________________

I plan to write another article soon about cookies in Python, both as part of CGI and as part of requests with Urllib2.
Now I will move onto sockets and raw HTTP requests, and include cookies in that.

Socket Programming in Python

Socket programming is a really useful thing to learn - it's a must really, especially if you want to learn about security.

Again, we'll get some code out there straight away:
CODE :
__________________________________________________________________________
import socket
s = socket.socket()

host = "www.example.com"
port = 80
addr = (host, port)

s.connect(addr)
s.send("Something to send..")
print s.recv(1024)
# 1024 is the buffer size, you don't need to worry about it
# much right now.

s.close()
__________________________________________________________________________

There we are. We've created a socket, connected to "www.example.com" on port 80 then sent "Something to send.." and received something back, which has been printed out. Then we closed the socket, which isn't strictly necessary - but good practice.

Here's some better stuff to send, however:
CODE :
__________________________________________________________________________
GET /index.html HTTP/1.1\r\n
Host: www.example.com\r\n
__________________________________________________________________________

That's a simple HTTP GET request, asking for "index.html".
Here's a post request:
CODE :
__________________________________________________________________________
POST /index.php HTTP/1.1\r\n
Host: www.example.com\r\n
Content-Length: 11\r\n
\r\n
hello=world\r\n
__________________________________________________________________________

Now let's add a cookie to a HTTP GET:
CODE :
__________________________________________________________________________
GET /index.html HTTP/1.1\r\n
Host: www.example.com\r\n
Set-Cookie: hello=world\r\n
__________________________________________________________________________

There are other socket modes that can be set, this article is a very basic introduction. I would recommend reading this article if you want to learn more: http://www.amk.ca/python/howto/sockets/

Conclusion

Hopefully this article will help you begin to interact with the Internet using Python. It's just the beginning and I will work on follow-up articles. Good luck and thanks for reading.
dotty.

Web Interaction Using Python

Introduction


In a number of the HTS programming missions you are asked to interact with the site from a program that you have written, as opposed to using a webbrowser. There are plenty of other applications for web interaction, however. I have written a few python scripts to download various data from websites (e.g. http://python.pastebin.com/f268e6319 )

I will cover two ways of getting data from a website (and in fact, sending data too). If there are any problems with the article, leave a comment.

All examples have been written in Python 2.6. There are quite a few differences between 2.6 and 3.0, but the only ones that should apply in the code snippets in this article involve the print function.

In Python 2.6 a simple hello world is this:
CODE :
__________________________________________________________________________
print "Hello World"
__________________________________________________________________________

In Python 3.0 it looks like this:
CODE :
__________________________________________________________________________
print("Hello World")
__________________________________________________________________________

It's a good idea, and I will switch to 3.0 when it is finally worn in, but for the moment I'm sticking with 2.6.
If there are problems with any of the code running as 3.0, try using the 2to3 script (It came preinstalled with Xubuntu for me.. not sure about on windows etc).

Anyway, now that's all covered, on with the article.

The Url Libraries

First of all we will start with a tutorial on the URL libraries. These are urllib and urllib2.

Let's immediately get started with some code.
CODE :
__________________________________________________________________________
import urllib2
url = "http://example.com"
website = urllib2.urlopen(url)
print website.read()
__________________________________________________________________________

Pretty simple code really, and for a lot of things it's all you need to know. It fetches the website "http://example.com" and stores the data as an instance on which we use the read() function to return the data retrieved from the site. Here are the functions:
instance.read() This returns the data retrieved from the site.
instance.info() This returns the HTTP message from the server, it has a lot of useful information in it including cookie info and server type.
instance.geturl() Returns the URL that was requested - seems pointless but we'll cover it in a second and you'll see why there is a point.
instance.getcode() Returns the HTTP status code. (e.g. 404, 200)

It's worth messing around with those a bit, rather than just taking my word for what they do.
I'll now just show a use of the geturl() function:
CODE :
__________________________________________________________________________
import urllib2
url = "http://google.com" # After google, try 'http://example.com'
website = urllib2.urlopen(url)
if url == website.geturl():
print "Website not redirected."
else:
print "Website redirected you."
__________________________________________________________________________

Why you'd want to do that, I don't know, but there's bound to be a use for it sometime. But that is one application of the geturl() function anyway.

Let's do a HTTP POST request now. They're pretty easy really, but can look a little complicated, so don't worry.
Before you look at the code, you might want to set up a server (or get some webspace) so you can test this out. A little PHP script like below will do the trick:
CODE :
__________________________________________________________________________
echo $_POST['test'];
?>
__________________________________________________________________________

And before anyone says anything about XSS - get lost - it's a testpage that will be up for 10 minutes on a server that noone cares about. But if you really are that bothered, you can use strip_tags() around that. (I say this because I can tell there'll be someone who will try and pipe up a clever comment).

Now then, we'll be introducing a new module for this (though it isn't strictly necessary, it's the best way I reckon). I will import the single function as we don't need any other functions from the module.

Okay, let's go:
CODE :
__________________________________________________________________________
import urllib2
from urllib import urlencode # new module and function

url = "http://localhost/test.php"
data = {'test':'lolwut'}
# you can add as much info as you want to this dictionary
# "test" is the label for the data, so that PHP script above
# should display "lolwut".

encoded_data = urlencode(data)
# remember that this is from that imported module, normally you'd
# use this: urllib.urlencode(data) if you used a normal import.

website = urllib2.urlopen(url, encoded_data)
print website.read() # That was pretty easy, right?
__________________________________________________________________________

Pretty straightforward, right?
Let's go onto HTTP Basic Authentication. This is more tricky. Here's the skeleton code for opening more advanced things, including HTTP authentication.
CODE :
__________________________________________________________________________
import urllib2

url = "http://example.com"

openerDirective1 = ...
openerDirective2 = ...

opener = urllib2.build_opener(openerDirective1, openerDirective2)

urllib2.install_opener(opener)

website = urllib2.urlopen(url)
__________________________________________________________________________

Okay, that's a lot more complicated. Note the "openerDirective"s. They are basically a way of adding headers to the urlopen requests.
You can have numerous opener directives, or just the one. You build them into an opener using the build_opener() function then install it, using install_opener(). After that, you can request a site and it will include the headers that you have specified.

Let's look at creating a HTTP Basic Authentication header.

CODE :
__________________________________________________________________________
authDirective = urllib2.HTTPBasicAuthHandler()
realm = "Webmail"
url = "http://example.com/webmail/"
username = "leethaxxer"
password = "letmein"
authDirective.add_password(realm, url, username, password)
__________________________________________________________________________

Then, we just build the opener and install it like we did in the skeleton code. Here:
CODE :
__________________________________________________________________________
opener = urllib2.build_opener(authDirective)
urllib2.install_opener(opener)
__________________________________________________________________________

I plan to write another article soon about cookies in Python, both as part of CGI and as part of requests with Urllib2.
Now I will move onto sockets and raw HTTP requests, and include cookies in that.

Socket Programming in Python

Socket programming is a really useful thing to learn - it's a must really, especially if you want to learn about security.

Again, we'll get some code out there straight away:
CODE :
__________________________________________________________________________
import socket
s = socket.socket()

host = "www.example.com"
port = 80
addr = (host, port)

s.connect(addr)
s.send("Something to send..")
print s.recv(1024)
# 1024 is the buffer size, you don't need to worry about it
# much right now.

s.close()
__________________________________________________________________________

There we are. We've created a socket, connected to "www.example.com" on port 80 then sent "Something to send.." and received something back, which has been printed out. Then we closed the socket, which isn't strictly necessary - but good practice.

Here's some better stuff to send, however:
CODE :

GET /index.html HTTP/1.1\r\n
Host: www.example.com\r\n


That's a simple HTTP GET request, asking for "index.html".
Here's a post request:
CODE :

POST /index.php HTTP/1.1\r\n
Host: www.example.com\r\n
Content-Length: 11\r\n
\r\n
hello=world\r\n


Now let's add a cookie to a HTTP GET:
CODE :

GET /index.html HTTP/1.1\r\n
Host: www.example.com\r\n
Set-Cookie: hello=world\r\n


There are other socket modes that can be set, this article is a very basic introduction. I would recommend reading this article if you want to learn more: http://www.amk.ca/python/howto/sockets/

Conclusion

Hopefully this article will help you begin to interact with the Internet using Python. It's just the beginning and I will work on follow-up articles. Good luck and thanks for reading.
dotty.

Realistic 14 - Valid Key

1. Introduction

This is my first tutorial.
This tutorial isnt meant to be for whole mission.
Just part of it.

2. Getting the source code

Its assumed you got m*******.***s code and understand perls
code or at least syntax which is very similar to c or php.

3. Searching for exploits in code

Ive extracted particularly validkey function because rest of code
is doing pretty much nothing, if you cant have other functions.
You should have noticed that by now.

4. Understanding the code

If first argument($_[0]) isnt in range of alphanumerical
chars it will return 0 value or outside the function:
"You have entered an invalid id."
As you can see it uses regexp.
Splits every character in first argument($_[0]) and total,counter = 0
While idchars array is defined (true),
total of ascii code from given character plus total itself * counter
will give $total and counter goes up by 1.
If total is in range from 925559-927901 then we have valid id
anything else outside the range will return 0 (invalid key).

5. Coding

There are to ways two do this (in general):
Inputting different keys until we get right value which is range or
brute-force function until we get list of valid keys.

Pseudo code:
CODE :
__________________________________________________________________________
split keys
for i = 0 to len(keys)
do
key = keys[i]
validkey+= (ord(key[i]) + (total*i))
echo validkey
if valid key in range 925559-927901
then echo "valid key"
__________________________________________________________________________

6. Links

Regular Expressions
http://www.php.net/
http://www.python.org/download/
http://www.perl.com/download.csp
http://en.wikipedia.org/wiki/Regular_expression

PM or Email me with your codes or questions.
Thats pretty much all.

Realistic 14 - Valid Key

1. Introduction

This is my first tutorial.
This tutorial isnt meant to be for whole mission.
Just part of it.

2. Getting the source code

Its assumed you got m*******.***s code and understand perls
code or at least syntax which is very similar to c or php.

3. Searching for exploits in code

Ive extracted particularly validkey function because rest of code
is doing pretty much nothing, if you cant have other functions.
You should have noticed that by now.

4. Understanding the code

If first argument($_[0]) isnt in range of alphanumerical
chars it will return 0 value or outside the function:
"You have entered an invalid id."
As you can see it uses regexp.
Splits every character in first argument($_[0]) and total,counter = 0
While idchars array is defined (true),
total of ascii code from given character plus total itself * counter
will give $total and counter goes up by 1.
If total is in range from 925559-927901 then we have valid id
anything else outside the range will return 0 (invalid key).

5. Coding

There are to ways two do this (in general):
Inputting different keys until we get right value which is range or
brute-force function until we get list of valid keys.

Pseudo code:
CODE :
__________________________________________________________________________
split keys
for i = 0 to len(keys)
do
key = keys[i]
validkey+= (ord(key[i]) + (total*i))
echo validkey
if valid key in range 925559-927901
then echo "valid key"
__________________________________________________________________________

6. Links

Regular Expressions
http://www.php.net/
http://www.python.org/download/
http://www.perl.com/download.csp
http://en.wikipedia.org/wiki/Regular_expression

PM or Email me with your codes or questions.
Thats pretty much all.

Writing Your Own Python Modules

Extending the Python Language with your own Modules


One of the best bits about Python is the extensive catalogue of modules that are around, and free, to extend the power and functionality of the language. Sometimes, however, it is necessary to create your own - perhaps because a particular feature isn't available in the standard build, perhaps a new technology has arrived that you wish to write an API for, or perhaps you are writing a large application where the breaking up of code is necessary for it to remain flexible and unbloated. Whatever your reasoning, this article serves as a basic introduction to module creation in Python. It covers both procedural and Object Orientated programming, but will not go into detailed specifics of either. However, it will give enough information for you to be able to begin developing your own "add-ons" to the Python language.

I'm using Python 2.6 so there will be slight differences if you are using another version.

Brief Overview of Modules in Python


Modules in Python are imported using the "import" statement.
CODE :
__________________________________________________________________________
import re
__________________________________________________________________________
That, for example, imports "re" - the regex module - into the document. The functions and classes belonging to "re" can be accessed like so:
CODE :
__________________________________________________________________________
import re
re.function(....)
__________________________________________________________________________

Modules on my system (Linux Xubuntu) are stored in /usr/lib/python2.6/
It'll be different on Windows - obviously - and may be on Mac and other Linux distros. You'll have to do some searching. On a *nix system, using this command should help you locate them:
CODE :
__________________________________________________________________________
locate urllib2
__________________________________________________________________________

However you don't have to stick all your modules in there, if the python interpretter can't find your module there, it'll look in the current working directory.
Modules have to end with .py too.

Your First Module


First I'm going to create two files: "firstmod.py" - which will contain the actual module - and "test.py" - with which we will test it (this could also be replaced by the Python console).

CODE :
__________________________________________________________________________
# firstmod.py

def doSomething(a,b):
c = a + b + 5
return c
__________________________________________________________________________

CODE :
__________________________________________________________________________
# test.py
import firstmod
print firstmod.doSomething(5,6)
__________________________________________________________________________

When we execute test.py, we get 16.
The code is pretty self explanatory. Now I'm going to do something a little different.
CODE :
__________________________________________________________________________
#test.py
from firstmod import doSomething
print doSomething(4,9)
__________________________________________________________________________

This time, instead of importing the entire module (which, admittedly, in this case is still only the one function), we are just importing that doSomething function. However, there's another difference. We're not accessing that function through the module itself but instead as we would any other function that we have created. That can be useful if you only want to access the one function, and don't want to have to type out a long module name each time you want to use it.
Another way of using the "from ... import ..." statement is with the wildcard '*'. Now you should know that that means everything, and that's exactly what it does. Import everything. In most cases you shouldn't do that, but there are cases when it is useful - especially when doing GUI programming, should you want to.
CODE :
__________________________________________________________________________
#test.py
from firstmod import *
print doSomething(2,6)
__________________________________________________________________________
See? Pretty simple.

[h3]Initialisation[/h3]
You may, for some reason, want to execute a few lines of code as soon as your module is imported. What for, I don't know, but perhaps there's an application. To do this, well.. just write that code in there.
CODE :
__________________________________________________________________________
# firstmod.py
def doSomething(a,b):
c = a + b + 5
return c

print "Initialised."
a = 5
__________________________________________________________________________

Okay, so I've made it print out "Initialised" and have set the variable "a" to 5. Now what does this look like when I import it?
I'll show you the output from the Python console.
CODE :
__________________________________________________________________________
>>> import firstmod
Initialised.
>>> print firstmod.a
5
>>> print a
Traceback (most recent call last):
File "", line 1, in
NameError: name 'a' is not defined
__________________________________________________________________________

That's pretty important to note. When you set a global variable in a module as I did above, you can access that variable as you would a function.
As expected, it works the same as functions when specifically imported:
CODE :
__________________________________________________________________________
>>> from firstmod import *
Initialised.
>>> print a
5
__________________________________________________________________________

However, here's a little quirk:
CODE :
__________________________________________________________________________
>>> from firstmod import doSomething
Initialised.
>>> print a
Traceback (most recent call last):
File "", line 1, in
NameError: name 'a' is not defined
>>> print firstmod.a
Traceback (most recent call last):
File "", line 1, in
NameError: name 'firstmod' is not defined
__________________________________________________________________________

Hopefully those output snippets will help you understand how things work, I think that they explain better than I could, anyway.

[h3]Documentation[/h3]
Presumably at some point in your Python programming lives you will have used the help function. What you may not know is that the documentation for each module is in fact built in to the module.
Here's some code that will explain how it works.
CODE :
__________________________________________________________________________
# firstmod.py
"""This is an introduction to the firstmod module. It tells the user how to use the module as a whole, may give code snippets and generally give an idea of what the module does and how it does it. This can, and nearly always does, run over multiple lines."""
def doSomething(a,b):
"""This explains what this particular module does, so for example, in this circumstance I might say:
doSomething returns the sum of the two arguments with 5 added on."""
c = a + b + 5
return c
__________________________________________________________________________

Now try importing your module in the Python console, and typing "help(firstmod)".

[h3]Object Orientated[/h3]
Object Orientated programming is a tricky subject and can take a while to grasp completely. It's something that I like, but am wary of, and would advise anyone to read a lot about it before just jumping in.
I think the best way to teach this bit would be to actually create a working module - so that's what we'll do. We're going to write a 'toggle' variable: a variable with two specific positions and a function that switches between them. The best example is "True" or "False". Yep, they already have the boolean variable. Yep, you probably will never use this (though I've actually used it occasionally). Yep, it's pretty simple and a good learning script. So let's get started.

You can look at the full, finished code here: http://pastebin.com/m64220b4b
We won't be writing that much but feel free to add to the module if you want.

First of all I'm going to create a single file, "toggle.py". I will use the Python console for importing the module, etc.
Inside that, we'll have the class "toggle". Let's write some code.
CODE :
__________________________________________________________________________
# toggle.py
"""Toggle variable module"""
class toggle(object):
"""Toggle object with 2 possible values"""
def __init__(self, first, second):
"""This is executed as soon as the class is loaded"""
self.statements = first,second
self.position = 0
__________________________________________________________________________

Right, I'm going to stop there and explain a little. From now on, however, we'll be adding code to that class (not function), unless I state otherwise.
If we were to import this code and use it, we'd do it like this:
CODE :
__________________________________________________________________________
>>> import toggle
>>> t = toggle.toggle("option1","option2")
__________________________________________________________________________

The self in self.statements and self.position refers to the class itself. I won't explain it in detail because that'd involve basically explaining OOP, which would take another article. The self.statements variable is a tuple. It is assigned the variables passed to the __init__ function.

Okay, let's give this some functionality - we're going to make the object return a value when printed. Try this:
CODE :
__________________________________________________________________________
import toggle
t = toggle.toggle()
print t
__________________________________________________________________________

Now wouldn't it be nicer if it returned the current value then, rather than some weird value? Python is great in the way it lets you manipulate the language like that, here's how we'd do it:
CODE :
__________________________________________________________________________
# remember, we're adding a new function to the class here
def __str__(self):
"""Returns the current value"""
return str(self.statements[self.position])
__________________________________________________________________________

Now try the previous code and see what you get. Great, eh?
Currently though, you can't change the value. I'm going to introduce a toggle() function now.

CODE :
__________________________________________________________________________
def toggle(self):
"""Switch value, returns new value"""
if self.position == 0:
self.position = 1
else:
self.position = 0
return self.statements[self.position]
__________________________________________________________________________

Pretty self explanatory really. Let's try using it now:
CODE :
__________________________________________________________________________
>>> import toggle
>>> t = toggle.toggle("option1","option2")
>>> print t
option1
>>> t.toggle()
'option2'
>>> print t
option2
__________________________________________________________________________

[h3]Conclusion[/h3]
There we have it. That's your first basic module. There's a lot more you can do too and I encourage you to read up more on the subject. Writing modules is great fun, especially if you know Perl or another scripting language, and can port some modules over.
If you want to try out your skills writing modules, why not consider writing an IRC bot module? Or a noughts and crosses game, perhaps? There are plenty of applications.

Here's an old module I wrote for writing IRC bots. It isn't complete and it is probably pretty poorly coded in parts - I haven't looked at it for a while now. I'm not saying that it's perfect at all, but why don't you fix some problems it has? It definitely needs some regexs in there - message parsing has been done by hand :/
http://pastebin.com/m4e121ebf

Good luck, thanks for reading and have fun!
If there are any errors in the article, please leave a comment (or PM me if the comments system is down). Feel free to leave your opinions. :)
~thedotmaster

An Introduction to CSRF Vulnerabilities

What is a CSRF Attack?

A CSRF attack is a form of attack in which commands are transmitted from a victim to another website without the users consent. CSRF attacks are usually invisible and rely on browser functionality (such as automatically loading images).
How is a CSRF Attack Carried Out?

CSRF attacks are embedded in an element browsers automatically react to (such as an image tag).

Lets assume that Joe has just registered with a brand new Electronic Money transfer site. When Joe wants to transfer money, he heads to www.example.com/transfer.php to send some money to his wife, Mary.

He fills out the forms for the amount of money he wants to transfer and who he wants to send the money to, and then clicks submit. The URL now looks like this:
CODE :
__________________________________________________________________________
//www.example.com/transfer.php?from=Joe&to=Mary&amount=2
__________________________________________________________________________

The next day, Janice, Joes angry ex-wife, also registers with the site. She decides to send $5 to her boyfriend, named Sam. She heads to www.example.com/transfer.php, fills out the forms (quite angrily), and hits submit.

She notices that the URL,
CODE :
__________________________________________________________________________
//www.example.com/transfer.php?from=Janice&to=Sam&amount=5
__________________________________________________________________________

does not require any authorization besides her Session cookie (which is automatically send to the web server).

Still bitter over what she thinks was an unfair settlement to her divorce with Joe, she slips on her black hat and decides to cheat Joe out of his money.

She sends a message to Joe which looks like this:

CODE :
__________________________________________________________________________
//Title: I Hate You!!!!

You are the most rotten, vile, PIG ever!!!!!
[img]www.example.com/transfer.php?from=Joe&to=Janice&amount=5000[/img]
__________________________________________________________________________

(Note that Janice used BBCode for her image tag, which example.com automatically translates into HTML.)

Later that day, Joe logs on and views the message. His browser sees the image tag and automatically follows it to
CODE :
__________________________________________________________________________
//www.example.com/transfer.php?from=Joe&to=Janice&amount=5000
__________________________________________________________________________

His browser then attempts to download the page and display it as an image. Because the link provided is not a valid image, the browser displays it as a broken image.

However, the server at example.com sees that Joe has visited the link, and transfers $5000 dollars to Janice.

All this happens invisibly and within seconds.
Dangers of CSRF

We now understand how a CSRF attack is performed. But, what can this attack do?

It can:

*Transfer Funds
*Log a user in/out
*Register a user
*Log data of a user
*Send a message from the victim to someone else
*Etc

The only requirement is that the server must not have a means of detecting if the user has authorized the action that a CSRF attack performs.
Prevention of CSRF Attacks

CSRF attacks can be prevented in a number of ways, but the best way to prevent them is a combination of many different methods.

CSRF attacks are much easier if an action can be performed with an HTTP GET request, but simply having only POST requests is not sufficient to prevent CSRF attacks.

One common method of preventing CSRF attacks is to have a hidden value randomly generated upon the users visit of the webpage. The advantage of this strategy is that the attacker can not predict what the token will be, and thus cannot conduct the attack. This would look something like
CODE :
__________________________________________________________________________
//


__________________________________________________________________________

Randomizing the name of the CSRF token provides additional security.

Another method of prevention is double-submitting cookies. This is when you send the session ID in two ways: once in the cookie and once in a hidden value.

The session ID must be generated randomly and be required for the action to be performed.

Other general protection methods include:

*Not keeping users logged in for a long time
*Client-side, a browser should only follow image tags that end in image types such as jpeg, gif, png, etc.
Conclusion

In conclusion, CSRF attacks are based on a client's trust in a browser. They are performed when a user's browser sends an HTTP request to a site that causes a 'side effect' without the user knowing.

Please note that while CSRF attacks commonly are included in image tags, they are not limited to one html element. There are multiple ways to exploit a CSRF vulnerability.

+++

Share |

"make something then You never be lost"

wibiya widget