Python - Data Sent Over Socket Appears Different on Client and Server - python

I've got a client/server program where the client sends plaintext to the server which then runs AES encryption and returns the ciphertext. I'm using the following algorithm for the encryption/decryption:
http://anh.cs.luc.edu/331/code/aes.py
When I get the results back from the encryption and print them on the server-side I see mostly gibberish in the terminal. I can save to a file immediately and get something along these lines:
tgâY†Äô®Ø8ί6ƒlÑÝ%ŠIç°´>§À¥0Ð
I can see that this is the correct output because if I immediately decrypt it on the server, I get the original plaintext back. If I run this through the socket, send it back to the client, and print(), I get something more like this:
\rtg\xe2Y\x86\x8f\xc4\xf4\xae\xd88\xce\xaf6\x83l\xd1\xdd%\x8aI\xe7\xb0\xb4>\xa7\xc0\x18\xa50\xd0
There's an obvious difference here. I'm aware that the \x represents a hex value. If I save on the client-side, the resulting text file still contains all \x instances (i.e., it looks exactly like what I displayed directly above). What must I do to convert this into the same kind of output that I'm seeing in the first example? From what I have seen so far, it seems that this is unicode and I'm having trouble...
Relevant code from server.py
key = aes.generateRandomKey(keysizes[len(key)%3])
encryptedText = aes.encryptData(key, text)
f = open("serverTest.txt", "w")
f.write(encryptedText)
f.close()
print(encryptedText)
decryptedText = aes.decryptData(key, encryptedText)
print(decryptedText)
conn.sendall(encryptedText)
Relevant code from client.py
cipherText = repr(s.recv(16384))[1:-1]
s.close()
cipherFile = raw_input("Enter the filename to save the ciphertext: ")
print(cipherText)
f = open(cipherFile, "w")
f.write(cipherText)
Edit: To put this simply, I need to be able to send that data to the client and have it display in the same way as it shows up on the server. I feel like there's something I can do with decoding, but everything I've tried so far doesn't work. Ultimately, I'll have to send from the client back to the server, so I'm sure the fix here will also work for that, assuming I can read it from the file correctly.
Edit2: When sending normally (as in my code above) and then decoding on the client-side with "string-escape", I'm getting identical output to the terminal on both ends. The file output also appears to be the same. This issue is close to being resolved, assuming I can read this in and get the correct data sent back to the server for decrypting.

Not sure I fully understood what you're up to, but one difference between client and server is that on the client you're getting the repr for the byte string, while on the server you print the byte string directly.
(if I got the issue right) I'd suggest replacing
repr(s.recv(16384))[1:-1]
with a plain
s.recv(16384)

Related

Long string output problems with input function when readline library is imported in Python, in virsh console of the device

I have a python configuration script, the problem occurs when I import readline library and enter the console of the device. I get the input from the user this way:
val = input(display_str).strip()
This piece of code works perfectly but in the console of the device, long strings are not formatted properly and looks like it puts a limitation how long the string is and prints then on top of each-other. This makes the string unreadable and looks like this:
D/E,GATEWAY or N to delete : in format A:B:C:D:E:F:G:H/K,GATEWAY A:A:B:B:C:C:D:D
when in reality it needs to be formatted like this:
Enter eth1 IPv6 static routes in format A:B:C:D:E:F:G:H/K,GATEWAY A:A:B:B:C:C:D:D/E,GATEWAY or N to delete :
The formatting works perfectly when I ssh or use monitor but when I connect with console it mixes. When I remove the library everything works smoothly.
I found another solution to this problem which is using:
sys.stdout.write(display_str)
val = input().strip()
However here another problem occurs, the user can move left to the beginning of the string and delete the display_str and is not suitable. Other than then it works normally in every mode. Is there a solution for the input function or for the sys.stdout

Unicode printing wrong characters

Unfortunately still using Python 2.7 and moving to Python 3 currently is not in the roadmap.
I am making a REST call to an external application to retrieve data which I need to then process, push into a CSV file and post that across another different REST interface.
The characters causing me problems I have receive from the first rest call are
u'\xe7\x83\xad\xe7\x82\xb9\xe8\xae\xa8\xe8\xae\xba'
which when printed are
>>> print u'\xe7\x83\xad\xe7\x82\xb9\xe8\xae\xa8\xe8\xae\xba'
热点讨论
What this should actually be is 热点讨论
Now this data is taken and then processed without change but is written into the csv file as '热点讨论' and then posted to the second REST interface
The actual characters I should be receiving from the CSV file should be
热点讨论
which in unicode should look like u'\u70ed\u70b9\u8ba8\u8bba'
>>> print u'\u70ed\u70b9\u8ba8\u8bba'
热点讨论
How do I either:
transform the initially received unicode u'\xe7\x83\xad\xe7\x82\xb9\xe8\xae\xa8\xe8\xae\xba' into u'\u70ed\u70b9\u8ba8\u8bba' before pushing into the CSV file and posting to the second rest api
Cause u'\xe7\x83\xad\xe7\x82\xb9\xe8\xae\xa8\xe8\xae\xba' to print 热点讨论 correctly once on the receive side of the second REST api?
Edit: for the first REST call.
The call is being made using requests.get()
This is the response data:
[
{"flags":1074200578,"results":{"id1":22212362104,"id2":22212362125,"fields":[{"id1":0,"id2":0,"count":0,"format":128,"type":"ip.src","flags":0,"group":0,"value":"172.18.16.74"},{"id1":0,"id2":0,"count":0,"format":65,"type":"filename","flags":0,"group":0,"value":"WinVNC6"},{"id1":0,"id2":0,"count":0,"format":65,"type":"checksum","flags":0,"group":0,"value":"5b52bc196bfc207d43eedfe585df96fcfabbdead087ff79fcdcdd4d08c7806db"},{"id1":22212362104,"id2":22212362104,"count":0,"format":32,"type":"time","flags":0,"group":0,"value":1618530308},{"id1":22212362106,"id2":22212362106,"count":0,"format":128,"type":"device.ip","flags":0,"group":0,"value":"172.18.21.197"},{"id1":22212362108,"id2":22212362108,"count":0,"format":65,"type":"device.type","flags":0,"group":0,"value":"symantecav"},{"id1":22212362111,"id2":22212362111,"count":0,"format":65,"type":"host.src","flags":0,"group":0,"value":"\u00E7\u0083\u00AD\u00E7\u0082\u00B9\u00E8\u00AE\u00A8\u00E8\u00AE\u00BA"},{"id1":22212362114,"id2":22212362114,"count":0,"format":65,"type":"checksum","flags":0,"group":0,"value":"5b52bc196bfc207d43eedfe585df96fcfabbdead087ff79fcdcdd4d08c7806db"},{"id1":22212362117,"id2":22212362117,"count":0,"format":65,"type":"filename","flags":0,"group":0,"value":"WinVNC6"},{"id1":22212362119,"id2":22212362119,"count":0,"format":65,"type":"action","flags":0,"group":0,"value":"Left alone"},{"id1":22212362124,"id2":22212362124,"count":0,"format":65,"type":"user.dst","flags":0,"group":0,"value":"Adam_Joe"},{"id1":22212362125,"id2":22212362125,"count":0,"format":128,"type":"ip.src","flags":0,"group":0,"value":"172.18.16.74"},{"id1":22212362104,"id2":22212362104,"count":1,"format":8,"type":"time","flags":2,"group":0,"value":"1"}]}},
{"flags":1074200577,"results":{"id1":22212362126,"id2":22212362136,"fields":[]}}
]
The piece in this causing the issues is "\u00E7\u0083\u00AD\u00E7\u0082\u00B9\u00E8\u00AE\u00A8\u00E8\u00AE\u00BA"

Why do I get IndexError: list index out of range

To explain you my Problem, my Client sends a username and password...
The Client format it like this
file = salt + username + "splitme" + salt + password
encrypt file via tls (when connecting to server)
Send to Server
server decrypts the file and trys to split at "splitme"
CODE:
data = data.split("splitme", 1)
username_salted = data[0]
password_salted = data[1]
And then Python stops the program and says
password_salted = data[1]
IndexError: list index out of range
I don't know whats the problem ...
Because data is a list of 1 element, since the first element access did not raise an error.
Did you decode data? Did it succeed?
data probably does not contain "splitme", in which case you get a list with the whole content you tried to split as the only element.
Have you verified the contents of data? Do you have access to the source code? Can you run it? In that case, you can either change the code for some extra logging, run a debugger such as ipdb3 or use the trace module to see what gets executed.

I am receiving \\r\\n on carriage return instead of \r\n

The project uses sockets to read from a connecting client until either there is no more characters to be read or it receives a \r\n. Here is a snippet of the code:
while True:
ch = connection.recv(1)
data += ch.decode('UTF-8')
if data.endswith('\r\n') or not ch:
data = data.replace('\r\n','')
break
The code works as intended when windows is used to run the server that reads from clients. However when I try to run it on a raspberry pi running rasbian, it always reads a carriage return as '\\r\\n'. For example when a client sends:
-list_networks wlan0 5180<return>
yields a string looking like:
-list_networks wlan0 5180\\r\\n
Why is this? Because of this it does not get read as a carriage return and is missed. I know different OS return different of strings for carriage return but I didn't find anything about this string when I researched a bit. Am I missing something? Suggestions and explanations are appreciated.
edit:
Forgot to add the command is sent through a telnet connection. I want to be able to connect to the socket via telnet. Type a command and when the enter key is pressed the command the loop will recognize and end the loop.

Facing issue while writing data into a file in python script

I have to write 7231 bytes into a file using python script. In a client-server program, my python script act like client and it received 7231 bytes from server. If I check in TCP-Dump, its shows complete data. But when I try to write into a file; I am missing the content.
My script:
def SendOnce(self, req='/gpsData=1',method="GET"):
conn = httplib.HTTPConnection(self.proxy)
self.Logresponse("\nConnection Open\n<br />")
conn.request(method,req)
Log="\nRequest Send: %s\n<br \>\n" %req
self.Logresponse(Log)
response = conn.getresponse()
Log = "\nResponse Code: %s\n<br \>\n" %response.status
self.Logresponse(Log)
Log = "\nSarav -- Get Header: %s \n version= %s <br \>\n" %(response.msg,response.version)
self.Logresponse(Log)
if (response.status==200):
Log = response.read()
self.Logresponse(Log)
conn.close()
self.Logresponse("\nConnection Close\n<br \>")
return response
this "self.Logresponse(Log)" is writing into file. If i receive 1023 bytes, its writing full content into that. Please help me out how to write complete data.
Note: I am writing Hexa Format data.
First of all, 7231 bytes is not exactly huge...
With the limited info you gave, I would guess that you might have forgotten to take the OS's write buffer into account. You probably try to read the file before all the content was written to it.
Python generally uses the system's standard buffer (you can change that). You can decrease that buffer, or force a flush yourself.
I'm just guessing, it might be that the .read() function doesn't return all data in one chunk; can you try to modify the inner part like this:
if (response.status==200):
while 1:
Log = response.read()
if not Log:
break
self.Logresponse(Log)

Categories

Resources