SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Python for Ethical Hackers
Mohammad reza Kamalifard
Kamalifard@datasec.ir
Python Language Essentials
Module 3 : Network Security
Part 1 :
SOCKET
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Network Programming
We Use Sockets for Network Programming :
TCP and UDP Sockets
Regular Servers and Clients
Raw Sockets
Sniffing and Injection
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Client/Server
Server
offer a service
Client
use/consume the service
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Socket
A network socket is an endpoint of an inter-process communication
flow across a computer network.
The Python socket module provides direct access to the standard BSD
socket interface, which is available on most modern computer
systems. The advantage of using Python for socket programming is
that socket addressing is simpler and much of the buffer allocation is
done for you. In addition, it is easy to create secure sockets and
several higher-level socket abstractions are available.
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Socket
To create a server, you need to:
create a socket
bind the socket to an address and port
listen for incoming connections
wait for clients
accept a client
send and receive data
To create a client, you need to:
create a socket
connect to the server
send and receive data
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Address
The BSD socket interface defines several different types of addresses called
families. These include:
AF_UNIX: A Unix socket allows two processes running on the same machine
to communicate with each other through the socket interface. In Python,
UNIX socket addresses are represented as a string.
AF_INET: An IPv4 socket is a socket between two processes, potentially
running on different machines, using the current version of IP (IP version 4).
This is the type of socket most programs use today. In Python, IPv4 socket
addresses are represented using a tuple of (host, port), where host is a string
host name and port is an integer called the port number. For the host name
you can specify either a standard Internet name, such as 'www.cnn.com' or an
IP address in dotted decimal notation, such as '64.236.24.20'.
AF_INET6: An IPv6 socket is similar to an IPv4 socket, except that it uses IP
version 6
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Address
To create a socket in Python, use the socket() method:
socket(family,type[,protocol])
The family is either AF_UNIX, AF_INET, or AF_INET6. There are several
different types of sockets; the main ones are SOCK_STREAM for TCP sockets
and SOCK_DGRAM for UDP sockets. You can skip the protocol number in
most cases.
Please note that the constants listed above for socket family and socket type
are defined inside the socket module. This means that you must import the
socket module and then reference them as 'socket.AF_INET'.
The important thing about the socket() method is it returns a socket object.
You can then use the socket object to call each of its methods, such as bind,
listen, accept, and connect.
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
import socket
#creating TCP Socket listen on a port
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# We bind Socket to 8000 Port and Interface with IP 0.0.0.0
tcp_socket.bind(('0.0.0.0', 8000))
# Start listening on IP and PORT for CLIENTS
tcp_socket.listen(2) # Argument here 2 is Number of Concurrent client Socket
can handle
# Start Accepting Clients
(client, (ip, port)) = tcp_socket.accept()
print client
print ip
print port
client.send('Welcome to PYSEC101 Course')
data = client.recv(2048) #buffer size is 2048
print data
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
-----Client----
nc 127.0.0.1 8000
Welcome to PYSEC101 Course
-----Server-----
<socket._socketobject object at 0x1420590>
127.0.0.1
33821
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Echo Server
import socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.bind(('127.0.0.1', 8000))
tcp_socket.listen(2)
print 'Waiting for client ...'
(client, (ip, port)) = tcp_socket.accept()
print 'Revived connection from : ', ip
print 'Starting ECHO output...'
data = 'dummy'
while len(data):
data = client.recv(2048)
print 'Client send : ', data
client.send(data)
print 'Closing Connection'
client.close()
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
-----Client----
nc 127.0.0.1 8000
Salam
Salam
PYSEC101 Echo Server
PYSEC101 Echo Server
^C
-----Server-----
Waiting for client ...
Revived connection from : 127.0.0.1
Starting ECHO output...
Client send : Salam
Client send : PYSEC101 Echo Server
Client send :
Closing Connection
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Echo Server With Reuse Address
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
import socket
tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
tcp_socket.bind(('127.0.0.1', 8000))
tcp_socket.listen(2)
print 'Waiting for client ...'
(client, (ip, port)) = tcp_socket.accept()
print 'Revived connection from : ', ip
print 'Starting ECHO output...'
data = 'dummy'
while len(data):
data = client.recv(2048)
print 'Client send : ', data
client.send(data)
print 'Closing Connection'
client.close()
Process Client Options
When client comes Connect we need to process client
Process client Sequentially and one at a time
Multi_Threaded Server
Multi_Process Server
Non_Blocking Sockets with Select (Multiplexing)
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
Exercise
Create a simple Echo Server to handle 1 client
Create a Multi_Threaded Echo Server
Create a Multi_Process Echo Server
Create a Non-Blocking Multiplexed Echo server using select()
Mohammad reza Kamalifard
Kamalifard.ir/pysec101
This work is licensed under the Creative Commons
Attribution-NoDerivs 3.0 Unported License.
To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/
Copyright 2013 Mohammad reza Kamalifard.
All rights reserved.

Weitere ähnliche Inhalte

Was ist angesagt?

Socket programming
Socket programmingSocket programming
Socket programmingharsh_bca06
 
Ethernet Shield
Ethernet ShieldEthernet Shield
Ethernet ShieldTinker
 
Socket programming
Socket programmingSocket programming
Socket programmingUjjwal Kumar
 
us-17-Tsai-A-New-Era-Of-SSRF-Exploiting-URL-Parser-In-Trending-Programming-La...
us-17-Tsai-A-New-Era-Of-SSRF-Exploiting-URL-Parser-In-Trending-Programming-La...us-17-Tsai-A-New-Era-Of-SSRF-Exploiting-URL-Parser-In-Trending-Programming-La...
us-17-Tsai-A-New-Era-Of-SSRF-Exploiting-URL-Parser-In-Trending-Programming-La...sonjeku1
 
Network configuration
Network configurationNetwork configuration
Network configurationengshemachi
 
Socket programming in C
Socket programming in CSocket programming in C
Socket programming in CDeepak Swain
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using CAjit Nayak
 
Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehi...
Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehi...Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehi...
Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehi...brien_wankel
 

Was ist angesagt? (20)

Socket programming
Socket programmingSocket programming
Socket programming
 
Ethernet Shield
Ethernet ShieldEthernet Shield
Ethernet Shield
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming
Socket programmingSocket programming
Socket programming
 
Socket programming in c
Socket programming in cSocket programming in c
Socket programming in c
 
us-17-Tsai-A-New-Era-Of-SSRF-Exploiting-URL-Parser-In-Trending-Programming-La...
us-17-Tsai-A-New-Era-Of-SSRF-Exploiting-URL-Parser-In-Trending-Programming-La...us-17-Tsai-A-New-Era-Of-SSRF-Exploiting-URL-Parser-In-Trending-Programming-La...
us-17-Tsai-A-New-Era-Of-SSRF-Exploiting-URL-Parser-In-Trending-Programming-La...
 
Network configuration
Network configurationNetwork configuration
Network configuration
 
Socket programming in C
Socket programming in CSocket programming in C
Socket programming in C
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Socket programming using C
Socket programming using CSocket programming using C
Socket programming using C
 
Sockets intro
Sockets introSockets intro
Sockets intro
 
Sockets
SocketsSockets
Sockets
 
Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehi...
Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehi...Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehi...
Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehi...
 
Arduino práctico ethernet
Arduino práctico   ethernetArduino práctico   ethernet
Arduino práctico ethernet
 
Elementary TCP Sockets
Elementary TCP SocketsElementary TCP Sockets
Elementary TCP Sockets
 
Ppt of socket
Ppt of socketPpt of socket
Ppt of socket
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
Sockets
SocketsSockets
Sockets
 
Os 2
Os 2Os 2
Os 2
 
Socket programming
Socket programming Socket programming
Socket programming
 

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

Ähnlich wie اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی (20)

Python networking
Python networkingPython networking
Python networking
 
Net Programming.ppt
Net Programming.pptNet Programming.ppt
Net Programming.ppt
 
Raspberry pi Part 23
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
 
Network Prog.ppt
Network Prog.pptNetwork Prog.ppt
Network Prog.ppt
 
Unit 8 Java
Unit 8 JavaUnit 8 Java
Unit 8 Java
 
Sockets
Sockets Sockets
Sockets
 
Np unit2
Np unit2Np unit2
Np unit2
 
Network programming using python
Network programming using pythonNetwork programming using python
Network programming using python
 
Socket Programming
Socket ProgrammingSocket Programming
Socket Programming
 
Network Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptxNetwork Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptx
 
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونیاسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
 
03 sockets
03 sockets03 sockets
03 sockets
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
Socket programming using java
Socket programming using javaSocket programming using java
Socket programming using java
 
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jbCN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
CN_UNIT4.ppt notre knxckvj bjbDJKVHFL jb
 
EN-04 (1).pptx
EN-04 (1).pptxEN-04 (1).pptx
EN-04 (1).pptx
 
Pycon - Python for ethical hackers
Pycon - Python for ethical hackers Pycon - Python for ethical hackers
Pycon - Python for ethical hackers
 
Socket programming-tutorial-sk
Socket programming-tutorial-skSocket programming-tutorial-sk
Socket programming-tutorial-sk
 
Socket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdfSocket Programming TCP:IP PPT.pdf
Socket Programming TCP:IP PPT.pdf
 
python programming
python programmingpython programming
python programming
 

Mehr von Mohammad Reza Kamalifard

Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacyTehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacyMohammad Reza Kamalifard
 
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱Mohammad Reza Kamalifard
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲Mohammad Reza Kamalifard
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱Mohammad Reza Kamalifard
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲Mohammad Reza Kamalifard
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
 
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیMohammad Reza Kamalifard
 
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی Mohammad Reza Kamalifard
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی Mohammad Reza Kamalifard
 
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی Mohammad Reza Kamalifard
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیMohammad Reza Kamalifard
 
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونیMohammad Reza Kamalifard
 
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونیMohammad Reza Kamalifard
 
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونیMohammad Reza Kamalifard
 

Mehr von Mohammad Reza Kamalifard (20)

Internet Age
Internet AgeInternet Age
Internet Age
 
Introduction to Flask Micro Framework
Introduction to Flask Micro FrameworkIntroduction to Flask Micro Framework
Introduction to Flask Micro Framework
 
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacyTehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy
 
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
 
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
 
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
 
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
 

Kürzlich hochgeladen

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 

Kürzlich hochgeladen (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 

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

  • 1. Python for Ethical Hackers Mohammad reza Kamalifard Kamalifard@datasec.ir
  • 2. Python Language Essentials Module 3 : Network Security Part 1 : SOCKET Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 3. Network Programming We Use Sockets for Network Programming : TCP and UDP Sockets Regular Servers and Clients Raw Sockets Sniffing and Injection Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 4. Client/Server Server offer a service Client use/consume the service Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 5. Socket A network socket is an endpoint of an inter-process communication flow across a computer network. The Python socket module provides direct access to the standard BSD socket interface, which is available on most modern computer systems. The advantage of using Python for socket programming is that socket addressing is simpler and much of the buffer allocation is done for you. In addition, it is easy to create secure sockets and several higher-level socket abstractions are available. Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 6. Socket To create a server, you need to: create a socket bind the socket to an address and port listen for incoming connections wait for clients accept a client send and receive data To create a client, you need to: create a socket connect to the server send and receive data Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 7. Address The BSD socket interface defines several different types of addresses called families. These include: AF_UNIX: A Unix socket allows two processes running on the same machine to communicate with each other through the socket interface. In Python, UNIX socket addresses are represented as a string. AF_INET: An IPv4 socket is a socket between two processes, potentially running on different machines, using the current version of IP (IP version 4). This is the type of socket most programs use today. In Python, IPv4 socket addresses are represented using a tuple of (host, port), where host is a string host name and port is an integer called the port number. For the host name you can specify either a standard Internet name, such as 'www.cnn.com' or an IP address in dotted decimal notation, such as '64.236.24.20'. AF_INET6: An IPv6 socket is similar to an IPv4 socket, except that it uses IP version 6 Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 8. Address To create a socket in Python, use the socket() method: socket(family,type[,protocol]) The family is either AF_UNIX, AF_INET, or AF_INET6. There are several different types of sockets; the main ones are SOCK_STREAM for TCP sockets and SOCK_DGRAM for UDP sockets. You can skip the protocol number in most cases. Please note that the constants listed above for socket family and socket type are defined inside the socket module. This means that you must import the socket module and then reference them as 'socket.AF_INET'. The important thing about the socket() method is it returns a socket object. You can then use the socket object to call each of its methods, such as bind, listen, accept, and connect. Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 9. import socket #creating TCP Socket listen on a port tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # We bind Socket to 8000 Port and Interface with IP 0.0.0.0 tcp_socket.bind(('0.0.0.0', 8000)) # Start listening on IP and PORT for CLIENTS tcp_socket.listen(2) # Argument here 2 is Number of Concurrent client Socket can handle # Start Accepting Clients (client, (ip, port)) = tcp_socket.accept() print client print ip print port client.send('Welcome to PYSEC101 Course') data = client.recv(2048) #buffer size is 2048 print data Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 10. -----Client---- nc 127.0.0.1 8000 Welcome to PYSEC101 Course -----Server----- <socket._socketobject object at 0x1420590> 127.0.0.1 33821 Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 11. Echo Server import socket tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcp_socket.bind(('127.0.0.1', 8000)) tcp_socket.listen(2) print 'Waiting for client ...' (client, (ip, port)) = tcp_socket.accept() print 'Revived connection from : ', ip print 'Starting ECHO output...' data = 'dummy' while len(data): data = client.recv(2048) print 'Client send : ', data client.send(data) print 'Closing Connection' client.close() Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 12. -----Client---- nc 127.0.0.1 8000 Salam Salam PYSEC101 Echo Server PYSEC101 Echo Server ^C -----Server----- Waiting for client ... Revived connection from : 127.0.0.1 Starting ECHO output... Client send : Salam Client send : PYSEC101 Echo Server Client send : Closing Connection Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 13. Echo Server With Reuse Address Mohammad reza Kamalifard Kamalifard.ir/pysec101 import socket tcp_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) tcp_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) tcp_socket.bind(('127.0.0.1', 8000)) tcp_socket.listen(2) print 'Waiting for client ...' (client, (ip, port)) = tcp_socket.accept() print 'Revived connection from : ', ip print 'Starting ECHO output...' data = 'dummy' while len(data): data = client.recv(2048) print 'Client send : ', data client.send(data) print 'Closing Connection' client.close()
  • 14. Process Client Options When client comes Connect we need to process client Process client Sequentially and one at a time Multi_Threaded Server Multi_Process Server Non_Blocking Sockets with Select (Multiplexing) Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 15. Exercise Create a simple Echo Server to handle 1 client Create a Multi_Threaded Echo Server Create a Multi_Process Echo Server Create a Non-Blocking Multiplexed Echo server using select() Mohammad reza Kamalifard Kamalifard.ir/pysec101
  • 16. This work is licensed under the Creative Commons Attribution-NoDerivs 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nd/3.0/ Copyright 2013 Mohammad reza Kamalifard. All rights reserved.