diff --git a/cowrie/commands/ping.py b/cowrie/commands/ping.py index 3d0bc427..322d2523 100644 --- a/cowrie/commands/ping.py +++ b/cowrie/commands/ping.py @@ -5,6 +5,7 @@ import re import random import hashlib import socket +import getopt from twisted.internet import reactor @@ -23,10 +24,28 @@ class command_ping(HoneyPotCommand): def start(self): self.host = None - for arg in self.args: - if not arg.startswith('-'): - self.host = arg.strip() - break + self.max = 0 + self.running = False + + try: + optlist, args = getopt.getopt(self.args, "c:") + except getopt.GetoptError as err: + self.writeln('ping: %s' % err ) + self.exit() + return + + for opt in optlist: + if opt[0] == '-c': + try: + self.max = int(opt[1]) + except: + self.max = 0 + if self.max == 0: + self.writeln('ping: bad number of packets to transmit.') + self.exit() + return + + self.host = args[0].strip() if not self.host: for l in ( @@ -50,6 +69,7 @@ class command_ping(HoneyPotCommand): self.ip = '.'.join([str(int(x, 16)) for x in (s[0:2], s[2:4], s[4:6], s[6:8])]) + self.running = True self.writeln('PING %s (%s) 56(84) bytes of data.' % \ (self.host, self.ip)) self.scheduled = reactor.callLater(0.2, self.showreply) @@ -60,14 +80,29 @@ class command_ping(HoneyPotCommand): self.writeln( '64 bytes from %s (%s): icmp_seq=%d ttl=50 time=%.1f ms' % \ (self.host, self.ip, self.count + 1, ms)) + self.writeln( "self.max: %d, self.count: %d" % (self.max,self.count)) self.count += 1 - self.scheduled = reactor.callLater(1, self.showreply) + if self.count == self.max: + self.running = False + self.writeln('') + self.printstatistics() + self.exit() + else: + self.scheduled = reactor.callLater(1, self.showreply) - def handle_CTRL_C(self): - self.scheduled.cancel() + def printstatistics(self): self.writeln('--- %s ping statistics ---' % self.host) self.writeln('%d packets transmitted, %d received, 0%% packet loss, time 907ms' % \ (self.count, self.count)) self.writeln('rtt min/avg/max/mdev = 48.264/50.352/52.441/2.100 ms') - self.exit() + + def handle_CTRL_C(self): + if self.running == False: + return HoneyPotCommand.handle_CTRL_C(self) + else: + self.writeln('^C') + self.scheduled.cancel() + self.printstatistics() + self.exit() + commands['/bin/ping'] = command_ping