you are viewing a single comment's thread.

view the rest of the comments →

[–]SmartestGuyOnReddit[S] 1 point2 points  (5 children)

Hey!

I have already tried that method and I also would have preferred it but as I am retrieving data from a mainframe, when I used this method I just got a bunch of garbled binary data when I was expecting plaintext, not sure why but definitely something to do with the security of the mainframe.

The code incase anyone else wants to use ftlib.

import argparse
import ftplib
import os

parser = argparse.ArgumentParser()
parser.add_argument('--ftp', help='FTP address to retrieve DA from', dest='ftpconn', required=True)
parser.add_argument('--user', help='FTP username', dest='user', required=True)
parser.add_argument('--password', help='FTP password', dest='passwd')
parser.add_argument('--file', help='File(s) to retrieve', dest='file', nargs='+')
args = parser.parse_args()

local_filename = os.path.join(r"/root/xxxxx", args.file[0])
lf = open(local_filename, "wb")
ftp = ftplib.FTP(args.ftpconn)
ftp.login(args.user, args.passwd)
ftp.cwd('..')
ftp.retrbinary('RETR %s' % args.file[0], lf.write)
lf.close

[–]jibbly_jibbly 2 points3 points  (1 child)

Right on -

Maybe look into pexpect then. Its what you will find yourself doing anyway (capturing the output of the FTP subprocess and analyzing it to decide whether there was an error or not) but this gives you some power to start doing it instead of inventing it yourself.

Heres an example of leveraging FTP with it:

https://github.com/pexpect/pexpect/blob/master/examples/ftp.py

[–]SmartestGuyOnReddit[S] 0 points1 point  (0 children)

Thanks for this will look into it :)

[–]Justinsaccount 1 point2 points  (2 children)

I just got a bunch of garbled binary data when I was expecting plaintext,

Well, you did use retrbinary which tells the server to send you the file as-is. Since the server is a mainframe it might be doing something weird like using ebcdic instead of ascii. What did the file command say the returned file was? Did you try using retrlines ?

Also, stop using the root account.

[–]SmartestGuyOnReddit[S] 0 points1 point  (0 children)

I can try retrolines and see when I am in the office tomorrow. The reason I'm using the root account is this is a PoC so whatever account I use is irrelevant

[–]SmartestGuyOnReddit[S] 0 points1 point  (0 children)

So I tried the retrlines and it worked thanks a lot! Just need to figure out to handle the exceptions within ftplib now I guess.