Python Forum
How to send output to stout?
Thread Rating:
  • 2 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to send output to stout?
#1
I'm very new to learning python, but as part of a wargames/learning challenge, may require me to brute force a PIN number password in an linux application (I don't even know if it's a 4-digit number or any characters, but that's another story)

Just trying to learn more Python as I go...


The application is run like this:

./application <4-digit code>


I found an example Python script online using "pexpect" that allows you to control other programs, so that made sense for my scenario since I want to run this specific binary so I'm working on customizing that script example.

I'm sure I have lots more coding to do but the current issue is that I just can't see what the script is doing. I know it's running because if I set a large (9999 instead of 10) it hangs so assume it's working but I'm not able to see the actual input/output on the screen.

What would I need to add in order for this to show me each login attempt and allow  me to also see the "Wrong Answer" output from the application when I enter a wrong PIN?


import pexpect as pe

p = pe.spawn('/home/leviathan6/leviathan6')

for i in range(10):
        p.send(str(i))
        p.send('\n')
        p.expect(pe.EOF)
Also, if there is something better than pexpect that I should consider in this case please let me know.

Thanks
Reply
#2
Hey natv,

From what I can read in the docs, you should specify a logfile after spawning your leviathan6 program.

Something like
p.logchild = open('/tmp/leviathan6-log', 'w') #you could name it whatever you want
Regarding sending that program's standard output to the terminal where you're running pexpect, maybe this could work:

import sys

#Your code here including the spawn call

p.logchild = sys.stdout
I'm just talking out loud, sort of speak, here. Haven't tested any of this, but maybe this can get you going a bit further.

PS: is that from the overthewire challenges?

Cheers!
Reply
#3
I always find it helpful to include 'print' commands at various points in the script in order to see if things are actually what they are meant to be.

Quote:What would I need to add in order for this to show me each login attempt and allow  me to also see the "Wrong Answer" output from the application when I enter a wrong PIN?
You might use a 'try/except' statement.
Quote:Also, if there is something better than pexpect that I should consider in this case please let me know.
The only thing that comes to mind at the moment is Pythons built-in 'subprocess'
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply
#4
(Sep-28-2016, 01:01 PM)Crimson King Wrote: I'm just talking out loud, sort of speak, here. Haven't tested any of this, but maybe this can get you going a bit further.

PS: is that from the overthewire challenges?

Cheers!


 
Thanks I'll try that!

Yes this is an overthewire challenge, and I now think brute forcing the PIN is prob not the solution for this particular challenge - but I'm set on at least figuring out how to build a small brute forcer in Python to learn how and have it handy for future challenges  :)
Reply
#5
Hello,

I wrote a class that toggles stdio from within a script.
Be aware that it

see the __name__ clause for usage
save the following as 'ToggleStdio.py'


# add
import sys

class ToggleStdio:
    def __init__(self, filename=None):
        self.savestdout = None
        self.stdio_redirected = False
        if filename:
            self.savefile = filename

    def toggle_stdout(self):
        if self.stdio_redirected:
            sys.stdout = self.savestdout
            self.savestdout = None
            self.stdio_redirected = False
        else:
            self.savestdout = sys.stdout
            sys.stdout = open(self.savefile, 'w')
            self.stdio_redirected = True

if __name__ == '__main__':
    ts = ToggleStdio('mystdio.txt')
    ts.toggle_stdout()
    print('This should go to file')
    ts.toggle_stdout()
    print('This should go to screen')
    ts.toggle_stdout()
    print('added to file')
    print('more added to file')
    ts.toggle_stdout()
    print('more on screen')
To use from your class, import ToggleStdio
then use as in __name__

Larz60+
Reply
#6
Good place for a context manager, assuming not threading issues.
Reply
#7
Hello,

My post was based on the thread title, but is really inappropriate here.
I'll leave it here, but will make a copy on the snippets thread.

Larz60+
Reply
#8
Larz60+ I appreciate your post, I had to just add that to my notes for later though (still in the infancy of my Python training and haven't learned anything about classes yet :)


For now was just hoping to put together a simple standalone script.


I tried some of the suggestions above but haven't been able to get anything to log. I am getting something to display in stdout but it's what my script is sending to the app (1, 2, 3, etc) and now what the app is resulting back which is what I also wanted to see.

Maybe there's something other than pexpect that works better for this purpose I don't know yet.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Send The output of Gyroscope sensor to node red jenkins43 1 2,062 Feb-07-2019, 11:00 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020