2

I have a Python program which connects to an iPhone and pulls off the my_app.app but it also explores the .app folder to find the binary name as sometimes they can be different. my_app.app doesn't mean the binary is called my_app. Frustrating.

So I grab the binary name, and then my program that used to work, suddenly stops working, it seems when pulling the binary name from iPhone back to Mac, it inserts an ESC character, or ASCII char 27 at the start and end of the binary string. I thought I could fix this with str[1:-1] but I'm still having problems.

Is there a way in Python to strip this out, or to convert it, or replace it to a proper character so it can be removed?

example: once I have pulled the binary file from the iPhone I want to run file on it, however the presence of the ESC char at the end, means the shell cannot find the file.

EDIT: Added some code for further troubleshooting in case it isn't the ESC char

#grabbing the binary name
binName = dlg.actListBox.GetStringSelection()
binName = binName.replace('\x27', '')
binName = binName[1:-1]

#trying to run file
cmd = "file " + appBinPath + "/" + binName
p = subprocess.Popen(cmd, shell=True,stdout=subprocess.PIPE)
stdout = p.communicate()[0]
self.progressBox.AppendText(stdout)


#output I get in program is:
 /Users/p/Documents/ios/output/myapp/myapp_decrypt/Payload/myapp.app/myapp: cannot open `/Users/p/Documents/ios/output/myapp/myapp_decrypt/Payload/myapp.app/myapp' (No such file or directory)

However if I copy the path above, and type file [path] into a terminal and press Enter, I first receive a terminal beep tone (I'm guessing due to the ASCII 27 char) but on the second press the file command works perfect, so that path is certainly correct.

#manual file cmd with same path as used by program
file /Users/p/Documents/ios/output/myapp/myapp_decrypt/Payload/myapp.app/myapp

/Users/p/Documents/ios/output/myapp/myapp_decrypt/Payload/myapp.app/myapp: Mach-O 64-bit executable

SOLVED: Using the repr() function outlined below in the comments allowed us to see what was in the string. Comment from PM2Ring that ESC is \x1b and a standard .replace() solved this

P Shaw
  • 121
  • 2
  • 14

1 Answers1

1

The ESC character is ASCII 27, which is 0x1B in hexadecimal. So to strip the ESC chars from the binName string you can do

binName = binName.replace('\x1b', '')

As Martijn Pieters mentioned in the comments it's generally better to avoid shell=True in subprocess calls unless you specifically need shell features. Instead, pass the command name and the command args as a list of strings and use shell=False, or just don't bother passing a shell arg, since False is the default. Please see Actual meaning of 'shell=True' in subprocess and questions linked from that page for further info.

Community
  • 1
  • 1
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182