-2

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?

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • 2
    Can you show an example of how you'd run this program manually? Then we can talk about how to automate it. – melpomene Jun 06 '19 at 05:23
  • I would do ./program –  Jun 06 '19 at 05:26
  • 1
    That invokes `program` without arguments. Does `program` take arguments or not? – melpomene Jun 06 '19 at 05:27
  • ./program requires arguments. I have tried ./program script.py. But I really don't know how to do it. –  Jun 06 '19 at 05:33
  • maybe something like `while read key do; ./program key; done < ./script.py` – Rorschach Jun 06 '19 at 05:33
  • 1
    Dude, forget about your Python script. How would you normally run the program and pass it a number? – melpomene Jun 06 '19 at 05:34
  • I would just do ./program 5412 for example. I'm bruteforcing this, so I don't know which passcode would be. –  Jun 06 '19 at 05:36
  • Every combination of a 4 digits number are just the numbers from 0000 to 9999. There is no need to use Python at all. Just use seq (https://ss64.com/bash/seq.html) to produce the numbers and apply them with xargs (https://ss64.com/bash/xargs.html). – MisterMiyagi Jun 06 '19 at 05:49
  • Possible duplicate of [How to call a shell script from python code?](https://stackoverflow.com/questions/3777301/how-to-call-a-shell-script-from-python-code) – MisterMiyagi Jun 06 '19 at 05:50

2 Answers2

1

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
melpomene
  • 84,125
  • 8
  • 85
  • 148
-1

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')

H. Farrow
  • 21
  • 3