When it comes to creating powerful and effec tive hacking tools, Python is the language of


    Chapter 2 TCP Server



Download 4,64 Mb.
Pdf ko'rish
bet15/17
Sana31.12.2021
Hajmi4,64 Mb.
#239671
1   ...   9   10   11   12   13   14   15   16   17
Bog'liq
Early Access BlackHatPython 2nd edition

12

   


Chapter 2

TCP Server

Creating TCP servers in Python is just as easy as creating a client. You might 

want to use your own TCP server when writing command shells or crafting a 

proxy (both of which we’ll do later). Let’s start by creating a standard multi-

threaded TCP server. Crank out the following code:

import socket

import threading

IP = '0.0.0.0'

PORT = 9998

def main()

    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    1 server.bind((IP, PORT))

    2 server.listen(5)

    print(f'[*] Listening on {IP}:{PORT}')

    while True:

        3 client, address = server.accept()

        print(f'[*] Accepted connection from {address[0]}:{address[1]}')

        client_handler = threading.Thread(target=handle_client, 

args=(client,))

        4 client_handler.start()

5 def handle_client(client_socket):

    with client_socket as sock:

        request = sock.recv(1024)

        print(f'[*] Received: {request.decode("utf-8")}')

        sock.send(b'ACK')

if __name__ == '__main__':

    main()

To start off, we pass in the IP address and port we want the server to lis-

ten on 1. Next, we tell the server to start listening 2, with a maximum back-

log of connections set to 5. We then put the server into its main loop, where 

it waits for an incoming connection. When a client connects 3, we receive 

the client socket in the 

client

 variable and the remote connection details in 



the 

address


 variable. We then create a new thread object that points to our 

handle_client

 function, and we pass it the client socket object as an argument. 

We then start the thread to handle the client connection 4, at which point 

the main server loop is ready to handle another incoming connection. The 

handle_client

 function 5 performs the 

recv()


 and then sends a simple mes-

sage back to the client.

If you use the TCP client that we built earlier, you can send some test 

packets to the server. You should see output like the following:

[*] Listening on 0.0.0.0:9998

[*] Accepted connection from: 127.0.0.1:62512

[*] Received: ABCDEF

Black Hat Python (Early Access) © 2021 by Justin Seitz and Tim Arnold




The Network: Basics

   


13

That’s it! While pretty simple, this is a very useful piece of code. We’ll 

extend it in the next couple of sections, when we build a netcat replacement 

and a TCP proxy.




Download 4,64 Mb.

Do'stlaringiz bilan baham:
1   ...   9   10   11   12   13   14   15   16   17




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish