<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 re
import curses
from time import sleep
from lib.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)
      curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK)
      curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK)
      curses.init_pair(5, curses.COLOR_YELLOW, curses.COLOR_BLACK)
      md_b   =curses.A_BOLD | curses.color_pair(1)
      md_bt  =curses.A_BOLD | curses.color_pair(2)
      md_cont=curses.A_BOLD | curses.A_BLINK
      md_head=curses.A_BOLD | curses.color_pair(2)
      md_norm=curses.color_pair(3)
      md_mark=curses.color_pair(1)
      md_bad=curses.color_pair(1)
      md_med=curses.color_pair(5)
      md_good=curses.color_pair(4)
      curses.curs_set(0)
      curses.noecho()
      scr.keypad(1)
    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]              ",md_cont)
      scr.refresh()
      scr.getch()

    def printHelp(section=None):
      if not section:
        text=[{'t':'----------','m':md_head},
              {'t':'|  HELP  |','m':md_head},
              {'t':'----------','m':md_head},
              {'t':' '},
              {'t':'Navigating','m':md_head},
              {'t':' * You can navigate through the scanned systems with p and n, or the left and right arrow key'},
              {'t':' * You can scroll through the current system with u and d, or the up and down arrow key'},
              {'t':' * You can jump directly to a scanned system by entering the page number with j, and entering the page number.'},
              {'t':' '},
              {'t':'Commands','m':md_head},
              {'t':'By pressing c, you can enter commands:'},
              {'t':'  help                    - Displays this menu'},
              {'t':'  c/cve [port/cpe/banner] - If found, displays CVEs of the current line, or service with the parameter'}]
      f=[' - press q to return to the previous page -']
      scroll(text,footer=f)

    def scroll(text,footer=[],cursor=False,limit=True):
      contInd=0
      cursInd=0
      cursPos=0
      while True:
        maxy,maxx=scr.getmaxyx()
        maxCont=maxy-len(footer)-4
        scr.clear()
        lines=list(text)
        if len(text)>maxCont:
          lines=lines[contInd:contInd+maxCont]

        for i,line in enumerate(lines):
          markup=line['m'] if 'm' in line else md_norm
          if 't' in line:
            scr.addstr(i+2,2,line['t'],markup)
          elif 'f' in line and 'v' in line:
            scr.addstr(i+2,2,line['f'],md_head)
            scr.addstr(i+2,14,str(line['v'])[:maxx-18],markup)
          else:
            scr.addstr(i+2,2,"Line is an invalid format")
        if cursor:
          scr.addstr(cursPos+2,1,">")
        for i,line in enumerate(footer):
          scr.addstr((maxy-len(footer)-1)+i,2,line)

        x=scr.getch()
        if x==ord('u') or x==curses.KEY_UP:
          if cursor:
            if cursInd>0:cursInd-=1
            if cursPos>0:cursPos-=1
            if cursPos==0 and contInd>0:contInd-=1
          else:
            if contInd>0:contInd-=1
        elif x==ord('d') or x==curses.KEY_DOWN:
          if cursor:
            if cursInd<len(text)-1:cursInd+=1
            if cursPos<maxCont and cursPos<len(text)-1:cursPos+=1
            if cursPos==maxCont and (len(text)-contInd)>maxCont:contInd+=1
          else:
            if (len(text)-contInd)>maxCont:contInd+=1
        elif x==ord('q'):
          return ord('q') if limit else (ord('q'),cursInd)
        else:
          if not limit:
            return (x,cursInd)

    def cveView(cves,service=None):
      h=['CVE - Vector - Complexity']
      f=['Vector:     N(etwork) - A(djecent network) - L(ocal)',
         'Complexity: L(ow)     - M(edium)           - H(igh)']
      list=[{'t':'%s - %s %s'%(x['id'], x['access']['vector'][0:1],
                  x['access']['complexity'][0:1])} for x in cves]
      scroll(list,footer=f,cursor=True,limit=False)
      

    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'
      marked=md_mark if 'cves' in cpes[0] else md_norm
      cont=[({'f':'IP',     'v':sys['ip']}),
            ({'f':'MAC',    'v':mac}),
            ({'f':'Status', 'v':sys['status']}),
            ({'f':'CPEs',   'v':cpes[0]['cpe'],       'm':marked})]
      if len(cpes)>1:
        for cpe in cpes[1:]:
          marked=md_mark if 'cves' in cpe else md_norm
          cont.append({'f':' ', 'v':cpe['cpe'], 'm':marked})
      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'])<commit_msg>add header to 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 re
import curses
from time import sleep
from lib.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)
      curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK)
      curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK)
      curses.init_pair(5, curses.COLOR_YELLOW, curses.COLOR_BLACK)
      md_b   =curses.A_BOLD | curses.color_pair(1)
      md_bt  =curses.A_BOLD | curses.color_pair(2)
      md_cont=curses.A_BOLD | curses.A_BLINK
      md_head=curses.A_BOLD | curses.color_pair(2)
      md_norm=curses.color_pair(3)
      md_mark=curses.color_pair(1)
      md_bad=curses.color_pair(1)
      md_med=curses.color_pair(5)
      md_good=curses.color_pair(4)
      curses.curs_set(0)
      curses.noecho()
      scr.keypad(1)
    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]              ",md_cont)
      scr.refresh()
      scr.getch()

    def printHelp(section=None):
      if not section:
        text=[{'t':'----------','m':md_head},
              {'t':'|  HELP  |','m':md_head},
              {'t':'----------','m':md_head},
              {'t':' '},
              {'t':'Navigating','m':md_head},
              {'t':' * You can navigate through the scanned systems with p and n, or the left and right arrow key'},
              {'t':' * You can scroll through the current system with u and d, or the up and down arrow key'},
              {'t':' * You can jump directly to a scanned system by entering the page number with j, and entering the page number.'},
              {'t':' '},
              {'t':'Commands','m':md_head},
              {'t':'By pressing c, you can enter commands:'},
              {'t':'  help                    - Displays this menu'},
              {'t':'  c/cve [port/cpe/banner] - If found, displays CVEs of the current line, or service with the parameter'}]
      f=[' - press q to return to the previous page -']
      scroll(text,footer=f)

    def scroll(text,header=[],footer=[],cursor=False,limit=True):
      contInd=0
      cursInd=0
      cursPos=0
      while True:
        maxy,maxx=scr.getmaxyx()
        maxCont=maxy-len(header)-len(footer)-4
        s=len(header)+2 if len(header)!=0 else 1
        scr.clear()
        lines=list(text)
        if len(text)>maxCont:
          lines=lines[contInd:contInd+maxCont]
        for i,line in enumerate(header):
          scr.addstr(i+1,2,line)
        for i,line in enumerate(lines):
          markup=line['m'] if 'm' in line else md_norm
          if 't' in line:
            scr.addstr(i+s,2,line['t'],markup)
          elif 'f' in line and 'v' in line:
            scr.addstr(i+s,2,line['f'],md_head)
            scr.addstr(i+s,14,str(line['v'])[:maxx-18],markup)
          else:
            scr.addstr(i+s,2,"Line is an invalid format")
        if cursor:
          scr.addstr(cursPos+s,1,">")
        for i,line in enumerate(footer):
          scr.addstr((maxy-len(footer)-1)+i,2,line)

        x=scr.getch()
        if x==ord('u') or x==curses.KEY_UP:
          if cursor:
            if cursInd>0:cursInd-=1
            if cursPos>0:cursPos-=1
            if cursPos==0 and contInd>0:contInd-=1
          else:
            if contInd>0:contInd-=1
        elif x==ord('d') or x==curses.KEY_DOWN:
          if cursor:
            if cursInd<len(text)-1:cursInd+=1
            if cursPos<maxCont and cursPos<len(text)-1:cursPos+=1
            if cursPos==maxCont and (len(text)-contInd)>maxCont:contInd+=1
          else:
            if (len(text)-contInd)>maxCont:contInd+=1
        elif x==ord('q'):
          return ord('q') if limit else (ord('q'),cursInd)
        else:
          if not limit:
            return (x,cursInd)

    def cveView(cves,service=None):
      h=['CVE - Vector - Complexity']
      f=['Vector:     N(etwork) - A(djecent network) - L(ocal)',
         'Complexity: L(ow)     - M(edium)           - H(igh)']
      list=[{'t':'%s - %s %s'%(x['id'], x['access']['vector'][0:1],
                  x['access']['complexity'][0:1])} for x in cves]
      scroll(list,header=h,footer=f,cursor=True,limit=False)
      

    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'
      marked=md_mark if 'cves' in cpes[0] else md_norm
      cont=[({'f':'IP',     'v':sys['ip']}),
            ({'f':'MAC',    'v':mac}),
            ({'f':'Status', 'v':sys['status']}),
            ({'f':'CPEs',   'v':cpes[0]['cpe'],       'm':marked})]
      if len(cpes)>1:
        for cpe in cpes[1:]:
          marked=md_mark if 'cves' in cpe else md_norm
          cont.append({'f':' ', 'v':cpe['cpe'], 'm':marked})
      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'])
{'fraction_non_alphanumeric': 0.1680531231757151, 'fraction_numerical': 0.012988908347927612, 'mean_word_length': 2.2936794039894255, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, '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': '2179472', 'n_tokens_mistral': 4928, 'n_tokens_neox': 4750, 'n_words': 1020}