Hi

I keep on getting data altered when I receive it

example:

SAM:~ SamarthAgarwal$ telnet 192.168.1.39 8888
Trying 192.168.1.39...
Connected to 192.168.1.39.
Escape character is '^]'.
--Welcome To HACKNET--
Username:
Admin
Password:
pass
--INCORRECT IDENTIFICATION--Connection closed by foreign host.
SAM:~ SamarthAgarwal$

the data on the server side:

('192.168.1.39', 57727) client tried to connect with username: Admin
 and password: pass

the data always has a extra space so i added an extra space to the password
but it still doesn't work.


Code:

#!/usr/bin/env python 
# ***** BEGIN LICENSE BLOCK ***** 
# Version: MPL 1.1 
# 
# The contents of this file are subject to the Mozilla Public License Version 
# 1.1 (the "License"); you may not use this file except in compliance with 
# the License. You may obtain a copy of the License at 
# http://www.mozilla.org/MPL/ 
# 
# Software distributed under the License is distributed on an "AS IS" basis, 
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
# for the specific language governing rights and limitations under the 
# License. 
# 
# The Original Code is HACKNET. 
# 
# The Initial Developer of the Original Code is 
# Samarth Agarwal. 
# Portions created by the Initial Developer are Copyright (C) 2011 
# the Initial Developer. All Rights Reserved. 
# 
# Contributor(s):
# 
# ***** END LICENSE BLOCK *****


import socket
import threading
import shelve
import SETTINGS

class Server:
    def __init__(self):
        self.username = ""
        self.password = ""
        print "Server Running At:", SETTINGS.host        

    
    def login(self):
    
        host = SETTINGS.host
        port = SETTINGS.port 
        backlog = 5 
        size = 1024 
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
        s.bind((host,port)) 
        s.listen(backlog) 
        while 1: 
            client, address = s.accept()
            client.send( SETTINGS.welcome)
            client.send("\nUsername:\n")
            self.username = str(client.recv(size))
            client.send("Password:\n")
            self.password = str(client.recv(size))
            if self.username == SETTINGS.username:
                if self.password == SETTINGS.password:                
                    client.send(("Loged in as :\n", SETTINGS.username))
                    break
            else:
                client.send("--INCORRECT IDENTIFICATION--")
                print client.getpeername(),"client tried to connect with username:",self.username," and password:",self.password
                client.close()
                
        
                


print SETTINGS.password
print SETTINGS.username
server = Server()

server.login()

the SETTINGS module/file:

#HACKNET Settings file

host = "192.168.1.39"
port = 8888


username = "Admim"
password = "pass"

welcome = "--Welcome To HACKNET--"

help

Recommended Answers

All 3 Replies

I think you might be forgetting that your username and password may come in with an appended newline <CR LF>. You should change your validation to:

if self.username == SETTINGS.username + "\r\n":
    if self.password == SETTINGS.password + "\r\n":                
        client.send("Loged in as :\n" + SETTINGS.username)
        break

Side note: Telnet is not quite the same as a plain network connection, because the server attempts to negotiate services with the terminal you are using to provide things like color, etc. I would guess you might get problems with that later if you want to send color, but maybe not.

The telnet protocol includes special characters to send telnet control sequences between the client and the server. Your 'telnet' command most likely sends these characters. You may be able to see them by printing repr(username), repr(password) instead of username and password for example. I don't think you can succeed without learning the telnet protocol first, but... a simpler alternative is to google 'telnet server in python' and browse or download the resulting existing attempts to implement this protocol.

Settings, line 7. Looks like a typo in the username.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.