I am trying to execute a command using subprocess.Popen() and extract the result using the following code.
proc = subprocess.Popen([command], stdout=subprocess.PIPE, shell=True)
(out, err) = proc.communicate()
gadgets = out.split('\n')
The result is a string and can be split into several lines by splitting based on the newline characters in the result. For simplicity, let's assume the gadgets variable in code snippet is a list of two strings. When I print an individual item from the list on a console using the python print() function, I am getting normal ascii text as follows.
for item in gadgets:
print(item)
output:
syscall;
xlatb; ret;
However, when I print the list, I am getting a different kind of characters. The output is as follows:
print(gadgets)
output:
['\x1b[1;33msyscall\x1b[0m\x1b[1;34m; \x1b[0m', '\x1b[1;33mxlatb\x1b[0m\x1b[1;34m; \x1b[0m\x1b[1;33mret\x1b[0m\x1b[1;34m; \x1b[0m']
I believe that the print() function somehow changes the encoding and prints normal ascii text on a console for the individual items. Can anyone please tell me what kind of encoding is used by the print() function in this scenario? Thank you!