How can I pass a python script as an argument?

Question:

I’m working on a program that I need to brute force with a 4 pin number in order to get a flag. This is for some Cybersecurity challenges. I was able to create a python script that will print every combination of 4 digits number. However, I don’t know how can I pass that as an argument to the program that I need to brute-force.

I am using Kali linux to run the program and create the script.

#!/usr/bin/python
from itertools import product

numbers = '0123456789' # chars to look for

for length in range(1, 3): # only do lengths of 1 + 2
    to_attempt = product(chars, repeat=4)
    for attempt in to_attempt:
        print(''.join(attempt))

Any thoughts on how can I pass those results as an argument to the program?

Asked By: user10660670

||

Answers:

You can set up the above code as a function and then just import the function into whatever program you are running.

def combinations (numbers=’0123456789′):

for length in range(1, 3): # only do lengths of 1 + 2
    to_attempt = product(chars, repeat=4)
    for attempt in to_attempt:
        print(''.join(attempt))

Then you can just call the file in your other program after you imported it: name_of_file.combinations(numbers=’0123456789′)

Answered By: H. Farrow

You’re not trying to pass a python script as an argument, you’re trying to turn the output of your script into arguments to another program.

There are several ways to do this. Probably the most efficient way is to just invoke the program from your Python script instead of printing output.

However, if you want to iterate over the lines output by some program, you can do it as follows:

python script.py | while read -r; do
    ./program "$REPLY"  # or do whatever you want with "$REPLY"
done

If all you’re doing in the loop is running a single program, passing lines as arguments can also be done with xargs instead:

python script.py | xargs -n1 ./program

You probably don’t need your Python script at all. For example, if you just want a list of all four-digit numbers, you can do

seq -w 0 9999

(The -w option pads 0 to 0000, 1 to 0001, etc.)

Combined with xargs that gives

seq -w 0 9999 | xargs -n1 ./program

Or you could write the whole loop in the shell:

for ((i=0; i<=9999; i++)); do
    # using printf to pad the number to 4 places
    ./program "$(printf '%04d' $i)"
done
Answered By: melpomene

To pass a Python script as an argument to another Python script, you can simply include the filename of the script as an argument when you run the second script.

Here’s an example of how to do this:

# main.py

import sys

if len(sys.argv) < 2:
    print("Please provide a script filename as an argument")
    sys.exit(1)

script_filename = sys.argv[1]

# Run the script
exec(open(script_filename).read())

In this example, sys.argv is a list that contains the command-line arguments passed to the script. The first argument (sys.argv[0]) is the name of the script itself, and the second argument (sys.argv[1]) is the name of the script to be executed.

To run the main.py script with another script as an argument, you would use the following command:

python main.py myscript.py

This would execute the myscript.py script using the exec function. Note that this approach assumes that the script being passed as an argument is in the same directory as the script that is executing it. If the script is in a different directory, you would need to specify the full path to the script file.

Answered By: Travis Uaiz
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.