Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i am trying to make my own root shell for my server so that i can work on the in side of it, but ever time i try and check if the input == "exit" it just don't check it and continues. sorry is this is spelled wrong, i am not english. i just can't see what i am doing wrong. plus i am trying to have to work with more then one client and will be adding a login and well i need to know how to compare the recv data and check it so that the login works and well i just don't know whats going wrong.

What I have tried:

Python
import socket
import threading
import json
import hashlib
from subprocess import PIPE, Popen

decode_utf8 = lambda data: data.decode("utf-8")

def main():
    s = socket.socket()
    s.bind(('127.0.0.1', 12344))
    s.listen(5)
    while True:
        r, addr = s.accept()
        print("Connected to by {}".format(str(addr)))
        threading.Thread(target=login(r)).start()

def login(r):
    read = open('data.json', 'r')
    data = json.load(read)
    while True:
        input_user = r.recv(1024)
        user = decode_utf8(input_user)
        try:
            password = data[str(user)]['password']
            input_user = r.recv(1024)
            pwd = decode_utf8(input_user)
            passdata = hashlib.sha256(pwd.encode().hexdigest()
            if passdata == password:
                rootShell(r)
            else:
                r.send('Wrong Password or Username')
                r.close()
        except:
            r.send('Wrong Password or Username')
            r.close()

def rootShell(r):
    while True:
        input_data = r.recv(1024)
        data = decode_utf8(input_data)
        print(data)
        try:
            if data.lower().startswith("exit"):
                r.send('Goodbye and see you later')
                r.close()
            else:
                r.send(cmdline(data))
        except:
            r.send("Error Running Command")

def cmdline(command):
    process = Popen(
        args=command,
        stdout=PIPE,
        shell=True
    )
    return process.communicate()[0]

main()
Posted
Updated 11-Apr-19 14:57pm
v4
Comments
Richard MacCutchan 28-Mar-19 11:17am    
Add some code to display exactly what is first received, and what is the result after the call to decode_utf8(input_data). Do not just assume that your code will work.
WOLF 2018 28-Mar-19 11:46am    
i have displayed the work and it looked fine but i still can't compare.

this is me connecting to it

root@Unknown:~# nc 127.0.0.1 12344
whoami
root

this is the python script output

/usr/bin/python2.7 /root/wolf-python-projects/server.py
Connected to by ('127.0.0.1', 39468)
whoami

root is what the server told me i am. (well is right i am root)
Richard MacCutchan 28-Mar-19 12:00pm    
But you have not shown the data that you type in and what Python interprets it as.
WOLF 2018 28-Mar-19 13:10pm    
i typed in whoami

Try this (no guarantees, just a stab in the dark):

Python
if (data.lower() == "exit"):


or maybe

Python
if (data.lower().startswith("exit")):
 
Share this answer
 
v5
Comments
WOLF 2018 28-Mar-19 11:15am    
no i know that i entered exit all lower case. but is just doesn't respond. ps but i did give it a shot
WOLF 2018 28-Mar-19 11:22am    
ok thanks man that second one worked, but why can't i just compare it as a string. because i am going to add a login and well i need to check if the username matches and the password matches
#realJSOP 28-Mar-19 11:33am    
I'm not a python dev (yet). I was just thinking about the different ways a string won't compare the way you think. I bet it's got something to do with you converting it to a UTF8 string before comparing it.
WOLF 2018 28-Mar-19 11:34am    
thats what i think. now i am getting this error

Traceback (most recent call last):
File "/root/wolf-python-projects/server.py", line 41, in <module>
main()
File "/root/wolf-python-projects/server.py", line 15, in main
a = threading.Thread(target=rootShell(r))
File "/root/wolf-python-projects/server.py", line 31, in rootShell
r.send("Error Running Command")
File "/usr/lib/python2.7/socket.py", line 174, in _dummy
raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor
#realJSOP 28-Mar-19 11:38am    
The best I can recommend for that is google, given my lack of experience with python.
I found a way of doing it by converting the bytes to hex and removing the “a0” at from the bytes and then convert it back to a string

data = r.recv(1024)
data = codecs.encode(data, ‘hex’)
data = str(data).replace(‘a0’, ‘’)
return codecs.decode(data, ‘hex’)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900