Hacking With Python-Code Blocks
Hacking With Python-Code Blocks
Commands
Code block
root@kali:/home# pwd
/home
root@kali:/home#
Code block
Code block
root@kali:/# cd /home/sanjib/
root@kali:/home/sanjib# cd ..
root@kali:/home#
Code block
2
Code block
root@kali:/home# cp -v [Link]
/home/sanjib/Documents/
‘[Link]’ ->
‘/home/sanjib/Documents/[Link]’
root@kali:/home#
Code block
Code block
root@kali:/home# cd sanjib/Documents/
root@kali:/home/sanjib/Documents# ls
[Link]
root@kali:/home/sanjib/Documents#
Code block
3
Code block
cat [Link]
Code block
Code block
nano [Link]
Code block
Code block
grep src
main restricted
deb-src [Link]
universe
deb-src [Link]
trusty-updates universe
multiverse
deb-src [Link]
trusty-updates multiverse
deb-src [Link]
multiverse
security universe
security multiverse
5
# deb-src [Link]
trusty partner
universe
hagudu@hagudu-H81M-S1:/etc/apt$
Code block
Code block
Code block
Code block
hagudu@hagudu-H81M-S1:~$
Code block
Code block
Code block
Code block
Hacking/
hagudu@hagudu-H81M-S1:~$ ls
7
Code block
Code block
hagudu@hagudu-H81M-S1:~$ cd Ethical-Hacking/
hagudu@hagudu-H81M-S1:~/Ethical-Hacking$ ls
hagudu@hagudu-H81M-S1:~/Ethical-Hacking$ touch
file1 file2
hagudu@hagudu-H81M-S1:~/Ethical-Hacking$ ls
file1 file2
hagudu@hagudu-H81M-S1:~/Ethical-Hacking$
Code block
Code block
sanjib@kali:~$ cd Documents/
8
sanjib@kali:~/Documents$ ls
[Link]
sanjib@kali:~/Documents$ ls -la
total 7048
[Link]
sanjib@kali:~/Documents$
Code block
Code block
[Link]
Code block
9
Code block
r-xr-xr-x
Code block
Code block
root@kali:~#
Code block
Code block
xman@kali:~$ cd Documents/
xman@kali:~/Documents$ ls
11
xman@kali:~/Documents$ ls -la
total 8
xman@kali:~/Documents$
Code block
Code block
#!/usr/bin/python3
inputs = input(">>>>>>")
outputs = inputs
def main():
print(outputs)
if __name__ == '__main__':
main()
12
Code block
xman@kali:~/Documents$ ls -la
total 12
xman@kali:~/Documents$
Code block
Code block
xman@kali:~/Documents$ ls -la
total 12
xman@kali:~/Documents$
Code block
Code block
xman@kali:~/Documents$ ./[Link]
>>>>>>xman
xman
Code block
PART TWO
14
Code block
hagudu@hagudu-H81M-S1:~$ python3
>>>
Code block
Code block
>>> print(name)
Sanjib
>>>
15
Code block
Code block
#!/usr/bin/python3
def main():
print(“Hello Python!”)
if __name__ == "__main__":
main()
Code block
Code block
Code block
Code block
16
./[Link]
Code block
Code block
#!/usr/bin/python3
def main():
if __name__ == "__main__":
main()
Code block
Code block
17
#!/usr/bin/python3
def main():
LetUsDoSomething()
def LetUsDoSomething():
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
LetUsDoSomething()
18
def LetUsDoSomething():
Code block
Code block
# coding=utf-8
def main():
if __name__ == main():main()
Code block
Code block
19
# coding=utf-8
def main():
if __name__ == main():main()
Code block
Code block
# coding=utf-8
def main():
OutsideMainFunction()
def OutsideMainFunction():
20
x = 0
while x < 5:
print(x)
x = x + 1
if __name__ == main():main()
Code block
8.3 – Commenting
Code block
def main():
OutsideMainFunction()
def OutsideMainFunction():
21
x = 0
while x < 5:
print(x)
x = x + 1
if __name__ == main():main()
Code block
Code block
#!/usr/bin/python3
# coding=utf-8
a = 1
print(a)
print(type(a))
22
print(id(a))
a = "One"
print(a)
print(type(a))
print(id(a))
Code block
#!/usr/bin/python3
def main():
x = 1
23
print(x)
print(id(x))
print(type(x))
x = 2
print(x)
print(id(x))
print(type(x))
x = 1
print(x)
print(id(x))
print(type(x))
if __name__ == "__main__":
main()
Code block
24
Code block
#!/usr/bin/python3
x = 1
print(type(x))
print(id(x))
##################
# class 'int'
# 139113568
##################
x = 1
y = 1
print(type(x))
print(id(x))
print(type(y))
print(id(y))
if x == y:
print("True")
else:
print("False")
if x is y:
print("True")
else:
print("False")
##################
26
# class 'int'
# 139113568
# class 'int'
# 139113568
# True
# True
##################
a = dict(x = 1, y = 1)
print(type(a))
print(id(a))
b = dict(x = 1, y = 1)
print(id(b))
if a == b:
print("True")
else:
27
print("False")
if a is b:
print("True")
else:
print("False")
##################
# class 'dict'
# 3072650252
# 3072692524
# True
# False
##################
##################
# 0 = 139113552
# 1 = 139113568
# 2 = 139113584
##################
Code block
Code block
#!/usr/bin/python3
def main():
x = 3
print(x)
print(id(x))
29
print(type(x))
print("*********")
x = 3 /2
print(x)
print(id(x))
print(type(x))
print("*********")
x = round(42 / 9)
print(x)
print(id(x))
print(type(x))
print("*********")
# we want to round it up
x = 42 // 9
print(x)
print(id(x))
30
print(type(x))
print("*********")
x = round(42 / 9, 3)
print(x)
print(id(x))
print(type(x))
print("*********")
x = 43 % 7
print(x)
print(id(x))
print(type(x))
print("*********")
x = int(34.78)
print(x)
print(id(x))
31
print(type(x))
print("*********")
x = float(23)
print(x)
print(id(x))
print(type(x))
print("*********")
if __name__ == "__main__":
main()
Code block
9.2 – String
Code block
#!/usr/bin/python3
32
def main():
print(strings)
print(anotherStrings)
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
def main():
33
print(strings)
print(anotherStrings)
print(rawStrings)
if __name__ == "__main__":
main()
Code block
Code block
days = 8
34
you." %days
print(lyrics)
Code block
Code block
days = 8
you."
print([Link](days))
Code block
Code block
35
newLines = """\
first line
second line
third line
more to come...
"""
print(newLines)
Code block
Code block
>>> x = 10
>>> x
36
10
>>> type(x)
<class 'int'>
>>> id(x)
10455328
>>> y = 10
>>> y
10
>>> type(y)
<class 'int'>
37
>>> id(y)
10455328
>>> a = dict(name='sanjib')
>>> a
{'name': 'sanjib'}
>>> type(a)
<class 'dict'>
>>> id(a)
38
139984318683592
>>> b = dict(name='sanjib')
>>> b
{'name': 'sanjib'}
>>> type(b)
<class 'dict'>
>>> id(b)
139984318683720
39
>>> a == b
True
>>> a is b
False
>>>
Code block
Code block
>>> x == y
True
40
>>> x is y
True
>>>
Code block
Code block
>>> a = dict(name='sanjib')
>>> a
{'name': 'sanjib'}
>>> type(a)
<class 'dict'>
41
>>> id(a)
139984318683592
>>> b = dict(name='sanjib')
>>> b
{'name': 'sanjib'}
>>> type(b)
<class 'dict'>
>>> id(b)
42
139984318683720
>>> a == b
True
>>> a is b
False
>>>
Code block
Code block
>>> a, b = 0, 1
>>> a == b
False
>>> a < b
True
>>> a > b
False
44
>>> a = True
>>> a
True
>>> type(a)
<class 'bool'>
>>> id(a)
10348608
>>> b = True
45
>>> b
True
>>> type(b)
<class 'bool'>
>>> id(b)
10348608
>>>
Code block
46
Code block
x = (1, 2, 3, 4)
print(x)
print(type(x))
Code block
Code block
for i in x:
print(i)
Code block
Code block
a = [1, 2, 3, 4]
print(a)
print(type(a))
47
Code block
Code block
#!/usr/bin/python3
# tuple
x = (1, 2, 3, 4)
# list
a = [1, 2, 3, 4]
[Link](x)
print(a)
[Link](0, x)
48
print(a)
for i in a:
print(i)
Code block
Code block
print(WeWillIterateThroughIt)
Code block
Code block
strings = "string."
print(strings[1:3])
49
Code block
9.6 – Dictionary
Code block
#!usr/bin/python3
EnglishDictionaries = {'bare':'jejune',
'anger':'dudgeon', 'abuse':'vituperate',
'howl':'ululate'}
print(EnglishDictionaries)
Code block
Code block
EnglishDictionaries = {'bare':'jejune',
50
'anger':'dudgeon', 'abuse':'vituperate',
'howl':'ululate'}
Code block
Code block
Code block
9.7 – Object
Code block
#!/usr/bin/python3
class Human:
51
[Link] = kind
def whatKind(self):
return [Link]
def main():
GoodHuman = Human()
print([Link]())
BadHuman = Human("Bad")
print([Link]())
if __name__ == "__main__":
main()
Code block
Code block
class Human:
[Link] = kind
def whatKind(self):
return [Link]
Code block
Code block
def whatKind(self):
return [Link]
Code block
Code block
def main():
GoodHuman = Human()
print([Link]())
BadHuman = Human("Bad")
print([Link]())
53
if __name__ == "__main__":
main()
Code block
Chapter 10 – Conditionals
Code block
def conditionals_exec():
a, b = 1, 3
if a < b:
elif a > b:
else:
conditionals_exec()
54
Code block
Code block
def conditional_values():
a, b = 1, 2
less than."
print(statements)
conditional_values()
Code block
Code block
def main():
conditionals_exec()
conditional_values()
55
def conditionals_exec():
a, b = 1, 3
if a < b:
elif a > b:
else:
def conditional_values():
a, b = 1, 2
less than."
print(statements)
56
Code block
Chapter 11 – Loops
Code block
b = 1
print(b)
b = b + 1
Code block
Code block
#!/usr/bin/python3
57
a, b = 0, 1
a, b = b, a + b
Code block
Code block
#!/usr/bin/python3
a, b = 0, 1
a = b
b = a + b
Code block
58
Code block
#!/usr/bin/python3
a, b = 0, 1
')
a, b = b, a + b
print("***********")
a, b = 0, 1
')
a = b
59
b = a + b
Code block
Code block
#!/usr/bin/python3
songs = open('[Link]')
print(lines, end='')
Code block
Code block
# enumerate
songs = open('[Link]')
Code block
Code block
this string
if s == 's':
position {}".format(index))
Code block
Code block
#!/usr/bin/python3
def main():
loops(0)
61
loops()
loops(3)
print("*************")
if __name__ == "__main__":
main()
Code block
Code block
62
#!/usr/bin/python3
import re
def main():
ReplaceWord()
DEmarcationLine()
MatchAndReplaceWord()
def ReplaceWord():
try:
files = open("../primary/[Link]")
print([Link]('lenor|more', "#####",
except FileNotFoundError as e:
def MatchAndReplaceWord():
try:
files = open("../primary/[Link]")
match = [Link]('(len|neverm)ore',
line)
if match:
print([Link]([Link](),
except FileNotFoundError as e:
def DEmarcationLine():
64
print("*************")
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
import re
def main():
CompilerAndReplaceWord()
def CompilerAndReplaceWord():
try:
65
files = open("../primary/[Link]")
pattern = [Link]('(len|neverm)ore',
[Link])
line
if [Link](pattern, line):
print([Link]("######", line),
end=' ')
except FileNotFoundError as e:
if __name__ == "__main__":
66
main()
Code block
Expressions
Code block
#!/usr/bin/python3
import re
def main():
FindWord()
DEmarcationLine()
MatchWord()
def FindWord():
try:
files = open("../primary/[Link]")
67
if [Link]('lenor|more', line):
except FileNotFoundError as e:
def MatchWord():
try:
files = open("../primary/[Link]")
match = [Link]('(len|neverm)ore',
line)
if match:
print([Link]())
68
except FileNotFoundError as e:
def DEmarcationLine():
print("*************")
if __name__ == "__main__":
main()
Code block
Errors
Code block
Code block
69
Code block
>>> 10 * x
10 * x
>>> 10 / 0
10 / 0
>>> '2' + 2
'2' + 2
70
implicitly
>>> inputs + 2
inputs + 2
implicitly
>>> inputs - 10
inputs - 10
71
and 'int'
>>> int(inputs) - 10
>>>
Code block
Code block
#!/usr/bin/python3
def main():
FileRead()
DemarcationLine()
LineStrip()
DemarcationLine()
CheckFileExtension()
def ReadFile(filename):
72
files = open(filename)
lines = [Link]()
def StripFile(filename):
files = open(filename)
def RaisingError(filename):
if [Link](".txt"):
lines = open(filename)
else:
def FileRead():
try:
ReadFile("../primary/[Link]") # path is
73
except IOError as e:
def LineStrip():
try:
StripFile("primary/[Link]")
except IOError as e:
give error
def CheckFileExtension():
try:
RaisingError("../primary/[Link]")
except IOError as e:
except ValueError as e:
print("Bad Filename:", e)
74
def DemarcationLine():
print("******************")
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
def main():
GetARangeOfNumber()
def GetARangeOfNumber():
number = start
yield number
number += step
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
def main():
if __name__ == "__main__":
main()
Code block
76
Code block
print(“*************”)
Code block
Code block
def DemarcationLine():
print(“*********”)
DemarcationLine():
DemarcationLine():
DemarcationLine():
Code block
Code block
def AnotherFunction():
def TestFunction():
77
AnotherFunction()
TestFunction()
Code block
Code block
def TestFunction():
AnotherFunction()
TestFunction()
def AnotherFunction():
Code block
78
Code block
#!/usr/bin/python3
def main():
TestFunction()
def TestFunction():
AnotherFunction()
def AnotherFunction():
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
def main():
79
PassingParameters(1,2,3)
argument3):
argument2, argument3)
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
def main():
PassingParameters(1)
argument3 = 6):
argument2, argument3)
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
def main():
argument3 = 6):
argument2, argument3)
if __name__ == "__main__":
main()
Code block
81
Code block
#!/usr/bin/python3
def main():
PassingParameters(1)
argument3 = 6):
if argument2 == None:
argument2, argument3)
else:
argument2, argument3)
if __name__ == "__main__":
main()
Code block
82
Code block
#!/usr/bin/python3
def main():
PassingParameters(1, 12)
argument3 = 6):
if argument2 == None:
argument2, argument3)
else:
argument2, argument3)
if __name__ == "__main__":
main()
Code block
83
Code block
#!/usr/bin/python3
def main():
def ReturnValues():
#return 56
return range(10)
if __name__ == "__main__":
main()
Code block
84
Code block
#!/usr/bin/python3
def main():
RangeFunctions()
def RangeFunctions():
for i in range(10):
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
def main():
85
i = start
yield i
i += step
if __name__ == "__main__":
main()
Code block
Code block
Code block
86
Code block
1):
i = start
yield i
i += step
Code block
Code block
Code block
Code block
87
def AnotherRangeFunctions(*args):
numberOfArguments = len(args)
elif numberOfArguments == 1:
stop = args[0]
start = 0
step = 1
elif numberOfArguments == 2:
step = 1
elif numberOfArguments == 3:
i = start
yield i
i += step
Code block
Code block
#!/usr/bin/python3
def main():
PassingAnotherListsOfArguments(1, 2, 3, 5, 7,
*args):
*params):
print(param1, param2)
if index == 76:
x = 10
y = index + x
continue
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
def main():
def NamedArguments(**kwargs):
if __name__ == "__main__":
main()
Code block
91
Code block
#!/usr/bin/python3
def main():
DemarcationLine()
1, two = 2, three = 3)
def NamedArguments(**kwargs):
**kwargs):
92
print(arg1, arg2)
DemarcationLine()
def DemarcationLine():
print("********")
if __name__ == "__main__":
main()
Code block
Chapter 15 - Classes
93
Code block
#!/usr/bin/python3
class Robot:
def __init__(self):
pass
def WalkLikeARobot(self):
def CareLikeARobot(self):
robu1 = Robot()
print(type(robu1))
print(id(robu1))
94
robu2 = Robot()
print(type(robu2))
print(id(robu2))
del robu2
def main():
robu = Robot()
print(type(robu))
print(id(robu))
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
class Robots:
def __init__(self):
95
pass
[Link] = style
return [Link]
def CareLikeARobot(self):
class Humans:
[Link] = nature
def GoodHumanBeing(self):
is always", [Link])
def BadHUmanBeing(self):
print([Link])
96
[Link] = style
return [Link]
def main():
robu = Robots()
[Link]()
print([Link]("walks like a
robot"))
GoodMan = Humans()
print([Link])
[Link]()
BadMan = Humans()
[Link] = "bad"
print([Link])
[Link]()
if __name__ == "__main__":
main()
Code block
Code block
print(type([Link](dict(one=1,
two=2))))
st = [Link](dict(one=1, two=2))
print(keys, st[keys])
ws = [Link]({'one':56, 'two':2})
print(keys, ws[keys])
Code block
98
Code block
Code block
Code block
./ [Link]
Code block
Code block
#!/usr/bin/python3
class Robots:
def __init__(self):
pass
[Link] = WalkingStyle
return [Link]
def CareLikeARobot(self):
class Humans:
[Link] = nature
def GoodHumanBeing(self):
is always", [Link])
def BadHUmanBeing(self):
print([Link])
[Link] = WalkingStyle
100
return [Link]
def main():
robu = Robots()
# [Link]()
GoodMan = Humans()
# print([Link])
# [Link]()
BadMan = Humans()
# [Link] = "bad"
# print([Link])
# [Link]()
happen
WhenABadManWalksLikeARobot =
monster inside',
fellow people',
torturing animals',
none',
state = 'finally he
destroys himself'))
Robot?")
102
and 'angel',"
WhenABadManWalksLikeARobot['change'] = change
reward = 0
if change == 'monster':
change)
reward = 1000
WhenABadManWalksLikeARobot['act'])
WhenABadManWalksLikeARobot['change'] =
change
if change == 'great':
reward = 10000
"points.")
WhenABadManWalksLikeARobot['feel'])
'no',"
WhenABadManWalksLikeARobot['change'] =
change
if change == 'no':
round:")
reward = 100000
"points.")
WhenABadManWalksLikeARobot['care'])
WhenABadManWalksLikeARobot['change'
] = change
if change == 'yes':
round:")
reward = 1000000
"points.")
"and type
here...>>>>")
WhenABadManWalksLikeARobot['cha
nge'] = change
if change == 'yes':
fifth round:")
reward = 100000000
else:
106
else:
else:
100, "points.")
else:
"points.")
else:
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
class Human:
[Link] = kind
def BeingHuman(self):
return [Link]
def main():
good = Human()
bad = Human("bad")
108
print([Link] ())
print([Link] ())
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
class MySelf:
[Link] = name
[Link] = quantity
def Eat(self):
def main():
109
hagu = MySelf("Hagu", 2)
mutu = MySelf("Mutu", 3)
[Link]()
[Link]()
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
import sqlite3
import [Link]
class MySQLiteConnection:
def __init__(self):
db = [Link]('[Link]')
110
[Link]()
print("Connected to SqLite3")
class MyMySQLConnection:
def __init__(self):
try:
= 'pass')
conn =
[Link](**kwargs)
connection =
[Link](host = 'localhost',
111
database = 'python_mysql',
user =
'root',
password = 'pass')
if connection.is_connected():
'conneection' object")
# if conn.is_connected():
object")
except Error as e:
print(e)
finally:
[Link]()
def main():
ConnectToMySQL = MyMySQLConnection()
112
ConenctToSqLite = MySQLiteConnection()
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
import sqlite3
import [Link]
class MySQLiteConnection:
def __init__(self, db =
[Link]('[Link]')):
113
[Link] = db
db.row_factory = [Link]
print("Connected to SqLite3")
def Retrieve(self):
order by i1')
print(row['t1'])
class MyMySQLConnection:
password = 'pass')):
try:
[Link] = kwargs
conn =
[Link](**kwargs)
if conn.is_connected():
except Error as e:
print(e)
finally:
[Link]()
def Retrieve(self):
database testdb.")
try:
115
conn = MySQLConnection(**[Link])
cursor = [Link]()
[Link]("SELECT * FROM
EMPLOYEE")
rows = [Link]()
except Error as e:
print(e)
finally:
[Link]()
116
[Link]()
def main():
ConnectToMySQL = MyMySQLConnection()
[Link]()
ConenctToSqLite = MySQLiteConnection()
[Link]()
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
class Human:
[Link] = height
117
def main():
ramu = Human()
print([Link])
print([Link])
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
class Human:
def __init__(self):
pass
# accessor
118
[Link] = height
def get_height(self):
return [Link]
def main():
ramu = Human()
ramu.set_height(5.12)
print(ramu.get_height())
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
119
class Human:
[Link] = kwargs
[Link] = kwargs
[Link][key] = value
def main():
[Link]['name'])
ManaName = [Link]['name']
mana.set_variables('class', 'two')
mana.get_variables('class'))
mana.set_manyVariables(school = 'balika
is", [Link]['school'])
BabuName = [Link]['name']
if __name__ == "__main__":
main()
Code block
121
19.5 - Polymorphism
Code block
#!/usrbin/python3
class Table:
def __init__(self):
pass
def ItHolds(self):
it.")
def YouCanWriteOnit(self):
class Book:
def __init__(self):
pass
def ItHelps(self):
122
new.")
def main():
MyTable = Table()
MyBook = Book()
[Link]()
[Link]()
[Link]()
if __name__ == "__main__":
main()
Code block
Code block
#!/usrbin/python3
class Table:
123
def __init__(self):
pass
def Get(self):
def Put(self):
of the room.")
def Destroy(self):
table.")
class Book:
def __init__(self):
pass
def Get(self):
124
def Put(self):
table.")
def Destroy(self):
book.")
def main():
MyTable = Table()
MyBook = Book()
InMistake(MyBook)
Intentionally(MyTable)
def InMistake(Table):
[Link]()
[Link]()
125
[Link]()
def Intentionally(Book):
[Link]()
[Link]()
[Link]()
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
class InclusiveRange:
numberOfArguments = len(args)
126
elif numberOfArguments == 1:
[Link] = args[0]
[Link] = 0
[Link] = 1
elif numberOfArguments == 2:
[Link] = 1
elif numberOfArguments == 3:
tuple
args
def __iter__(self):
i = [Link]
yield i
i += [Link]
def main():
for x in ranges:
if __name__ == "__main__":
main()
Code block
128
Code block
numberOfArguments = len(args)
elif numberOfArguments == 1:
[Link] = args[0]
[Link] = 0
[Link] = 1
elif numberOfArguments == 2:
[Link] = 1
elif numberOfArguments == 3:
tuple
args
i = [Link]
yield i
i += [Link]
Code block
130
15.7 - Inheritance
Code block
#!/usr/bin/python3
class AllUsers:
def __init__(self):
pass
def Register(self):
print("Please Register")
def Login(self):
print("Welcome Member.")
class Admin(AllUsers):
def __init__(self):
pass
def Register(self):
def Login(self):
print("Welcome Admin")
class Members(AllUsers):
def __init__(self):
pass
def main():
admin = Admin()
[Link]()
[Link]()
member = Members()
[Link]()
[Link]()
if __name__ == "__main__":
main()
Code block
132
19.8 -Decorator
Code block
#!/usr/bin/python3
class Dog:
[Link] = kwargs
def get_properties(self):
return [Link]
[Link](key, None)
def main():
print([Link]('nature'))
133
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
class Dog:
[Link] = kwargs
@property
def Color(self):
@[Link]
[Link]['color'] = color
134
@[Link]
def Color(self):
del [Link]['color']
def main():
lucky = Dog()
a normal property
print([Link])
if __name__ == "__main__":
main()
Code block
Code block
hagudu@hagudu-H81M-S1:~$ python3
'this is a string'
>>> s
'this is a string'
>>> [Link]()
'THIS IS A STRING'
>>> [Link](100)
>>>
Code block
Code block
>>> s
'this is a string'
>>> [Link]()
138
'THIS IS A STRING'
>>> [Link]()
'this is a string'
>>> s
'This Is A String'
>>> [Link]()
'tHIS iS a sTRING'
139
>>> s
'This Is A String'
>>> [Link]('is')
>>>
Code block
Code block
#!/usr/bin/python3
140
s = 'this is a string'
print([Link]('is'))
print(newstring)
UpperString = [Link]()
print(UpperString)
same string
print(id(s))
print(id(UpperString))
end
RemovingWhiteSpace = [Link]()
print(RemovingWhiteSpace)
141
print([Link]('this'))
Code block
Code block
x, y = 10, 11
FormattedString = [Link](x, y)
print(FormattedString)
m, n = 10, 11
FormattedString = f % (x, y)
print(FormattedString)
Code block
Code block
142
a, b = 10, 11
FormattedStirng = [Link](a, b)
print(FormattedStirng)
FormattedStirng = [Link](b, a)
print(FormattedStirng)
FormattedStirng = [Link](a, b)
print(FormattedStirng)
positional argument
FormattedStirng = [Link](a, b)
143
print(FormattedStirng)
print(FormattedStirng)
is mine: {mine}"
print(FormattedStirng)
Code block
Code block
144
print(type(strings))
print(id(strings))
print(type(AnotherStrings))
print(id(AnotherStrings))
print([Link]())
words = [Link]()
print(type(words))
print(words[0])
NewWords = ":".join(words)
print(NewWords)
NewWords = ",".join(words)
print(NewWords)
words[0] = "That"
145
print(words)
Code block
Code block
print(line, file=outfile)
print("Done")
Code block
Code block
BufferSize = 500000
146
buffer = [Link](BufferSize)
while len(buffer):
[Link](buffer)
buffer = [Link](BufferSize)
print()
print("Copying Done.")
Code block
Code block
BufferSize = 5000000
buffer = [Link](BufferSize)
while len(buffer):
[Link](buffer)
buffer = [Link](BufferSize)
print()
print("Copying Done.")
Code block
Code block
#!/usr/bin/python3
tuples1 = 1, 2, 3, 4
print(type(tuples1))
148
print(id(tuples1))
tuples2 = (1, 2, 3, 4)
print(type(tuples2))
print(id(tuples2))
print(tuples1[0])
print(tuples2[0])
print(tuples2[-1])
print(type(tuples1[0]))
print(type(tuples2[0]))
print(id(tuples1[0]))
print(id(tuples2[0]))
# tuples2[0] = 120
# print(tuples2)
149
separator
IsItTuple = (1)
print(type(IsItTuple))
IsItTuple = (1,)
print(type(IsItTuple))
list1 = [1, 2, 3, 4]
print(type(list1))
print(id(list1))
# first item
print(list1[0])
# last item
print(list1[-1])
list1[0] = 120
150
Code block
Object
Code block
root@kali:~# python3
>>> t = (1,2,3,4)
>>> t
(1, 2, 3, 4)
>>> t[0]
1
151
>>> t = tuple(range(25))
>>> type(t)
<class 'tuple'>
>>> 50 in t
False
>>> 10 in t
True
...
6
152
10
11
12
13
14
15
16
17
18
19
20
21
22
153
23
24
>>> l = list(range(20))
>>> type(l)
<class 'list'>
>>> for i in l:
... print(i)
print(i)
...
2
154
10
11
12
13
14
15
16
17
18
155
19
>>> l[2]
>>> 50 in l
False
>>> 12 in l
True
>>> t[0] = 25
assignment
>>> l[0] = 25
>>> print(l)
>>> [Link](50)
'append'
>>> [Link](120)
>>> print(l
l lambda
>>> print(l)
>>> [Link]()
given)
>>> [Link](5)
>>> [Link](25)
>>> [Link](25)
>>> [Link](10)
10
>>> [Link](10)
10
>>> [Link](range(25))
...
25
1
158
10
11
12
13
14
15
16
17
159
18
19
120
25
10
11
160
12
13
14
15
16
17
18
19
20
21
22
23
24
>>> l[0]
4656
161
>>> [Link](12)
14
>>> l[12]
147
>>> [Link](12)
>>> l[12]
147
>>> print(l)
>>> [Link](12)
>>> print(l)
162
>>> [Link](0)
4656
>>> print(l)
6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20,
>>> [Link]()
24
>>> print(l)
6, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20,
>>>
Code block
Code block
root@kali:~# python3
>>> type(x)
<class 'dict'>
>>> type(y)
<class 'dict'>
**y)
**y)
>>> type(z)
<class 'dict'>
>>> print(z)
'seven': 7, 'three': 3}
...
eight
two
nine
one
seven
three
...
eight 8
two 2
nine 9
one 1
seven 7
three 3
... print(value)
...
>>> [Link]()
>>> [Link](three)
>>> [Link]('three')
3
167
...
eight
two
nine
one
seven
... print(value)
...
>>>
Code block
Code block
#!/usr/bin/python3
import sqlite3
def main():
db = [Link]('[Link]')
db.row_factory = [Link]
int)')
[Link]()
order by i1')
# print(dict(row))
print(row['t1'])
# print(row['t1'], row['i1'])
170
# print(type(row))
if __name__ == "__main__":main()
Code block
Code block
#!/usr/bin/python3
import [Link]
def ConnectionTest():
try:
###
171
conn = [Link](**kwargs)
connection = [Link](host =
'localhost',
databa
se = 'python_mysql',
user =
'root',
passwo
rd = 'pass')
if conn.is_connected():
except Error as e:
print(e)
finally:
172
[Link]()
if __name__ == "__main__":
ConnectionTest()
Code block
Code block
#!/usr/bin/python3
import [Link]
def RetrieveValues():
try:
conn = [Link](**kwargs)
API
fetchall() ###
if conn.is_connected():
cursors = [Link]()
[Link]('SELECT * FROM
authors')
# row = [Link]()
'123828863494')
######
# row = [Link]()
# print(type(row))
for loop
174
# print(books)
######
# HowManyBooks = 8
# row = [Link](HowManyBooks)
# print(books)
row = [Link]()
print(books)
except Error as e:
print(e)
175
finally: [Link]()
if __name__ == "__main__":
RetrieveValues()
Code block
Code block
[mysql]
host = localhost
database = YourDatabaseName
user = root
password = pass
Code block
Code block
#!/usr/bin/python3
def ReadingMySQLConfig(filemame =
parser = ConfigParser()
[Link](filemame)
db = dict()
if parser.has_section(section):
items = [Link](section)
db[item[0]] = item[1]
else:
return db
Code block
Code block
177
#!/usr/bin/python3
ReadingMySQLConfig
def Connect():
kwargs = ReadingMySQLConfig()
MyConnection = MySQLConnection(**kwargs)
try:
if MyConnection.is_connected():
print("Connected")
except Error as e:
print(e)
finally:
[Link]()
if __name__ == "__main__":
Connect()
178
Code block
Code block
#!/usr/bin/python3
import [Link]
def connect():
try:
conn =
[Link](host='localhost',
database='Yo
urDatabase',
user='root',
password='Yo
179
urPassword')
if conn.is_connected():
except Error as e:
print(e)
finally:
[Link]()
if __name__ == '__main__':
connect()
Code block
Code block
#!/usr/bin/python3
read_db_config
180
while True:
rows = [Link](size)
if not rows:
break
yield row
def query_with_fetchmany():
try:
dbconfig = read_db_config()
conn = MySQLConnection(**dbconfig)
cursor = [Link]()
print(row)
181
except Error as e:
print(e)
finally:
[Link]()
[Link]()
if __name__ == '__main__':
query_with_fetchmany()
Code block
Code block
#!/usr/bin/python3
read_db_config
def query_with_fetchall():
try:
182
dbconfig = read_db_config()
conn = MySQLConnection(**dbconfig)
cursor = [Link]()
rows = [Link]()
except Error as e:
print(e)
finally:
[Link]()
183
[Link]()
if __name__ == '__main__':
query_with_fetchall()
Code block
Code block
#!/usr/bin/python3
ReadingMySQLConfig
def InsertBooks(books):
VALUES(%s, %s)"
try:
kwargs = ReadingMySQLConfig()
MyConnection = MySQLConnection(**kwargs)
184
if MyConnection.is_connected():
cursor = [Link]()
[Link](query, books)
[Link]()
except Error as e:
print(e)
finally:
[Link]()
def main():
InsertBooks(books)
if __name__ == "__main__":
main()
Code block
185
Code block
#!/usr/bin/python3
ReadingMySQLConfig
kwargs = ReadingMySQLConfig()
%s"
try:
MyConnection = MySQLConnection(**kwargs)
cursor = [Link]()
[Link](query, data)
[Link]()
except Error as e:
186
print(e)
finally:
[Link]()
def main():
if id == 3:
elif id == 4:
elif id == 5:
if __name__ == "__main__":
main()
187
Code block
Code block
#!/usr/bin/python3
ReadingMySQLConfig
def DeleteBooks(book_id):
kwargs = ReadingMySQLConfig()
try:
MyConnection = MySQLConnection(**kwargs)
cursor = [Link]()
[Link](query, (book_id,))
[Link]()
except Error as e:
188
print(e)
finally:
[Link]()
def main():
id = 87
DeleteBooks(id)
books")
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
ReadingMySQLConfig
def ReadFile(filename):
images = [Link]()
return images
kwargs = ReadingMySQLConfig()
data = ReadFile(filename)
= %s"
try:
MyConnection = MySQLConnection(**kwargs)
cursor = [Link]()
[Link](query, args)
[Link]()
190
except Error as e:
print(e)
finally:
[Link]()
def main():
id = 47
UpdateImage(id, "/home/hagudu/Pictures/[Link]")
updated.")
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
ReadingMySQLConfig
[Link](data)
kwargs = ReadingMySQLConfig()
%s'
try:
MyConnection = MySQLConnection(**kwargs)
cursor = [Link]()
[Link](query, (author_id,))
photo = [Link]()[0]
WriteFile(photo, filename)
except Error as e:
192
print(e)
finally:
[Link]()
def main():
id = 47
ReadImage(id, "/home/hagudu/Pictures/[Link]")
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
def main():
{}".format(*sys.version_info))
# os module
print([Link])
print([Link]('PATH'))
print([Link]())
#urllib module
page =
[Link]('[Link]
if __name__ == "__main__":
main()
Code block
194
Code block
#!/usr/bin/python3
def main():
{}".format(*sys.version_info))
# random module
print([Link](1, 1000))
x = list(range(25))
print(x)
[Link](x)
print(x)
[Link](x)
print(x)
195
[Link](x)
print(x)
PresentTime = [Link]()
print(PresentTime)
print([Link], [Link],
[Link], [Link],
[Link], [Link],
[Link])
if __name__ == "__main__":
main()
Code block
Module
Code block
196
#!/usr/bin/python3
# coding=utf-8
def PyVer():
{}".format(*sys.version_info))
def PyTime():
PresentTime = [Link]()
print(PresentTime)
print([Link], [Link],
[Link], [Link],
[Link],
[Link],
[Link])
#print(obj)
def main():
197
PyVer()
PyTime()
def test_Pyvar():
PyVer()
def test_Main():
PyTime()
if __name__ == "__main__":
main()
Code block
Code block
#!/usr/bin/python3
# coding=utf-8
import [Link]
import unittest
class SayTiemDate([Link]):
def setUP(self):
198
pass
def test_Version(self):
[Link]([Link]
.PyVer(),
[Link].test_Pyvar())
def test_Time(self):
[Link]([Link]
.main(),
[Link].test_Main())
if __name__ == "__main__":
[Link]()
Code block
Code block
# coding=utf-8
import socket
print([Link]("[Link].
com"))
print([Link]("[Link]
[Link]"))
Code block
Code block
nmap
specification}
TARGET SPECIFICATION:
[Link]; 10.0.0-255.1-254
hosts/networks
hosts/networks
file
HOST DISCOVERY:
discovery
[default: sometimes]
DNS servers
SCAN TECHNIQUES:
-sS/sT/sA/sW/sM: TCP
SYN/Connect()/ACK/Window/Maimon scans
25,80,139,8080,S:9
scan
ports
203
<ratio>
SERVICE/VERSION DETECTION:
service/version info
(intensity 2)
9)
SCRIPT SCAN:
separated list of
204
categories
--script-args=<n1=v1,[n2=v2,...]>: provide
arguments to scripts
args in a file
scripts.
of script-files or
script-categories.
OS DETECTION:
targets
205
--min-parallelism/max-parallelism <numprobes>:
Probe parallelization
--min-rtt-timeout/max-rtt-timeout/initial-rtt-
probe retransmissions.
206
this long
w/given MTU)
decoys
packets
specified ip options
checksum
OUTPUT:
given filename.
at once
208
greater effect)
greater effect)
particular state
received
debugging)
MISC:
file location
frames or IP packets
privileged
210
privileges
EXAMPLES:
nmap -v -A [Link]
Code block
#!/usr/bin/python
import nmap
nm = [Link]()
print ('------------------')
callback=callback_result)
while nm.still_scanning():
print("Waiting >>>")
[Link](2)
nm1 = [Link]()
a = nm1.nmap_version()
212
print (a)
Waiting >>>
------------------
('[Link]', None)
(6, 40)
[Link]
Network Scanner
213
Code block
#!/usr/bin/python
import nmap
nm = [Link]()
print (nm.nmap_version())
print([Link]())
print([Link]())
Code block
hagudu@hagudu-H81M-S1:~$ ./[Link]
(6, 40)
'connect'}}
214
host;hostname;hostname_type;protocol;port;name;stat
e;product;extrainfo;reason;version;conf;cpe
[Link];host3.x0x;PTR;tcp;22;ssh;open;;;syn-
ack;;3;
[Link];host3.x0x;PTR;tcp;25;smtp;open;;;syn-
ack;;3;
[Link];host3.x0x;PTR;tcp;53;domain;open;;;syn-
ack;;3;
[Link];host3.x0x;PTR;tcp;80;http;open;;;syn-
ack;;3;
[Link];host3.x0x;PTR;tcp;137;netbios-
ns;filtered;;;no-response;;3;
[Link];host3.x0x;PTR;tcp;138;netbios-
dgm;filtered;;;no-response;;3;
[Link];host3.x0x;PTR;tcp;139;netbios-
ssn;filtered;;;no-response;;3;
215
[Link];host3.x0x;PTR;tcp;445;microsoft-
ds;filtered;;;no-response;;3;
Code block
#!/usr/bin/python
import nmap
nm = [Link]()
print (nm.nmap_version())
print([Link]())
print([Link]())
(6, 40)
'connect'}}
216
host;hostname;hostname_type;protocol;port;name;stat
e;product;extrainfo;reason;version;conf;cpe
[Link];;;tcp;25;smtp;open;;;syn-ack;;3;
[Link];;;tcp;53;domain;open;;;syn-ack;;3;
[Link];;;tcp;80;http;open;;;syn-ack;;3;
Code block
#!/usr/bin/python
import nmap
nm = [Link]()
print (nm.nmap_version())
all')
print(nm.all_hosts())
217
(6, 40)
'connect'}}
['[Link]']
#!/usr/bin/python
import nmap
nm = [Link]()
print (nm.nmap_version())
print(nm.all_hosts())
Code block
218
#!/usr/bin/python
import nmap
nm = [Link]()
print (nm.nmap_version())
all')
print ([Link]())
print (nm['[Link]'].state())
print (nm['[Link]'].all_protocols())
print (nm['[Link]']['tcp'].keys())
(6, 40)
'elapsed': '5.73'}
219
up
['tcp']
[Link];host3.x0x;PTR;tcp;22;ssh;open;;;syn-
ack;;3;
[Link];host3.x0x;PTR;tcp;25;smtp;open;;;syn-
ack;;3;
[Link];host3.x0x;PTR;tcp;53;domain;open;;;syn-
ack;;3;
[Link];host3.x0x;PTR;tcp;80;http;open;;;syn-
ack;;3;
[Link];host3.x0x;PTR;tcp;137;netbios-
ns;filtered;;;no-response;;3;
[Link];host3.x0x;PTR;tcp;138;netbios-
dgm;filtered;;;no-response;;3;
220
[Link];host3.x0x;PTR;tcp;139;netbios-
ssn;filtered;;;no-response;;3;
[Link];host3.x0x;PTR;tcp;445;microsoft-
ds;filtered;;;no-response;;3;
PART THREE
Code block
Code block
Code block
Code block
221
Network or VPN
Code block
cat /etc/[Link]
Code block
Code block
nano /etc/dhcp/[Link]
Code block
Code block
Code block
222
Code block
sanjib@kali:~$ cd Downloads/
sanjib@kali:~/Downloads$ ls
[Link]
[Link]
[Link]
[Link]
Code block
Code block
openvpn [Link]
Code block
Code block
ipconfig
Code block
Code block
SYSTEMS)
SYSTEMS)
Code block
Code block
root@kali:~# macchanger –h
Code block