pythonpython-3.xexceptionsubprocessformatexception

How can I open a C-file in Python using 'subprocess' without triggering an Exception?


I am trying to open a C-file in Python by using the "subprocess"-module. I cannot execute the program without triggering a 'FileNotFoundError', and if I put in the full path of the file, I get an 'Exec format error'. I don't necessarily need to use the 'subprocess'-module, I just don't know of any other method.

The Python-Skript:

#!/usr/bin/env python
import subprocess

shellcode_file = "shellcode.bin"

try:
    with open(shellcode_file, "rb") as f:
        shellcode = f.read();
    
    subprocess.call(["script.c", shellcode])
except FileNotFoundError as e:
    print(e, "not found.")

I am pretty sure that I am opening the File wrong but I couldn't find a way to fix it.


Solution

  • You are passing the C source file itself, not the compiled executable (*.exe).

    For further info if needed, C is not an interpreted/scripted language, so it cannot be run through its code file like other scripting languages (e.g. Python, Batch). It's a compiled language, meaning that it has to be run through a compiler to generate a separate file (almost always a .exe file) that you can then run your program with.

    Compile your C program first and pass the executable file.

        subprocess.call(["script.exe", shellcode])