Instead of being indexed by numbers, dictionaries are using keys for indexing elements
We have some methods:
.values(): returns all the values stored in the dictionary
.keys(): returns all the keys stored in the dictionary
.items(): returns all the keys and values in the dictionary
We can also check wif an specific item exists using the existing two following methods:
key in dictionary
get(key, message): if the key exists, returns the associated value, otherwise prints the message passed as an parameter
dictionary ={'first':'one','second':2}
# Functions# - `function_name.__doc__` shows function description# - each call to a function creates a new local scope as well as the assigned names within a function that are local to that function# - global variables can ge used within the function, but to do that we need to insert the keyword `global` followed by the variable namedeffunction_name(param1,param2, ...):""" function documentation """ function_statementsreturn expression# Modules# A module is a file that contains source codefrom module_name import object_name1, object_name2, ...from module_name import*
Network sockets
""" server.py """""" Binds itself to a specific address and port and will listen for incoming TCP communications """import socketSVR_ADDR =input("Type the server IP address: ")SRV_PORT =input("Type the server port: ")s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)# Default family socket, using TCP and the default socket type connection-oriented (SOCK_STREAM)s.bind((SRV_ADDR, SRV_PORT))s.listen(1)print("Server started! Waiting for connections...")connection, address = s.accept()# 'connection' is the socket object we will use to send and receive data, 'address' contains the client address bound to the socketprint('Client connected with address:', address)while1:# maximum number of queued connections data = connection.recv(1024)ifnot data:break connection.sendall(b'-- Message Received --\n')print(data.decode('utf-8'))connection.close()
nc<server_ip><port>
""" client.py """import socketSVR_ADDR =input("Type the server IP address: ")SRV_PORT =input("Type the server port: ")my_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)my_sock.connect((SVR_ADDR, SRV_PORT))print("Connection established")message =input("Message to send: ")my_sock.sendall(message.encode())my_sock.close()
Port Scanner
Instead of using connect() we'll use connect_ex(), which returns - if the operation succeeded.
This script will use the full 3-way handshake.
import sockettarget =input('Enter the IP address to scan: ')portrange =input('Enter the port range to scan (ex 5-200): ')lowport =int(portrange.split('-')[0])highport =int(portrange.split('-')[1])print('Scanning host ', target, 'from port', lowport, 'to port', highport)for port inrange(lowport, highport): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) status = s.connect((target, port))if (status ==0):print('*** Port', port, ' - OPEN ***')else:print('*** Port', port, ' - Closed ***') s.close()
Backdoor
The program simply binds itself to a NIC and a specific port (6666) and then waits for the client commands. Depending on the command received, it will return specific information to the client.