<commit_before>#!/usr/bin/env python3.3
# -*- coding: utf8 -*-
#
# Terminal component of CVE-Scan. Takes a list of scanned hosts as
# input and uses the terminal to displays this information in a
# graphical manner, enhancing it with information from the CVE-Search
# database and ToolsWatch Default Password Enumeration (DPE) list.
#
# Copyright (c) 2015	NorthernSec
# Copyright (c) 2015	<NAME>
# This software is licensed under the Original BSD Licence

# Imports
import os
import sys
runpath = os.path.dirname(os.path.realpath(__file__))

import curses
from time import sleep
from Toolkit import make_dict, enhance

# Class
class TermDisplay():
  @classmethod
  def start(self, systems=None):
    try:
      scr=curses.initscr()
      curses.start_color()
      curses.init_pair(1, curses.COLOR_RED,curses.COLOR_BLACK)
      curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK)
      md_b   =curses.A_BOLD | curses.color_pair(1)
      md_bt  =curses.A_BOLD | curses.color_pair(2)
      mb_cont=curses.A_BOLD | curses.A_BLINK
      curses.curs_set(0)
    except Exception as ex:
      curses.endwin()
      raise Exception(ex)

    # Init sub functions
    def printWelcome():
      scr.clear()
      scr.border(1)
      scr.addstr( 2,5," _____ _   _ _____      _____                 ",md_b)
      scr.addstr( 3,5,"/  __ \ | | |  ___|    /  ___|                ",md_b)
      scr.addstr( 4,5,"| /  \/ | | | |__ _____\ `--.  ___ __ _ _ __  ",md_b)
      scr.addstr( 5,5,"| |   | | | |  __|_____|`--. \/ __/ _` | '_ \ ",md_b)
      scr.addstr( 6,5,"| \__/\ \_/ / |___     /\__/ / (_| (_| | | | |",md_b)
      scr.addstr( 7,5," \____/\___/\____/     \____/ \___\__,_|_| |_|",md_b)
      scr.addstr( 8,5,"                            (c) NorthernSec   ",md_bt)
      scr.addstr(20,5,"             [Press the any key]              ",mb_cont)
      scr.refresh()
      scr.getch()
    def product(banner):
      if banner:
        r=make_dict(banner)
        return r['product']
      else:
        return 'Unknown'

    def getLinesFor(sys):
      cpes=sys['cpes'] if 'cpes' in sys else ['Not Detected']
      mac=sys['mac'] if sys['mac'] else 'Unknown'
      cont=[({'f':'IP',     'v':sys['ip']}),
            ({'f':'MAC',    'v':mac}),
            ({'f':'Status', 'v':sys['status']}),
            ({'f':'CPEs',   'v':cpes[0]})]
      if len(cpes)>1:
        for cpe in cpes[1:]:
          cont.append({'f':' ', 'v':cpe})
      cont.append({'f':'Vendor', 'v':sys['vendor']})
      hosts=sys['hostnames'] if 'hostnames' in sys else ['None']
      cont.append({'f':'Hostnames', 'v':hosts[0]})
      for host in hosts[1:]:
        cont.append({'f':' ',  'v':host})
      cont.append({'f':'Distance', 'v':sys['distance']})
      serv=sys['services'] if 'services' in sys else ['No services found']
      s=serv[0]
      ser='%s (%s/%s) is %s'%(s['name'],s['port'],s['protocol'],s['state'])
      cont.append({'f':'Services', 'v':ser})
      cont.append({'f':' ', 'v':' > %s'%product(s['banner'])})
      for s in serv[1:]:
        ser='%s (%s/%s) is %s'%(s['name'],s['port'],s['protocol'],s['state'])
        cont.append({'f':' ', 'v':ser})
        cont.append({'f':' ', 'v':' > %s'%product(s['banner'])})

      return cont


    def home():
      ind=0
      while True:
        maxy,maxx=scr.getmaxyx()
        scr.clear()
        sys=systems[ind]
        lines=getLinesFor(sys)
        for i,line in enumerate(lines):
          scr.addstr(i+2,2,line['f'])
          scr.addstr(i+2,14,str(line['v']))
        scr.addstr(maxy-2,2,("(j)ump to | (n)ext | (p)revious | (c)ommand  [%s]")%ind)
        x=scr.getch()
        if x==ord('n'):
          ind+=1
          if ind>=len(systems):ind=0
        elif x==ord('p'):
          ind-=1
          if ind<0:ind=len(systems)-1
        else:
          scr.addstr(2,2,("%s %s %s"%(x, chr(x), ord('j'))))
          scr.getch()
          break

    try:
      printWelcome()
      home()
      curses.endwin()
    except Exception as ex:
      curses.endwin()
      raise Exception(ex)
<commit_msg>Dynamic header + up/down scroll<commit_after>#!/usr/bin/env python3.3
# -*- coding: utf8 -*-
#
# Terminal component of CVE-Scan. Takes a list of scanned hosts as
# input and uses the terminal to displays this information in a
# graphical manner, enhancing it with information from the CVE-Search
# database and ToolsWatch Default Password Enumeration (DPE) list.
#
# Copyright (c) 2015	NorthernSec
# Copyright (c) 2015	<NAME>
# This software is licensed under the Original BSD Licence

# Imports
import os
import sys
runpath = os.path.dirname(os.path.realpath(__file__))

import curses
from time import sleep
from Toolkit import make_dict, enhance

# Class
class TermDisplay():
  @classmethod
  def start(self, systems=None):
    try:
      scr=curses.initscr()
      curses.start_color()
      curses.init_pair(1, curses.COLOR_RED,curses.COLOR_BLACK)
      curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK)
      md_b   =curses.A_BOLD | curses.color_pair(1)
      md_bt  =curses.A_BOLD | curses.color_pair(2)
      mb_cont=curses.A_BOLD | curses.A_BLINK
      mb_head=curses.A_BOLD | curses.color_pair(2)
      curses.curs_set(0)
    except Exception as ex:
      curses.endwin()
      raise Exception(ex)

    # Init sub functions
    def printWelcome():
      scr.clear()
      scr.border(1)
      maxy,maxx=scr.getmaxyx()
      if maxy<10:raise("Please make sure your terminal has at least 10 rows")
      anykey=int(((maxy-7)/2+8))
      start=int((maxx/2)-24)
      scr.addstr(2,start," _____ _   _ _____      _____                 ",md_b)
      scr.addstr(3,start,"/  __ \ | | |  ___|    /  ___|                ",md_b)
      scr.addstr(4,start,"| /  \/ | | | |__ _____\ `--.  ___ __ _ _ __  ",md_b)
      scr.addstr(5,start,"| |   | | | |  __|_____|`--. \/ __/ _` | '_ \ ",md_b)
      scr.addstr(6,start,"| \__/\ \_/ / |___     /\__/ / (_| (_| | | | |",md_b)
      scr.addstr(7,start," \____/\___/\____/     \____/ \___\__,_|_| |_|",md_b)
      scr.addstr(8,start,"                            (c) NorthernSec   ",md_bt)
      scr.addstr(anykey,start,"             [Press the any key]              ",mb_cont)
      scr.refresh()
      scr.getch()
    def product(banner):
      if banner:
        r=make_dict(banner)
        return r['product']
      else:
        return 'Unknown'

    def getLinesFor(sys):
      cpes=sys['cpes'] if 'cpes' in sys else ['Not Detected']
      mac=sys['mac'] if sys['mac'] else 'Unknown'
      cont=[({'f':'IP',     'v':sys['ip']}),
            ({'f':'MAC',    'v':mac}),
            ({'f':'Status', 'v':sys['status']}),
            ({'f':'CPEs',   'v':cpes[0]})]
      if len(cpes)>1:
        for cpe in cpes[1:]:
          cont.append({'f':' ', 'v':cpe})
      cont.append({'f':'Vendor', 'v':sys['vendor']})
      hosts=sys['hostnames'] if 'hostnames' in sys else ['None']
      cont.append({'f':'Hostnames', 'v':hosts[0]})
      for host in hosts[1:]:
        cont.append({'f':' ',  'v':host})
      cont.append({'f':'Distance', 'v':sys['distance']})
      serv=sys['services'] if 'services' in sys else ['No services found']
      s=serv[0]
      ser='%s (%s/%s) is %s'%(s['name'],s['port'],s['protocol'],s['state'])
      cont.append({'f':'Services', 'v':ser})
      cont.append({'f':' ', 'v':' > %s'%product(s['banner'])})
      for s in serv[1:]:
        ser='%s (%s/%s) is %s'%(s['name'],s['port'],s['protocol'],s['state'])
        cont.append({'f':' ', 'v':ser})
        cont.append({'f':' ', 'v':' > %s'%product(s['banner'])})

      return cont


    def home():
      ind=0
      contInd=0
      while True:
        maxy,maxx=scr.getmaxyx()
        maxCont=maxy-6
        scr.clear()
        sys=systems[ind]
        content=getLinesFor(sys)
        lines=list(content)
        if len(content)>maxCont:
          lines=lines[contInd:contInd+maxCont]
        for i,line in enumerate(lines):
          scr.addstr(i+2,2,line['f'],mb_head)
          scr.addstr(i+2,14,str(line['v']))
        scr.addstr(maxy-2,2,("(j)ump to | (n)ext | (p)revious | (c)ommand  [%s]")%ind)
        x=scr.getch()
        if x==ord('n'):
          ind+=1
          if ind>=len(systems):ind=0
        elif x==ord('p'):
          ind-=1
          if ind<0:ind=len(systems)-1
        elif x==ord('u'):
          if contInd>0:contInd-=1
        elif x==ord('d'):
          if (len(content)-contInd)>maxCont:contInd+=1
        else:
          scr.addstr(2,2,("%s %s %s"%(x, chr(x), ord('j'))))
          scr.getch()
          break

    try:
      printWelcome()
      home()
      curses.endwin()
    except Exception as ex:
      curses.endwin()
      raise Exception(ex)

{'fraction_non_alphanumeric': 0.18990857539636616, 'fraction_numerical': 0.013655826871889827, 'mean_word_length': 2.354813664596273, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7282302', 'n_tokens_mistral': 3259, 'n_tokens_neox': 3131, 'n_words': 610}