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

Mohammad Reza Kamalifard
Mohammad Reza KamalifardTechnical Team Lead at Karina Mobile Solutions à Karina Mobile Solutions
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.
1 sur 16

Recommandé

Programming TCP/IP with Sockets par
Programming TCP/IP with SocketsProgramming TCP/IP with Sockets
Programming TCP/IP with Socketselliando dias
5.8K vues38 diapositives
Advanced Sockets Programming par
Advanced Sockets ProgrammingAdvanced Sockets Programming
Advanced Sockets Programmingelliando dias
3.4K vues40 diapositives
Socket System Calls par
Socket System CallsSocket System Calls
Socket System CallsAvinash Varma Kalidindi
31.1K vues22 diapositives
Network Sockets par
Network SocketsNetwork Sockets
Network SocketsPeter R. Egli
7.7K vues28 diapositives
Sockets par
Sockets Sockets
Sockets babu4b4u
269 vues28 diapositives
Socket programming par
Socket programmingSocket programming
Socket programmingAnurag Tomar
863 vues39 diapositives

Contenu connexe

Tendances

Socket programming par
Socket programmingSocket programming
Socket programmingharsh_bca06
6.1K vues11 diapositives
Ethernet Shield par
Ethernet ShieldEthernet Shield
Ethernet ShieldTinker
1.8K vues22 diapositives
Socket programming par
Socket programmingSocket programming
Socket programmingMuhammad Fouad Ilyas Siddiqui
2.2K vues34 diapositives
Socket programming par
Socket programmingSocket programming
Socket programmingUjjwal Kumar
2.2K vues17 diapositives
Socket programming in c par
Socket programming in cSocket programming in c
Socket programming in cMd. Golam Hossain
224 vues24 diapositives
us-17-Tsai-A-New-Era-Of-SSRF-Exploiting-URL-Parser-In-Trending-Programming-La... par
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
92 vues90 diapositives

Tendances(20)

Socket programming par harsh_bca06
Socket programmingSocket programming
Socket programming
harsh_bca066.1K vues
Ethernet Shield par Tinker
Ethernet ShieldEthernet Shield
Ethernet Shield
Tinker1.8K vues
us-17-Tsai-A-New-Era-Of-SSRF-Exploiting-URL-Parser-In-Trending-Programming-La... par sonjeku1
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...
sonjeku192 vues
Network configuration par engshemachi
Network configurationNetwork configuration
Network configuration
engshemachi5.7K vues
Socket programming in C par Deepak Swain
Socket programming in CSocket programming in C
Socket programming in C
Deepak Swain1.1K vues
Socket programming using C par Ajit Nayak
Socket programming using CSocket programming using C
Socket programming using C
Ajit Nayak1.7K vues
Customize Your Car: An Adventure in Using Elixir and Nerves to Hack Your Vehi... par brien_wankel
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_wankel790 vues

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

Net Programming.ppt par
Net Programming.pptNet Programming.ppt
Net Programming.pptEloAcubaOgardo
4 vues37 diapositives
Raspberry pi Part 23 par
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23Techvilla
302 vues29 diapositives
Network Prog.ppt par
Network Prog.pptNetwork Prog.ppt
Network Prog.pptEloOgardo
4 vues37 diapositives
Unit 8 Java par
Unit 8 JavaUnit 8 Java
Unit 8 Javaarnold 7490
1.5K vues31 diapositives
Sockets par
Sockets Sockets
Sockets Gopaiah Sanaka
161 vues33 diapositives

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

Raspberry pi Part 23 par Techvilla
Raspberry pi Part 23Raspberry pi Part 23
Raspberry pi Part 23
Techvilla302 vues
Network programming using python par Ali Nezhad
Network programming using pythonNetwork programming using python
Network programming using python
Ali Nezhad198 vues
Socket Programming par CEC Landran
Socket ProgrammingSocket Programming
Socket Programming
CEC Landran6.7K vues
Network Programming-Python-13-8-2023.pptx par ssuser23035c
Network Programming-Python-13-8-2023.pptxNetwork Programming-Python-13-8-2023.pptx
Network Programming-Python-13-8-2023.pptx
ssuser23035c4 vues
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی par Mohammad Reza Kamalifard
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونیاسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
اسلاید دوم جلسه یازدهم کلاس پایتون برای هکر های قانونی
Socket programming using java par UC San Diego
Socket programming using javaSocket programming using java
Socket programming using java
UC San Diego5.7K vues

Plus de Mohammad Reza Kamalifard

Internet Age par
Internet AgeInternet Age
Internet AgeMohammad Reza Kamalifard
560 vues45 diapositives
Introduction to Flask Micro Framework par
Introduction to Flask Micro FrameworkIntroduction to Flask Micro Framework
Introduction to Flask Micro FrameworkMohammad Reza Kamalifard
991 vues38 diapositives
Tehlug 26 Nov 2013 Hackers,Cyberwarfare and Online privacy par
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
516 vues44 diapositives
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲ par
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
800 vues29 diapositives
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲ par
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
414 vues22 diapositives
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲ par
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲Mohammad Reza Kamalifard
317 vues25 diapositives

Plus de Mohammad Reza Kamalifard(20)

جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲ par Mohammad Reza Kamalifard
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه دوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲ par Mohammad Reza Kamalifard
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه سوم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲ par Mohammad Reza Kamalifard
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه چهارم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱ par Mohammad Reza Kamalifard
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲ par Mohammad Reza Kamalifard
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه پنجم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱ par Mohammad Reza Kamalifard
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۱
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲ par Mohammad Reza Kamalifard
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه ششم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲- ارائه ۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲ par Mohammad Reza Kamalifard
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲ par Mohammad Reza Kamalifard
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
جلسه اول پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی par Mohammad Reza Kamalifard
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه اول دوره پاییز کلاس پایتون برای هکرهای قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی par Mohammad Reza Kamalifard
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه دوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی par Mohammad Reza Kamalifard
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه اول جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی par Mohammad Reza Kamalifard
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید ارائه سوم جلسه ۱۰ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی par Mohammad Reza Kamalifard
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونیاسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی par Mohammad Reza Kamalifard
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونیاسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید اول جلسه هشتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی par Mohammad Reza Kamalifard
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید سوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی par Mohammad Reza Kamalifard
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونیاسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی
اسلاید دوم جلسه هفتم کلاس پایتون برای هکرهای قانونی

Dernier

UNIDAD 3 6º C.MEDIO.pptx par
UNIDAD 3 6º C.MEDIO.pptxUNIDAD 3 6º C.MEDIO.pptx
UNIDAD 3 6º C.MEDIO.pptxMarcosRodriguezUcedo
150 vues32 diapositives
Six Sigma Concept by Sahil Srivastava.pptx par
Six Sigma Concept by Sahil Srivastava.pptxSix Sigma Concept by Sahil Srivastava.pptx
Six Sigma Concept by Sahil Srivastava.pptxSahil Srivastava
51 vues11 diapositives
Ask The Expert! Nonprofit Website Tools, Tips, and Technology.pdf par
 Ask The Expert! Nonprofit Website Tools, Tips, and Technology.pdf Ask The Expert! Nonprofit Website Tools, Tips, and Technology.pdf
Ask The Expert! Nonprofit Website Tools, Tips, and Technology.pdfTechSoup
62 vues28 diapositives
BUSINESS ETHICS MODULE 1 UNIT I_A.pdf par
BUSINESS ETHICS MODULE 1 UNIT I_A.pdfBUSINESS ETHICS MODULE 1 UNIT I_A.pdf
BUSINESS ETHICS MODULE 1 UNIT I_A.pdfDr Vijay Vishwakarma
92 vues25 diapositives
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice par
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a ChoiceCreative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a ChoiceTaste
52 vues50 diapositives
Career Building in AI - Technologies, Trends and Opportunities par
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and OpportunitiesWebStackAcademy
47 vues44 diapositives

Dernier(20)

Ask The Expert! Nonprofit Website Tools, Tips, and Technology.pdf par TechSoup
 Ask The Expert! Nonprofit Website Tools, Tips, and Technology.pdf Ask The Expert! Nonprofit Website Tools, Tips, and Technology.pdf
Ask The Expert! Nonprofit Website Tools, Tips, and Technology.pdf
TechSoup 62 vues
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice par Taste
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a ChoiceCreative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice
Creative Restart 2023: Atila Martins - Craft: A Necessity, Not a Choice
Taste52 vues
Career Building in AI - Technologies, Trends and Opportunities par WebStackAcademy
Career Building in AI - Technologies, Trends and OpportunitiesCareer Building in AI - Technologies, Trends and Opportunities
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy47 vues
Education of marginalized and socially disadvantages segments.pptx par GarimaBhati5
Education of marginalized and socially disadvantages segments.pptxEducation of marginalized and socially disadvantages segments.pptx
Education of marginalized and socially disadvantages segments.pptx
GarimaBhati547 vues
UNIT NO 13 ORGANISMS AND POPULATION.pptx par Madhuri Bhande
UNIT NO 13 ORGANISMS AND POPULATION.pptxUNIT NO 13 ORGANISMS AND POPULATION.pptx
UNIT NO 13 ORGANISMS AND POPULATION.pptx
Madhuri Bhande43 vues
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37 par MysoreMuleSoftMeetup
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
Payment Integration using Braintree Connector | MuleSoft Mysore Meetup #37
Monthly Information Session for MV Asterix (November) par Esquimalt MFRC
Monthly Information Session for MV Asterix (November)Monthly Information Session for MV Asterix (November)
Monthly Information Session for MV Asterix (November)
Esquimalt MFRC213 vues
NodeJS and ExpressJS.pdf par ArthyR3
NodeJS and ExpressJS.pdfNodeJS and ExpressJS.pdf
NodeJS and ExpressJS.pdf
ArthyR350 vues
Introduction to AERO Supply Chain - #BEAERO Trainning program par Guennoun Wajih
Introduction to AERO Supply Chain  - #BEAERO Trainning programIntroduction to AERO Supply Chain  - #BEAERO Trainning program
Introduction to AERO Supply Chain - #BEAERO Trainning program
Guennoun Wajih123 vues
Creative Restart 2023: Leonard Savage - The Permanent Brief: Unearthing unobv... par Taste
Creative Restart 2023: Leonard Savage - The Permanent Brief: Unearthing unobv...Creative Restart 2023: Leonard Savage - The Permanent Brief: Unearthing unobv...
Creative Restart 2023: Leonard Savage - The Permanent Brief: Unearthing unobv...
Taste62 vues
JRN 362 - Lecture Twenty-Three (Epilogue) par Rich Hanley
JRN 362 - Lecture Twenty-Three (Epilogue)JRN 362 - Lecture Twenty-Three (Epilogue)
JRN 362 - Lecture Twenty-Three (Epilogue)
Rich Hanley43 vues
Retail Store Scavenger Hunt.pptx par jmurphy154
Retail Store Scavenger Hunt.pptxRetail Store Scavenger Hunt.pptx
Retail Store Scavenger Hunt.pptx
jmurphy15453 vues
Interaction of microorganisms with vascular plants.pptx par MicrobiologyMicro
Interaction of microorganisms with vascular plants.pptxInteraction of microorganisms with vascular plants.pptx
Interaction of microorganisms with vascular plants.pptx

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

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