2

I am using python and I am supposed to read a file from command line for further processing. My input file has a binary that should be read for further processing. Here is my input file sub.py:

   CODE = " \x55\x48\x8b\x05\xb8\x13\x00\x00"

and my main file which should read this is like the following:

import pyvex
import archinfo
import fileinput

import sys
filename = sys.argv[-1]


f = open(sys.argv[-1],"r")
CODE = f.read()
f.close()
print CODE

#CODE = b"\x55\x48\x8b\x05\xb8\x13\x00\x00"
# translate an AMD64 basic block (of nops) at 0x400400 into VEX
irsb = pyvex.IRSB(CODE, 0x1000, archinfo.ArchAMD64())

# pretty-print the basic block
irsb.pp()

# this is the IR Expression of the jump target of the unconditional exit at the end of the basic block
print irsb.next

# this is the type of the unconditional exit (i.e., a call, ret, syscall, etc)
print irsb.jumpkind

# you can also pretty-print it
irsb.next.pp()

# iterate through each statement and print all the statements
for stmt in irsb.statements:
        stmt.pp()

# pretty-print the IR expression representing the data, and the *type* of that IR expression written by every store statement
import pyvex
for stmt in irsb.statements:
  if isinstance(stmt, pyvex.IRStmt.Store):
    print "Data:",
    stmt.data.pp()
    print ""

    print "Type:",
    print stmt.data.result_type
    print ""

# pretty-print the condition and jump target of every conditional exit from the basic block
for stmt in irsb.statements:
        if isinstance(stmt, pyvex.IRStmt.Exit):
                print "Condition:",
                stmt.guard.pp() 
                print "" 

                print "Target:",
                stmt.dst.pp()
                print ""

# these are the types of every temp in the IRSB
print irsb.tyenv.types

# here is one way to get the type of temp 0
print irsb.tyenv.types[0]

The problem is that when I run "python maincode.py sub.py' it reads the code as content of the file but its output is completely different from when I directly add CODE into the statement irsb = pyvex.IRSB(CODE, 0x1000, archinfo.ArchAMD64()). Does anyone know what is the problem and how can I solve it? I even use importing from inputfile but it does not read a text.

sarah123
  • 175
  • 1
  • 7

2 Answers2

1

Have you considered the __import__ way?

You could do

mod = __import__(sys.argv[-1])
print mod.CODE

and just pass the filename without the .py extension as your command line argument:

python maincode.py sub

EDIT: Apparently using __import__ is discouraged. Instead though you can use importlib module:

import sys,importlib
mod = importlib.import_module(sys.argv[-1])
print mod.CODE

..and it should work the same as using __import__

If you need to pass a path to the module, one way is if in each of the directories you added an empty file named

__init__.py

That will allow python to interpret the directories as module namespaces, and you can then pass the path in its module form: python maincode.py path.to.subfolder.sub

If for some reason you cannot or don't want to add the directories as namespaces, and don't want to add the init.py files everywhere, you could also use imp.find_module. Your maincode.py would instead look like this:

import sys, imp
mod  = imp.find_module("sub","/path/to/subfolder/")
print mod.code

You'll have to write code which breaks apart your command line input into the module part "sub" and the folder path "/path/to/subfolder/" though. Does that make sense? Once its ready you'll call it like you expect, python maincode.py /path/to/subfolder/sub/

stackPusher
  • 6,076
  • 3
  • 35
  • 36
  • What if i want to enter input file on command line from a folder?I tried sudo python tnt_test.py \test\sub but it says mod = __import__(sys.argv[-1]) ImportError: Import by filename is not supported. how can I solve this problem? – sarah123 Sep 13 '17 at 19:06
  • see my edited answer. I tried to type it here but kept losing the underscores around the init.py file to stackoverflow's autoformatting... you must name that file with the underscores – stackPusher Sep 13 '17 at 22:28
  • Thank you for your reply. But it did not work for me. I am stopped with error __import__(name) ImportError: Import by filename is not supported – sarah123 Sep 17 '17 at 17:00
  • Did you try both folder solutions? No offense, and I dont want to assume anything, but that error message makes it sound like youre still just trying the first two solutions which only work without folders. If you tried the dotted module solution you'd be calling it like so: 'python maincode.py test.sub' and so it wouldnt give an error about passing in a filename (since you are not passing in a filename anymore). And if you tried the second solution (where you do pass the filename), you would no longer be using importlib, but instead the imp module which doesnt give that error. – stackPusher Sep 18 '17 at 23:11
  • Both solutions are working for me so most likely something was miscommunicated. If you post or send me your latest code which is giving you that error we can find out – stackPusher Sep 18 '17 at 23:11
  • Thank you. My problem was the way i was compiling it. Instead of python maincode.py test.sub I was trying python maincode.py test/sub. Now it is solved. Thank you for your help. – sarah123 Sep 19 '17 at 15:45
  • Thank you for your kindness. – sarah123 Sep 21 '17 at 14:58
0

you're reading the code as text, while when reading as file you're likely reading as binary

you probably need to convert binary to text of vice-versa to make this work

Binary to String/Text in Python

MrE
  • 19,584
  • 12
  • 87
  • 105