formatting + debug remove

This commit is contained in:
Michel Oosterhof
2016-09-18 16:45:04 +00:00
parent a98163ce93
commit 57a349d291
2 changed files with 56 additions and 22 deletions

View File

@@ -5,7 +5,6 @@ import stat
import time import time
import random import random
import re import re
import exceptions
import os import os
import getopt import getopt
import hashlib import hashlib
@@ -274,7 +273,7 @@ Options: (H) means HTTP/HTTPS only, (F) means FTP only
port = parsed.port or (443 if scheme == 'https' else 80) port = parsed.port or (443 if scheme == 'https' else 80)
path = parsed.path or '/' path = parsed.path or '/'
if scheme != 'http' and scheme != 'https': if scheme != 'http' and scheme != 'https':
raise exceptions.NotImplementedError raise NotImplementedError
except: except:
self.write('%s: Unsupported scheme.\n' % (url,)) self.write('%s: Unsupported scheme.\n' % (url,))
self.exit() self.exit()

View File

@@ -3,7 +3,6 @@
import time import time
import re import re
import exceptions
import os import os
import getopt import getopt
import hashlib import hashlib
@@ -21,9 +20,14 @@ from twisted.python import log
from cowrie.core.honeypot import HoneyPotCommand from cowrie.core.honeypot import HoneyPotCommand
from cowrie.core.fs import * from cowrie.core.fs import *
"""
"""
commands = {} commands = {}
def tdiff(seconds): def tdiff(seconds):
"""
"""
t = seconds t = seconds
days = int(t / (24 * 60 * 60)) days = int(t / (24 * 60 * 60))
t -= (days * 24 * 60 * 60) t -= (days * 24 * 60 * 60)
@@ -32,25 +36,39 @@ def tdiff(seconds):
minutes = int(t / 60) minutes = int(t / 60)
t -= (minutes * 60) t -= (minutes * 60)
s = '%ds' % int(t) s = '%ds' % (int(t),)
if minutes >= 1: s = '%dm %s' % (minutes, s) if minutes >= 1: s = '%dm %s' % (minutes, s)
if hours >= 1: s = '%dh %s' % (hours, s) if hours >= 1: s = '%dh %s' % (hours, s)
if days >= 1: s = '%dd %s' % (days, s) if days >= 1: s = '%dd %s' % (days, s)
return s return s
def sizeof_fmt(num): def sizeof_fmt(num):
"""
"""
for x in ['bytes','K','M','G','T']: for x in ['bytes','K','M','G','T']:
if num < 1024.0: if num < 1024.0:
return "%d%s" % (num, x) return "%d%s" % (num, x)
num /= 1024.0 num /= 1024.0
# Luciano Ramalho @ http://code.activestate.com/recipes/498181/ # Luciano Ramalho @ http://code.activestate.com/recipes/498181/
def splitthousands( s, sep=','): def splitthousands( s, sep=','):
"""
"""
if len(s) <= 3: return s if len(s) <= 3: return s
return splitthousands(s[:-3], sep) + sep + s[-3:] return splitthousands(s[:-3], sep) + sep + s[-3:]
class command_wget(HoneyPotCommand): class command_wget(HoneyPotCommand):
"""
"""
def start(self): def start(self):
"""
"""
try: try:
optlist, args = getopt.getopt(self.args, 'cqO:P:') optlist, args = getopt.getopt(self.args, 'cqO:P:')
except getopt.GetoptError as err: except getopt.GetoptError as err:
@@ -112,7 +130,10 @@ class command_wget(HoneyPotCommand):
self.deferred.addCallback(self.success, outfile) self.deferred.addCallback(self.success, outfile)
self.deferred.addErrback(self.error, url) self.deferred.addErrback(self.error, url)
def download(self, url, fakeoutfile, outputfile, *args, **kwargs): def download(self, url, fakeoutfile, outputfile, *args, **kwargs):
"""
"""
try: try:
parsed = urlparse(url) parsed = urlparse(url)
scheme = parsed.scheme scheme = parsed.scheme
@@ -120,7 +141,7 @@ class command_wget(HoneyPotCommand):
port = parsed.port or (443 if scheme == 'https' else 80) port = parsed.port or (443 if scheme == 'https' else 80)
path = parsed.path or '/' path = parsed.path or '/'
if scheme != 'http' and scheme != 'https': if scheme != 'http' and scheme != 'https':
raise exceptions.NotImplementedError raise NotImplementedError
except: except:
self.write('%s: Unsupported scheme.\n' % (url,)) self.write('%s: Unsupported scheme.\n' % (url,))
self.exit() self.exit()
@@ -141,17 +162,21 @@ class command_wget(HoneyPotCommand):
contextFactory = ssl.ClientContextFactory() contextFactory = ssl.ClientContextFactory()
contextFactory.method = SSL.SSLv23_METHOD contextFactory.method = SSL.SSLv23_METHOD
reactor.connectSSL(host, port, factory, contextFactory) reactor.connectSSL(host, port, factory, contextFactory)
else: #can only be http, since we raised an error above for unknown schemes else: # Can only be http, since we raised an error above for unknown schemes
self.connection = reactor.connectTCP( self.connection = reactor.connectTCP(
host, port, factory, bindAddress=out_addr) host, port, factory, bindAddress=out_addr)
return factory.deferred return factory.deferred
def handle_CTRL_C(self): def handle_CTRL_C(self):
self.write('^C\n') self.write('^C\n')
self.connection.transport.loseConnection() self.connection.transport.loseConnection()
def success(self, data, outfile): def success(self, data, outfile):
"""
"""
if not os.path.isfile(self.safeoutfile): if not os.path.isfile(self.safeoutfile):
log.msg("there's no file " + self.safeoutfile) log.msg("there's no file " + self.safeoutfile)
self.exit() self.exit()
@@ -159,7 +184,7 @@ class command_wget(HoneyPotCommand):
shasum = hashlib.sha256(open(self.safeoutfile, 'rb').read()).hexdigest() shasum = hashlib.sha256(open(self.safeoutfile, 'rb').read()).hexdigest()
hash_path = '%s/%s' % (self.download_path, shasum) hash_path = '%s/%s' % (self.download_path, shasum)
# if we have content already, delete temp file # If we have content already, delete temp file
if not os.path.exists(hash_path): if not os.path.exists(hash_path):
os.rename(self.safeoutfile, hash_path) os.rename(self.safeoutfile, hash_path)
else: else:
@@ -178,20 +203,22 @@ class command_wget(HoneyPotCommand):
outfile=hash_path, outfile=hash_path,
shasum=shasum) shasum=shasum)
# link friendly name to hash # Link friendly name to hash
os.symlink( shasum, self.safeoutfile ) os.symlink( shasum, self.safeoutfile )
# FIXME: is this necessary? # FIXME: is this necessary?
self.safeoutfile = hash_path self.safeoutfile = hash_path
# update the honeyfs to point to downloaded file # Update the honeyfs to point to downloaded file
f = self.fs.getfile(outfile) f = self.fs.getfile(outfile)
f[A_REALFILE] = hash_path f[A_REALFILE] = hash_path
self.exit() self.exit()
def error(self, error, url): def error(self, error, url):
print(repr(error)) """
if hasattr(error, 'getErrorMessage'): # exceptions """
if hasattr(error, 'getErrorMessage'): # Exceptions
error = error.getErrorMessage() error = error.getErrorMessage()
self.write(error+'\n') self.write(error+'\n')
# Real wget also adds this: # Real wget also adds this:
@@ -201,7 +228,7 @@ class command_wget(HoneyPotCommand):
commands['/usr/bin/wget'] = command_wget commands['/usr/bin/wget'] = command_wget
commands['/usr/bin/dget'] = command_wget commands['/usr/bin/dget'] = command_wget
# from http://code.activestate.com/recipes/525493/ # From http://code.activestate.com/recipes/525493/
class HTTPProgressDownloader(client.HTTPDownloader): class HTTPProgressDownloader(client.HTTPDownloader):
def __init__(self, wget, fakeoutfile, url, outfile, headers=None): def __init__(self, wget, fakeoutfile, url, outfile, headers=None):
client.HTTPDownloader.__init__(self, url, outfile, headers=headers, client.HTTPDownloader.__init__(self, url, outfile, headers=headers,
@@ -215,13 +242,19 @@ class HTTPProgressDownloader(client.HTTPDownloader):
self.nomore = False self.nomore = False
self.quiet = self.wget.quiet self.quiet = self.wget.quiet
def noPage(self, reason): # called for non-200 responses
def noPage(self, reason): # Called for non-200 responses
"""
"""
if self.status == '304': if self.status == '304':
client.HTTPDownloader.page(self, '') client.HTTPDownloader.page(self, '')
else: else:
client.HTTPDownloader.noPage(self, reason) client.HTTPDownloader.noPage(self, reason)
def gotHeaders(self, headers): def gotHeaders(self, headers):
"""
"""
if self.status == '200': if self.status == '200':
if self.quiet == False: if self.quiet == False:
self.wget.write('200 OK\n') self.wget.write('200 OK\n')
@@ -243,24 +276,25 @@ class HTTPProgressDownloader(client.HTTPDownloader):
self.contenttype)) self.contenttype))
else: else:
if self.quiet == False: if self.quiet == False:
self.wget.write('Length: unspecified [%s]\n' % \ self.wget.write('Length: unspecified [{}]\n'.format(self.contenttype))
(self.contenttype))
if self.wget.limit_size > 0 and \ if self.wget.limit_size > 0 and \
self.totallength > self.wget.limit_size: self.totallength > self.wget.limit_size:
log.msg( 'Not saving URL (%s) due to file size limit' % \ log.msg( 'Not saving URL ({}) due to file size limit'.format(self.wget.url))
(self.wget.url,) )
self.fileName = os.path.devnull self.fileName = os.path.devnull
self.nomore = True self.nomore = True
if self.quiet == False: if self.quiet == False:
self.wget.write('Saving to: `%s\'\n\n' % self.fakeoutfile) self.wget.write('Saving to: `{}\'\n\n'.format(self.fakeoutfile))
return client.HTTPDownloader.gotHeaders(self, headers) return client.HTTPDownloader.gotHeaders(self, headers)
def pagePart(self, data): def pagePart(self, data):
"""
"""
if self.status == '200': if self.status == '200':
self.currentlength += len(data) self.currentlength += len(data)
# if downloading files of unspecified size, this could happen: # If downloading files of unspecified size, this could happen:
if not self.nomore and self.wget.limit_size > 0 and \ if not self.nomore and self.wget.limit_size > 0 and \
self.currentlength > self.wget.limit_size: self.currentlength > self.wget.limit_size:
log.msg( 'File limit reached, not saving any more data!' ) log.msg( 'File limit reached, not saving any more data!' )
@@ -273,7 +307,7 @@ class HTTPProgressDownloader(client.HTTPDownloader):
return client.HTTPDownloader.pagePart(self, data) return client.HTTPDownloader.pagePart(self, data)
if self.totallength: if self.totallength:
percent = (self.currentlength/self.totallength)*100 percent = (self.currentlength/self.totallength)*100
spercent = "%i%%" % percent spercent = "{}%".format(percent)
else: else:
spercent = '%dK' % (self.currentlength/1000) spercent = '%dK' % (self.currentlength/1000)
percent = 0 percent = 0
@@ -291,7 +325,10 @@ class HTTPProgressDownloader(client.HTTPDownloader):
self.lastupdate = time.time() self.lastupdate = time.time()
return client.HTTPDownloader.pagePart(self, data) return client.HTTPDownloader.pagePart(self, data)
def pageEnd(self): def pageEnd(self):
"""
"""
if self.totallength != 0 and self.currentlength != self.totallength: if self.totallength != 0 and self.currentlength != self.totallength:
return client.HTTPDownloader.pageEnd(self) return client.HTTPDownloader.pageEnd(self)
if self.quiet == False: if self.quiet == False:
@@ -313,5 +350,3 @@ class HTTPProgressDownloader(client.HTTPDownloader):
self.wget.fileName = self.fileName self.wget.fileName = self.fileName
return client.HTTPDownloader.pageEnd(self) return client.HTTPDownloader.pageEnd(self)
# vim: set sw=4 et: