SPLOIT: How To Build a Peer to Peer Chat Application in Python ( GUI - Linux )

How To Build a Peer to Peer Chat Application in Python ( GUI - Linux )

Developing GUI apps in python is really cool. I ask those who understand the language to help convert some of the command-line or console programs or apps ( I mean programs that are useful to a hacker ) to GUI. I know GUI makes us lazy and not wanna learn but we should all know its fast. Today's tutorial is on p2p chat app I developed last week and decided to share it with the community since its kinda cool.

Not to waste much time. Lets design the UI ( User Interface )

STEP 1: Designing The User Interface

If you followed tutorial Developing A PyGeoIP Gui - Linux, I walked you through the installation of the necessary tools needed to develop GUI apps in python using PyQt.

  • Fire Up Qt Designer
Image via wonderhowto.com
  • Create a MainWindow

Having that opened, You should be presented with a startup dialog ( Unless you disable it, It will show up always ). Click on the MainWindow and create a new form.

Image via wonderhowto.com
  • Main Form

We should now be presented with a blank form.

Image via wonderhowto.com
  • Adding Widgets or Objects
Image via imgur.com
  1. 3 Frames ( frame )
  2. 2 Labels ( label)
  3. 2 Textboxes ( lineEdit )
  4. 2 Buttons ( Send Message and Clear Logs )
  5. A Menu Bar with the text: Menu Actions
  • Has A Sub item labelled: Version
  • Has A Sub item labelled: Exit
  • Arranging The Widgets
  1. MENU BAR: Click on the menu bar, You should see Type Here. Click on it and type Menu Actions and press enter ( Return Key ). Now to add the subitems, Simply click on the Menu Actions, A dropdown list will appear with another Type Here, Write Version and press enter ( Return Key ). Once again, Click on Menu Actions and below the Version subitem, Write Exit in the Type Here field and press enter key.
Image via imgur.com
  1. FRAME 1: Drag a frame onto the form from the left toolbox, Drag 2 labels, 2 lineEdits ( TextBox ).

Name the first label as IP Address: and the second Nick: .

Align the first textEdit ( TextBox ) to the first label ( "IP Address: ") and the second textEdit ( TextBOox ) to the second label ("IP Address: ").

Image via imgur.com
  1. FRAME 2: Drag another frame below the first frame. Make the width of the second frame half the width of the MainWindow so that we can add a third frame. Now drag a textEdit ( Rich Text Box ) onto the second frame from the left toolbox. Make space between the end of the textEdit and the bottom of the second frame so that we can put the buttons there. Add two PushButtons ( Button ) to the frame. The first button will be labelled Send Message and the second Clear Logs.
Image via imgur.com
  1. FRAME 3: Drag a third frame onto the form beside the second frame, remember we made a space for it. Drag a listWidget onto the third frame.
Image via imgur.com

Making a mistake will lead to code displacement or misplacement.

UPDATE: I made a mistake myself, change the objectName of the Send Message button to pushButton_3 and that of the Clear Logs button to pushButton_4. To change the objectName, click on the button, Look at the right side of the Qt Framework, You should see a property box. It has some keys and values. Usually, the objectName is the first.

STEP 2: Compiling

Save the project <Ctrl> + <S>. I saved mine as chat.ui. After we compile it into a .py file. Don't close the Designer, we are gonna need it.

  • #> pyuic4 -x chat.ui -o chat.py
Image via imgur.com

STEP 3: Inserting Our Code

Open your chat.py and you should know there are gonna be a lot of codes in there so don't freak out yet. We only need to edit some specific parts and be out of here.

Image via imgur.com
  • Modify The Import Statements

Change the import statements to these.

Image via imgur.com
  • Creating A BackGround Server Process

When we look at the codes, we will see or note a function inside the generated codes. retranslateUI, Just search if you can't find it. Now, define the function in the image below under the retranslateUI function. As if it was part of the generated codes.

Image via imgur.com

It calls a function called server_socket and passes the parameter self to it. self simply allows the receiving function to access widgets or objects within the self which in our case is the form. Now,

Ok, Put this code, below the import statements at the top.

Image via imgur.com

NOTE: Not inside the generated code or under the retranslateUI function, but beneath the import statements at the very top

the server_socket simply sets up the socket, binds, listens and accept incoming messages and connections. If you noticed, I place some sort of authentication in the server accept function. This will prevent others who are not connected to your chat to send you messages. When you receive a message, Your computer will beep. The code is print "\a". Simple and small right.

  • Making A Client Socket

Define this function client_send_message(self) under the start_server function which is also under the retranslateUI function within the generated function.

NOTE: Please change the client port from 9000 to 6190 ... I was actually experimenting with the app to check if it was working. I am limited in resources right now. A single linux computer.

Image via imgur.com
  • Clearing Logs

Write this function clear_logs above or below the retranslateUI function, Anyway you want it.

Image via imgur.com

ERROR: Forgive me, This tutorial is a long one so I am feeling frustrated myself.

  • Clear line 154 and line 155 in the image above.
  • In the start_server function we defined, clear the last line:

self.pushButton.setEnabled(False)

STEP 3: Calling The Functions

  • We call the start_server function on start up of the GUI. We therefore put it on the first line of the generated code, Under setupUI ( Use the search function in your editors to find setupUI(self, ... ). We put self.start_server() on the first line.
Image via imgur.com
  • Look for pushButton_3 and put this code after the last line of the pushButton_3. Do same for pushButton_4.

Write only the codes within the hash tags and also check not to misplace them. I mean write only the codes with the .clicked.connect

Image via imgur.com
  • Scroll down and look for actionExit
Image via imgur.com

STEP 4: Executing

Once again, I am running in a limited environment. This application cannot be run twice at the same instance on the same machine.Because the server utilizes the port 6190, another application cannot use the same port. For that, I am gonna create a seperate server to receive the message and another client to send messages.

  • Our GUI Application
Image via imgur.com
  • My Special Server
Image via imgur.com
  • My Special Client
Image via imgur.com

Step 4: Encryption
Setup your own encryption.

Step 6: Conclusion

You should know the app will run fine if the application is on different machines. Thanks for reading and hope you learnt sometime today. And also: my waist really aches. See you guys later.. Peace

You can copy the code from pastebin. I copied it there for those that had errors since I am sure someone will by all means not get something. If you find an error also, Do post it. Thanks for reading.

Link To Source Code

Image via wonderhowto.com

# Sergeant

Just updated your iPhone? You'll find new emoji, enhanced security, podcast transcripts, Apple Cash virtual numbers, and other useful features. There are even new additions hidden within Safari. Find out what's new and changed on your iPhone with the iOS 17.4 update.

17 Comments

Really love your python series. Interesting, innocent ways to teach useful capabilities of the language, as a ruby guy trying to learn python this is quite useful.

Thanks for your contributions.

Welcome ... Ruby and python have some similar syntax. You will get it with time. You made my day. :)

I like your writing style (for programming and for teaching). I'm not ready to learn this yet, but I'll save it for later :)

Thanks and do gimme a hint if it worked for you.

nice, specially with the most used tools, sometimes u need to access it fast but u gotta bother with the commands, i guess we can start a new project on null byte,,,we can build a kali linux version full of gui tools, or completely gui..

Yeah ... GUI is fast, Within a couple of clicks, You've hacked NSA ,, so cool. But I will to say, Command line is recommended when running tools that need efficiency and resources.

sure, command line are efficient, and i prefer it too, and it makes u more effective too, while gui can lead u to a lazy land

Hmm. I'm having a bit of a problem. At the beginning of the tutorial, I try to create a new "Main Window" But when I click "Create", A diolog pops up and it says:

Read error
The temporary form file /tmp/i23233.ui could not be written.

And then it lets me push an "OK" button, which then just takes me to the beginning setup. I am not sure how to fix this, and it is halting my progression due to the fact that I must have a Main Window. lol

Ok, So I haven't personally had a problem like that, And i think neither the Internet ( Or i didn't find any help with that ). Or are you running in a limited user account of some sort.

Can you be more specific ?

Hello everyone

Sergean T Sploit I been starting last month in python and moved to the PyQT designer. I pretend to make an app for a college project. At the moment I would like to know, how to apply the stuff you have here, without needing to add anything to the ui file, so that way I can alter the the ui incase I need to (basically all it would have to do would be to convert from .ui to .py again).

Because I am new to scripting, I might be missing some logic in my code, due to the lack of practice with the python syntax.

Hope you or anyone can help me :), many thanks for the tutorial by the way.

There seems to be an issue with the code. After both machines have connected, when you try to send a message, it only shows the first letter of the user and no message goes through. Has anyone been able to fix this?

Same here, I can't tell what the issue is

Hi, really nice post. Thanks a lot.

can you tell me how to include the user logins into this? So only after logging in I will be able to chat with my contacts.

I've wrote your code step by step or even copy/paste on Pycharms. It just shows Server started successfully. Nothing else. It doesn't show the main window

I'm in need of a big HELP !!!!!

I've just one problem left. How to run the code ?
I'm confused here.
Someone mention it.
Thanks

In a terminal just run, python2 chat.py

Hello everyone i have a error NameError: name 'QApplication' is not defined
Please Help Me Thanks

Share Your Thoughts

  • Hot
  • Latest