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

Aug 4, 2015 04:57 PM
Aug 4, 2015 04:58 PM
635742790150309005.jpg

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

635742666963507401.jpg
  • Main Form

We should now be presented with a blank form.

635742668823194300.jpg
  • Adding Widgets or Objects
635742670965850761.jpg
  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.
635742706830537937.jpg
  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: ").

635742680345538106.jpg
  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.
635742690681163237.jpg
  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.
635742695231787926.jpg

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 + . 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
635742708143977676.jpg

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.

635742710926631474.jpg
  • Modify The Import Statements

Change the import statements to these.

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

635742744217652511.jpg

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.

635742748469215079.jpg

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.

635742752362496470.jpg
  • Clearing Logs

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

635742760163902761.jpg

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

635742775607808908.jpg
  • Scroll down and look for actionExit
635742777239215023.jpg

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
635742782863277938.jpg
  • My Special Server
635742783392810921.jpg
  • My Special Client
635742783702654510.jpg

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

635742786691719872.jpg

# Sergeant

Just updated your iPhone? You'll find new Apple Intelligence capabilities, sudoku puzzles, Camera Control enhancements, volume control limits, layered Voice Memo recordings, and other useful features. Find out what's new and changed on your iPhone with the iOS 18.2 update.

Comments

No Comments Exist

Be the first, drop a comment!