From 9e09235cfd0c124280a9986dcf72df9eef4e3627 Mon Sep 17 00:00:00 2001 From: Upi Tamminen Date: Sat, 26 Jul 2014 12:02:12 +0300 Subject: [PATCH] add support for -O parameter (fixes #131) --- kippo/commands/wget.py | 52 ++++++++++++++++++++++++++++++------------ 1 file changed, 38 insertions(+), 14 deletions(-) diff --git a/kippo/commands/wget.py b/kippo/commands/wget.py index ce18937..84cf1eb 100644 --- a/kippo/commands/wget.py +++ b/kippo/commands/wget.py @@ -5,8 +5,15 @@ from kippo.core.honeypot import HoneyPotCommand from kippo.core.fs import * from twisted.web import client from twisted.internet import reactor -import stat, time, urlparse, random, re, exceptions + +import stat +import time +import urlparse +import random +import re +import exceptions import os.path +import getopt commands = {} @@ -38,14 +45,16 @@ def splitthousands( s, sep=','): class command_wget(HoneyPotCommand): def start(self): - url = None - for arg in self.args: - if arg.startswith('-'): - continue - url = arg.strip() - break + try: + optlist, args = getopt.getopt(self.args, 'O:') + except getopt.GetoptError, err: + self.writeln('Unrecognized option') + self.exit() + return - if not url: + if len(args): + url = args[0].strip() + else: self.writeln('wget: missing URL') self.writeln('Usage: wget [OPTION]... [URL]...') self.nextLine() @@ -53,14 +62,30 @@ class command_wget(HoneyPotCommand): self.exit() return + outfile = None + for opt in optlist: + if opt[0] == '-O': + outfile = opt[1] + if '://' not in url: url = 'http://%s' % url urldata = urlparse.urlparse(url) - outfile = urldata.path.split('/')[-1] - if not len(outfile.strip()) or not urldata.path.count('/'): - outfile = 'index.html' + if outfile is None: + outfile = urldata.path.split('/')[-1] + if not len(outfile.strip()) or not urldata.path.count('/'): + outfile = 'index.html' + + outfile = self.fs.resolve_path(outfile, self.honeypot.cwd) + path = os.path.dirname(outfile) + if not path or \ + not self.fs.exists(path) or \ + not self.fs.is_dir(path): + self.writeln('wget: %s: Cannot open: No such file or directory' % \ + outfile) + self.exit() + return self.url = url self.limit_size = 0 @@ -228,10 +253,9 @@ class HTTPProgressDownloader(client.HTTPDownloader): (time.strftime('%Y-%m-%d %H:%M:%S'), self.speed / 1000, self.fakeoutfile, self.currentlength, self.totallength)) - outfile = '%s/%s' % (self.wget.honeypot.cwd, self.fakeoutfile) - self.wget.fs.mkfile(outfile, 0, 0, self.totallength, 33188) + self.wget.fs.mkfile(self.fakeoutfile, 0, 0, self.totallength, 33188) self.wget.fs.update_realfile( - self.wget.fs.getfile(outfile), + self.wget.fs.getfile(self.fakeoutfile), self.wget.safeoutfile) return client.HTTPDownloader.pageEnd(self)