جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز...

29
Python for ethical hackers Mohammad reza Kamalifard [email protected]

Upload: mohammad-reza-kamalifard

Post on 10-May-2015

444 views

Category:

Education


9 download

DESCRIPTION

در این جلسه از کلاس به معرفی ساختار های داده ای در زبان پایتون و معرفی رشته ها و اعداد می‌پردازیم PySec101 Fall 2013 J2E1 By Mohammad Reza Kamalifard Talk About Python Data Structures, Strings, Numbers,...

TRANSCRIPT

Page 1: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Python for ethical hackersMohammad reza [email protected]

Page 2: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Python language essentialsModule 1:Introduction to Python and Setting up an Environment for ProgramingPart 2 :Variables and Data Types

Page 3: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Variables, Objects and References• There are integers, floating point numbers, strings, and

many more, but things are not the same as in C or C++.• Python variables do not have any types associate with

them• Don’t need declaration before use• Variable name is a reference to an Object

Page 4: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Variables• Something which can change• Way of referring to a memory location used by a computer

program. Variable is a symbolic name for this physical location

• Memory location contains values, like numbers, text or more complicated types

Page 5: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Variables, Objects and References

>>> name = 'reza'

>>> print name

reza

>>>

>>>name = 10

>>>name

10

Identifier Object

Page 6: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Data Type>>> name = 10

>>> type(name)

<type 'int'>

>>> name = 'Reza'

>>> type(name)

<type 'str'>

>>> name = 10.42

>>> type(name)

<type 'float'>

>>>

Page 7: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Variables vs. Identifiers● Variables and identifiers are very often mistaken as

synonyms● The name of a variable is an identifier, but a variable is

"more than a name". ● An identifier is not only used for variables. An identifier can

denote various entities like variables, types, labels, subroutines or functions, packages and so on.

Page 8: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Naming Identifiers of VariablesA valid identifier is a non-empty sequence of characters of any length with:● the start character can be the underscore "_" or a capital or

lower case letter● the letters following the start character can be anything

which is permitted as a start character plus the digits.● Just a warning for Windows-spoilt users: Identifiers are

case-sensitive● Python keywords are not allowed as identifier names

and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is, lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield

Page 9: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Variables vs. Identifiers>>> name = 'reza'

>>> id(name)

3072831328L

>>> hex(id(name))

'0xb727af60L'

>>>

An other way to do that :>>> name.__repr__

<method-wrapper '__repr__' of str object at 0xb727af60>

>>>

0xb727af60 is exact memory location where the object stored, name is just a reference to that object.

Page 10: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Data Types Python offers1. Numbers2. Strings3. List4. Dictionaries5. Tuples6. Boolean7. …

Page 11: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Numbers - Integer● Normal integers

e.g. 4321● Octal literals (base 8)

A number prefixed by a 0 (zero) will be interpreted as an octal number>>> a = 010>>> print a8

● Hexadecimal literals (base 16)Hexadecimal literals have to be prefixed either by "0x" or "0X".>>> hex_number = 0xA0F>>> print hex_number2575

● Long integersthese numbers are of unlimeted sizee.g.42000000000000000000L

● Floating-point numbersfor example: 42.11, 3.1415e-10

Page 12: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Convert to Number>>>int('10')

10

>>>int('0011',2)

3

>>>int('1111',2)

15

>>>int('0xff',16)

255

Page 13: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

StringsStrings are marked by quotes:

– single quotes (')'This is a string with single quotes‘– double quotes (")

"Obama's dog is called Bo""– triple quotes, both single (''') and (""")

'''String in triple quotes can extend over multiple lines, like this on, and can contain'single' and "double" quotes.'''

Page 14: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Strings>>>name = 'reza'

>>>name = "reza">>>name = "rez'a">>>name = 'Mohammd\nreza'

>>>print name

Mohammad

reza

Page 15: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Strings>>>name = r'Mohammd\nreza'

raw string and turns off escapingprint name

Mohammd\nreza

>>>

Page 16: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Strings>>> name = '''

... Hello Dear Students

... Welcome to PYSEC101 Course!

... Python Scripting Course for Ethical Hackers

... '''

>>> name

'\nHello Dear Students\nWelcome to PYSEC101 Course!\nPython Scripting Course for Ethical Hackers\n'

>>> print name

Hello Dear Students

Welcome to PYSEC101 Course!

Python Scripting Course for Ethical Hackers

Page 17: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

String Index● A string in Python consists of a series or sequence of

characters - letters, numbers, and special characters.

Page 18: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Unicode ● Used for internationalization● “Wide Characters” are they are called(code all languages)>>>name = u'Mohammad'

>>> name

u'Mohammad'

>>>

unicode to regular string conversion>>> str(name)

'Mohammad'

>>>

regular string to unicode conversion>>> unicode(name)

u'Mohammad'

>>>

Page 19: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Concatenating Strings● Strings can be glued together (concatenated) with the +

operator>>>s1 + s2

>>> s1 = 'Hamid'

>>> s2 = 'rezaie'

>>> s1 + s2

'Hamidrezaie'

>>> s1 + ' ' + s2

'Hamid rezaie'

>>>

Page 20: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Repetition● String can be repeated or repeatedly concatenated with

the asterisk operator "*“>>> buffer = 'A' * 20

>>> buffer

'AAAAAAAAAAAAAAAAAAAA'

>>>

Page 21: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Int to string● >>> 'a' + 42● Traceback (most recent call last):● File "<stdin>", line 1, in <module>● TypeError: cannot concatenate 'str' and 'int' objects● >>>● >>> str(42)● '42'● >>>>'a' + str(42)

Page 22: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Slicing● Substrings can be created with the slice or slicing notation,

i.e. two indices in square brackets separated by a colon: ● "Python"[2:4] -> "th“● >>>string[start:end:steps]

Page 23: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

>>> name = 'Mohammad reza'

>>> name[5:10]

'mad r'

>>> name[0:10]

'Mohammad r'

>>> name[0:-1]

'Mohammad rez'

>>> name[0:-5]

'Mohammad'

>>> name[:]

'Mohammad reza'

>>> name[::-1]

'azer dammahoM'

>>> name[::2]

'Mhma ea'

>>>

Page 24: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

Stirngs are immutable objects>>>name = 'reza'

>>>name[0]

r

name [0] = 'a'

TypeError: 'str' object does not support item assignment

You can not change String Object directly in memory because they are immutable

>>>name = 'reza'

name = 'Mohammad'● 'reza' object still does exists now name is referenced to

'Mohammad' object

Page 25: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

String Methods● string.find()● string.replace()● string.split()

Page 26: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

>>> name = 'mohammad reza'

>>> name.find('PYSEC101')

-1

>>> name.find('mma')

4

>>>

>>> name.split()

['mohammad', 'reza']

by default split on white spaces and return list of strings.split on 'a'>>> name.split('a')

['moh', 'mm', 'd rez', '']

>>>

>>> name.replace('m', 'H')

'HohaHHad reza'

>>>

Page 27: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

String Formatting>>> ip = '192.168.1.252'

>>> line = 'Crack this IP :%s' % ip

>>> line

'Crack this IP :192.168.1.252'

>>>

>>> line = 'Crack this IP : %s and name %s ' % (ip, 'Reza-PC')

>>> line

'Crack this IP : 192.168.1.252 and name Reza-PC '

Page 28: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

ReferencesSPSE securitytube training by Vivek RamachandranSANS Python for Pentesters (SEC573)Violent pythonSecurity Power Toolspython-course.eu-----------------------------http://www.tutorialspoint.com/python/python_strings.htmhttp://www.tutorialspoint.com/python/python_numbers.htmhttp://www.python-course.eu/variables.phphttp://www.python-course.eu/sequential_data_types.phphttp://www.python-course.eu/sets_frozensets.php

Page 29: جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲

This work is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License.To view a copy of this license, visithttp://creativecommons.org/licenses/by-nd/3.0/

Copyright 2013 Mohammad Reza Kamalifard All rights reserved.Go to Kamalifard.ir/pysec101 to Download Slides and Course martials .