Skip to content

Instantly share code, notes, and snippets.

@Brawn1
Last active March 29, 2018 13:36
Show Gist options
  • Save Brawn1/f2d0d7ab293f71f75a20b61db54604da to your computer and use it in GitHub Desktop.
Save Brawn1/f2d0d7ab293f71f75a20b61db54604da to your computer and use it in GitHub Desktop.
Simple NTP Network Scanner to scan class C Network for NTP Server.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#################################################################
# programm to scan and check ntp servers in class C Network #
# requirements: tool ntpdate #
# @author: Bailey #
# @date: 2017-11-28 #
# MIT License #
#################################################################
import json
from os import curdir, sep
from subprocess import Popen, PIPE
from optparse import OptionParser
from time import strftime, time
__version__ = "0.1"
def printlicense():
print("""
MIT License:
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
""")
class Scan():
def __init__(self):
self.debug = False
self.foundlog = curdir+sep+"ntpserver.log"
self.dstore = dict()
self.dstore.update({'ntpserver': list(), 'ntp': 0, 'version': __version__})
def ntpsrvchk(self, cmd):
d = dict()
d.update({'time': strftime("%H:%M:%S"), 'date': strftime("%Y-%m-%d"), 'time': time()})
output = Popen(cmd, stdout=PIPE, stderr=PIPE)
out1, out1error = output.communicate()
if len(out1error)>0:
if self.debug:
print(out1error.decode())
pass
elif len(out1)>0:
for x in out1.decode().split(','):
if x.startswith(' '):
x = x.split(' ')[1:]
else:
x = x.split(' ')
if "server" in x:
d['server'] = x[1].rstrip().replace('\n28','')
elif 'stratum' in x:
d['stratum'] = x[1].rstrip()
elif 'offset' in x:
d['offset'] = x[1].rstrip()
else:
d['optinfo'] = x[1].rstrip()
if self.debug:
print(d)
self.dstore['ntpserver'].append(d)
self.dstore['ntp'] = self.dstore.get('ntp', 0) + 1
def write_result(self):
if self.debug:
print("write file:",self.foundlog)
f = open(self.foundlog, "a")
f.write(json.dumps(self.dstore))
f.close()
def scan_ntp(self, baseip, dbg=False):
self.debug = dbg
for x in range(1,255):
if self.debug:
print("check IP:",baseip+"."+str(x))
cmd = ["ntpdate", "-q", baseip+"."+str(x)]
self.ntpsrvchk(cmd=cmd)
self.write_result()
print(str(self.dstore.get('ntp', 0))+" NTP Server found")
class App():
def __init__(self):
usage = "usage: NTP Network Scan v%s options" %(__version__)
parser = OptionParser(usage=usage)
parser.add_option("--baseip", default=False, dest="baseip", metavar="BASEIP",
help="BaseIP to scan without trailing dot. 192.168.1")
parser.add_option("--debug", default=False, dest="debug",
action="store_false",
help="Optional: Debug output")
parser.add_option("--license", default=False, dest="printlicense",
action="store_true", help="Print MIT License")
(options, args) = parser.parse_args()
if options.printlicense:
printlicense()
elif options.baseip:
if options.debug:
Scan().scan_ntp(baseip=options.baseip, dbg=True)
else:
Scan().scan_ntp(baseip=options.baseip)
else:
parser.print_help()
exit(1)
if __name__ == "__main__":
app = App()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment