Python3 For Pentesting - Developing A TCP Client
Key Takeaways
The video demonstrates how to create a TCP client using Python's socket module, covering topics such as socket programming, network communication, and client-server communication. It provides a step-by-step guide on how to initialize a connection, transfer data, and decode received messages.
Full Transcript
[Music] hey guys hackersploit here back again with another video and welcome back to the python for ethical hacking series in this video we're going to be looking at creating the tcp client so in the previous video we looked at how to create the tcp server but now it's time to move on to that uh to that end and finally we'll uh we'll initialize the connection and see how data is being transferred uh you know for the for the purpose or for the sake of learning about sockets uh that being said i did uh tell you guys to leave feedback after the first video and we created the tcp server and that was uh a very very good thing because some of you did not understand a few things and uh you you had a lot of feedback for me so thank you for that and before we actually get into creating the tcp client let's look at the feedback that you gave me so there were a few points that you guys pointed out and some of them were questions others were uh were like points that you had made to me and i totally got them anyway so essentially the first thing that many people didn't get was uh why are we using the get host name this was always i was always told that this was good programming practice in the sense that you should be able to know how to automate the process of getting the ip address of the server that's the server that's what this function does here it gets the it gets the host name uh in theory it gets the ip address now uh of course this usually causes a bit of trouble because uh if you're running it on windows it might not get the correct ip belonging to the subnet and you can experiment that uh for yourself now i didn't mention it in that video that was because we weren't testing the connection uh but this host just as it is of course there are a few additions that i was going to make so you can see that the variable here uh is using the um is using the module and the function from the socket module called get hostname so this is the this essentially will get the value or the ip address of the host or the server now if if you if you don't want this you can easily just change or add the ip address here in fact i could add this comment right here i could say host is equal to and because it's a string and we want to keep it that way i would check my ip address of the server so ipconfig let me just check that and yes it is 0.104 so 192.168.1.104 and that's how i would initialize that variable but again you can see what i was talking about when we talk about automation it's always good to make your script as intelligent as possible so uh that's why i use this function so for those of you asking what it does and what parameters you can specify the parameters can furthermore be specified when you're talking about binding to the socket so for example this is commented and it's not active so we can change the value in here and replace it with the ip so 182.168.1.104 and remember to put it in to quotation because it is of type string you can convert it to uh two integer but we are already converting it uh for ourselves uh when we when the data is being transferred back and forth okay so that was uh the first thing you guys did not uh get again you can also change the port the same way i specified here don't worry about it whatever you find yourself comfortable with remember it's all about experimentation all right so the other thing that you guys pointed out wanted to know was what i was doing here with the percentage sign and with this str so essentially str is to convert this into a string all right and the percentage allows you to convert uh you know certain data into different data types so for example if i wanted to convert this which i should have actually thank you for reminding me uh if i wanted to convert it into a string i would use the instead of using uh str because then i would have to encapsulate it twice and then it would be too much i would use the percentage and this uh the s command and as you can see the s will will denote a string conversion okay so that means we have converted this into a string now for those of you who are a bit confused uh and you're asking whether you can encapsulate it as one whole data type in python what you do is you would use the um the wrapper command which is the r command which if you just hover over it right now you can see that it's going to it doesn't really mean string and i'll explain what this does this means that uh this statement will not be printed in the form of one data type so right now when we had s here it was printing it out as of as it was printing it out as a string but when you have the wrapper the wrapper can be used to make this statement as unambiguous as possible in the sense that it is not printed out in the traditional way of a string which is also very very useful and we'll be looking at that later on and then uh some of you are asking what this does here and i was a bit shocked to be honest because if you should be knowing this if you have been doing python this essentially goes to the next line this specifies that we are going to the next line all right so i added it there with uh concatenation to make sure that the the socket closes and uh finally ends uh at in the next line all right but before we we do that uh you can see that uh a lot of you especially a lot of you keen guys were pointing out that i should have encoded the message and you're absolutely right i wanted to talk about that in the next video and not mention it sorry about that guys i got a little message there apologies for the interruption uh so what i was saying is you are talking about encoding and that is true we are supposed to encode it and keep the data passed as small as possible because if we were to do this and not encode the data the likelihood of us transferring this data this would actually fail if we try to so uh you recommended that we use ascii so i'm going to do that so it's really very simple so the message is the message variable and we want to encode this so we use the encode function so encode and in brackets we specify the incr the encoding type so we can specify you can see that you can specify utf-8 but in this case you said ascii so let's keep it to ascii now when we're creating the client i know we are stretching along a bit too far now when creating the client i'll show you how to decode this message uh and i'll also show you a way of not using a loop uh once we have it we've established connection all right so that's what you guys had recommended here so let's get started now with the most important bit which is creating the tcp client now the tcp client is really very simple and nothing has changed or will come over in terms of uh client in terms of functions so there's no new functions that you should be looking for but uh probably the only one would be would be the decode and encode all right so let's get started so the first thing i want to do is i want to specify the um the the python version that we're using so user bin and python python 3 sorry about that so let's import the module now again a lot of you are saying why it didn't import everything again i said we're going really really slowly we're going really really gradual we are building our skill set as we move along so i'll show you how to import an entire uh how to import every method or function from the entire module it's really very simple sorry about this uh not module uh socket i was i typed out what i was thinking all right so import socket now we need to create the socket object and we can just call this client socket and that's going to be equal to socket dot socket and in here we would specify the uh the the ip protocol version and uh then the transmission control the tcp or we would specify udp a connectionless connection or one that uses uh a handshake which is dcp and that's what we're doing all right so we'll say socket dot af inet and we then say oops socket um dot af actually this is uh this is supposed to be a socket.sockstream actually because yes we have specified that so that socket.sockstream if we're using a udp would say d gram that's what we will be looking at in the next videos and i'll probably make an advanced one with that i was thinking of making a little chat application and then uh since this is ethical hacking it wouldn't be wise to create a gui but anyway we'll be looking at all the frameworks and all that exciting stuff as we move along all right so again here in terms of specifying the host we can actually give it the variable so this is going to be the server ip now this is where now you'd have to specify it every time or you'd have to make sure that the client does know the ip address of the uh of the server so in this case we can also specify 192.168.1.104 or you guys can also do it the correct way which is to get the host name and then you specify the host when connecting to the port which uh is the preferred way of doing it so what i was talking about is if we said host here and i'll i'll comment one of these we can say socket dot get host name oops sorry about that um dot get host name and then you know inside the parameters we don't need to specify anything so if i comment this and we use the preferred way let me just specify the port which is supposed to match which is supposed to match sorry that is 444 yes that's the correct one so now when connecting now to the port uh i would say uh client socket dot connect because we're using the connect function and india would specify the host which is the address as you can see uh and the port now i can specify it right now so i can say 192.168.1.104 and this any of these methods will work just fine and then we can specify the port or you can change the port here as that's how you know python is uh is interpreted okay so we'll specify the port now the most important thing and also something you guys pointed out is how we managing how data is being sent so yes we can uh we can uh we can essentially make sure that the data is coming in in units or in a certain amount of bytes meaning making sure that we we do not accept any more data than what we need so since we're encoding we've encoded the message we can finally say that the message is is should be uh so sorry about that client uh socket should be less than receive um so i'm pretty sure we can say dot receive and we specify the data um the buffer size as it's telling us here so this will be sorry not 1 1023 so this is the maximum amount of data that can that we will allow to come through the port okay or that we will allow being transmitted using the transmission control protocol now again you can use the same unit factorization to uh increase so you can say 2048 and so on and so forth you get the idea of how data is sorted out uh in addition i will be making a an illustration of how sockets work because most of you pointed out that you could not understand and i do agree that was my mistake apologies there all right so now uh if we don't want to create a a loop here that's also something that is not very smart to do what we can do is we can say since we have received we now need to decode the message because remember the message is encoded in ascii uh to save time because the the decoding will take a w it will take a while um depending on the amount of data we can close the socket because we already uh sorry uh client okay keep on using s because that's what we used to use as the object name so client socket uh and in here we would specify uh the fact that we are closing the socket so close and then we can finally say we can finally decode so we can print this out we can say print and we can say message dot decode and we know how to do that that is ascii that is what we are decoding and that should be the tcp client now of course i'll be commenting this and adding documentation before i upload it to github so you guys can understand what's going on all right so i'm currently on windows and the way we're going to test this is i have one of my virtual machines running one that i could get up set up really quickly which is uh ubuntu so that's going to be our client so what i'm going to do is i'm going to transfer the tcp client dot the the python file onto this virtual machine so let me just do that right now uh there it is tcp client so i'm just going to drag it across as that would be the easiest way of doing it so i think i have python 3 installed it would be a real bummer if i didn't sorry about that uh for some reason my virtual machine all right so we have it on the desktop so let me just open up my terminal here and uh desktop um let's list the files yeah there we are so python3 hopefully have it installed i should have because i use this for web development uh so tcp client and uh let's hit enter to see if it works uh and for some reason it is listening that's weird did we already start this server or was it already running because i think i had it running when i was testing it uh anyway don't worry uh we'll let us try and run this again we can run the server now in the terminal so i'm going to run it here through visual studio code and yes it has received it twice um so there's something wrong there so what i'm going to do is let's stop this and let's change the data here just to make to see if it works all right so um thank you for connecting to the server uh this is an example of how sockets can be used all right so something just we have added some more data there it should be able to handle this uh so let's see what we have here uh this was just added a few moments ago that's weird all right so let me just run this in the terminal so yes this the server is running that was really weird uh let me just see if this is working we should be able to get it uh all right so we see it's working that's how the message was transferred i think i'd ran this before now when i was testing the bytes there the amount of bytes that can be sent uh using uh you know uh all right so i get what happened so i did perform the run i i ran it once and i specified the host that was testing the port scanner sorry as i said i was working on another script and i was testing the port scanner and i was testing it on this client because this is the only virtual machine i have so you can see that it was successful we were we were able to uh to get the message hello thank you for connecting to the server this is an example of how sockets can be used now of course this does not look very complicated but we have in essence uh we have been able to send a message across now again a lot of you suggested that i create a chat application and i was thinking about that and you know what i think i will have that as one of the next projects that we'll be working on that'll really make it awesome especially if it was an instant messenger type of chat um so now let's see if we got the the message that was supposed to be displayed on the server and yes we did we go received connection from uh 192.168.1.106 and uh you see you can see this is the correct syntax let me just make that a bit better we can use that again and um let me just kill that session there let's run this in the terminal again and uh that is wasting or listening for a connection let me run that again and let's see uh how yes so received a connection from 192.168.1.106 so that worked perfectly and again you feel free to customize how you want to uh to specify the host uh or the server ip and also i'll be i'll be also tweaking this uh to give you extra options of uh of automatically setting the the server ip in the in the tcp client so for those of you asking why there weren't any additional features like encryption added uh as i said this is a gradual process we will be learning how to incorporate the most important modules from the python standard library and how to create really awesome scripts as i said so you can consider these really good examples for you to learn how everything works with uh with sockets so that was how to create a tcp uh tcps client and tcp server not really uh you know awesome mind-blowing stuff but it's it's a step in the right direction into uh you know for you to understand how uh how to use you know important modules like the socket in python and obviously to get accustomed to doing this with python 3 which is going to be you know without a doubt the future of python so that being said guys that's going to be it for this video you can find both of these on my github repository so i'll be posting that in the description if you found value in this video please leave a like down below if you have any questions or suggestions let me know in the comment section on my social networks or you can ask your personalized questions on my website so i'll be seeing you in the next video [Music] peace
Original Description
Hey guys! hackerSploit here back again with another Python tutorial series, where we will learn how to create Network Sockets, TCP clients, servers and custom scripts.
Github Repository: https://github.com/AlexisAhmed
⭐Help Support HackerSploit by using the following links:
🔗 NordVPN: https://nordvpn.org/hacker
Use the link above or the code below for 77% Off your order
Promo Code: hacker
Patreon: http://patreon.com/hackersploit
I Hope you enjoy/enjoyed the video.
If you have any questions or suggestions feel free to ask them in the comments section or on my social networks.
🔗 HackerSploit Website: https://hsploit.com/
🔗 HackerSploit Android App: https://play.google.com/store/apps/details?id=com.hsploitnews.hsploit&hl=en
🔹 Support The Channel
NordVPN Affiliate Link: https://nordvpn.org/hacker
Patreon: http://patreon.com/hackersploit
🔹 Get Our Courses
Get a special discount on our courses:
The Complete Deep Web Course 2018:
https://www.udemy.com/the-complete-deep-web-course-2017/?couponCode=DWCBP2017
🔹 SOCIAL NETWORKS - Connect With Us!
-------------------------------
Facebook: https://www.facebook.com/HackerSploit/
Instagram: https://www.instagram.com/alexi_ahmed/
Twitter: https://twitter.com/HackerSploit
Patreon: http://patreon.com/hackersploit
--------------------------------
Thanks for watching!
Благодаря за гледането
Kiitos katsomisesta
Aitäh vaatamast!
感谢您观看
Merci d'avoir regardé
Grazie per la visione
Gracias por ver
شكرا للمشاهدة
دیکھنے کے لیے شکریہ
देखने के लिए धन्यवाद
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from HackerSploit · HackerSploit · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
How To Install Kali Linux 2.0 On Virtual Box
HackerSploit
100 Subscriber Q&A! - How I Learned Ethical Hacking
HackerSploit
BlackArch Linux Review - Better Than Kali Linux?
HackerSploit
How to Access the Deep Web Safely | Deep Web Starter Guide 1.0
HackerSploit
Wireshark Tutorial for Beginners - Installation
HackerSploit
Wireshark Tutorial for Beginners - Overview of the environment
HackerSploit
Wireshark Tutorial for Beginners - Capture options
HackerSploit
Wireshark Tutorial for Beginners - Filters
HackerSploit
Complete Ethical Hacking Course - Become a Hacker Today - #1 Hacking Terminology
HackerSploit
Complete Ethical Hacking Course #2 - Installing Kali Linux
HackerSploit
Parrot OS 3.5 Review | The Best Kali Linux Alternative
HackerSploit
Nmap Tutorial For Beginners - 1 - What is Nmap?
HackerSploit
Katoolin | How To Install Pentesting Tools On Any Linux Distro
HackerSploit
Nmap Tutorial For Beginners - 2 - Advanced Scanning
HackerSploit
Nmap Tutorial For Beginners - 3 - Aggressive Scanning
HackerSploit
Zenmap Tutorial For Beginners
HackerSploit
How To Setup Proxychains In Kali Linux - #1 - Stay Anonymous
HackerSploit
How To Setup Proxychains In Kali Linux - #2 - Change Your IP
HackerSploit
How To Change Mac Address In Kali Linux | Macchanger
HackerSploit
How To Setup And Use anonsurf On Kali Linux | Stay Anonymous
HackerSploit
Ubuntu 17.04 "Zesty Zapus" Review - Bye Unity
HackerSploit
VPN And DNS For Beginners | Kali Linux
HackerSploit
Tails OS Installation And Review - Access The Deep Web/Dark Net
HackerSploit
Steganography Tutorial - Hide Messages In Images
HackerSploit
The Lazy Script - Kali Linux 2017.1 - Automate Penetration Testing!
HackerSploit
Best Linux Distributions For Penetration Testing
HackerSploit
Netcat Tutorial - The Swiss Army Knife Of Networking - Reverse Shell
HackerSploit
Gaining Access - Web Server Hacking - Metasploitable - #1
HackerSploit
Web Server Hacking - FTP Backdoor Command Execution With Metasploit - #2
HackerSploit
How To Install Kali Linux On VMware - Complete Guide 2018
HackerSploit
Q&A #1 - Best Cyber-security Certifications?
HackerSploit
Terminator - Kali Linux - Multiple Terminals
HackerSploit
Shodan Search Engine Tutorial - Access Routers,Servers,Webcams + Install CLI
HackerSploit
Q&A #2 - Mr Robot?
HackerSploit
Metasploit Community Web GUI - Installation And Overview
HackerSploit
Linux Expl0rer - Forensics Toolbox - Installation & Configuration
HackerSploit
QuasarRAT - The Best Windows RAT? - Remote Administration Tool for Windows
HackerSploit
Metasploit For Beginners - #1 - The Basics - Modules, Exploits & Payloads
HackerSploit
Metasploit For Beginners - #2 - Understanding Metasploit Modules
HackerSploit
Kali Linux Quick Tips - #1 - Adding a non-root user
HackerSploit
Metasploit For Beginners - #3 - Information Gathering - Auxiliary Scanners
HackerSploit
Spectre Meltdown Vulnerability - How To Check Your System
HackerSploit
Metasploit For Beginners - #4 - Basic Exploitation
HackerSploit
ARP Spoofing With arpspoof - MITM
HackerSploit
WordPress Vulnerability Scanning With WPScan
HackerSploit
Generating A PHP Backdoor with weevely
HackerSploit
Nikto Web Vulnerability Scanner - Web Penetration Testing - #1
HackerSploit
How To Install Kali Linux On Windows 10 - Windows Subsystem For Linux
HackerSploit
Stacer - System Optimizer And Monitoring Tool For Linux
HackerSploit
Kali Linux 2018.1 - Kernel Updates & Patches
HackerSploit
MITM With Ettercap - ARP Poisoning
HackerSploit
Password Cracking With John The Ripper - RAR/ZIP & Linux Passwords
HackerSploit
How To Detect Rootkits On Kali Linux - chkrootkit & rkhunter
HackerSploit
Channel Updates - How To Post Questions & Video Suggestions
HackerSploit
Web App Penetration Testing - #1 - Setting Up Burp Suite
HackerSploit
Web App Penetration Testing - #2 - Spidering & DVWA
HackerSploit
Cl0neMast3r - GitHub Repository Cloning Tool
HackerSploit
Kali Linux On Windows 10 Official - WSL - Installation & Configuration
HackerSploit
DoS/DDoS Protection - How To Enable ICMP, UDP & TCP Flood Filtering
HackerSploit
Web App Penetration Testing - #3 - Brute Force With Burp Suite
HackerSploit
More on: Tool Use & Function Calling
View skill →Related Reads
📰
📰
📰
📰
AI And The Rise Of The Bit Economy: A Structural Shift
Forbes Innovation
2026 Is the Year Everyone Is Redesigning Themselves. Are You?
Medium · AI
EU tech chief and Tim Cook hold ‘constructive’ talks as Siri AI stays blocked in Europe
The Next Web AI
Sonnet 5 launches: Opus performance at lower cost
Dev.to · The Dev Signal
🎓
Tutor Explanation
DeepCamp AI