SlideShare una empresa de Scribd logo
1 de 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.

Más contenido relacionado

La actualidad más candente

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
 
Socket programming in C
Socket programming in CSocket programming in C
Socket programming in C
Deepak Swain
 

La actualidad más candente (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
 

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

Similar a اسلاید اول جلسه یازدهم کلاس پایتون برای هکرهای قانونی (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
 

Más de Mohammad Reza Kamalifard

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

Más de 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
 
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
 
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
 
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
 
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
 
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
 
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
 
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
 
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
 
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
 
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
 

Último

Último (20)

Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
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
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
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
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 

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

  • 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.