-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter.py
148 lines (120 loc) · 5.37 KB
/
formatter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import os
from latex import build_pdf
'''
Takes results objects and outputs them in a nice format.
'''
class Formatter:
def __init__(self, results):
self.results = results
def __repr__(self):
return self.__str__()
def __str__(self):
return 'ResultsFormatter'
# print to stdout
def print(self):
print('Summary:')
print(f'UDP Ports Scanned: {sum(map(lambda hr: sum(map(lambda x: x.port.proto == "udp", hr.portResults)), self.results.hostResults.values()))}')
print(f'TCP Ports Scanned: {sum(map(lambda hr: sum(map(lambda x: x.port.proto == "tcp", hr.portResults)), self.results.hostResults.values()))}')
print(f'Total Ports Scanned: {sum(map(lambda hr: len(hr.portResults), self.results.hostResults.values()))}')
print(f'Open Ports: {sum(map(lambda hr: sum(map(lambda x: "open" in x.status, hr.portResults)), self.results.hostResults.values()))}')
print('')
print('Details:')
for hostR in sorted(self.results.hostResults.values(), key=lambda hr: hr.host):
print(hostR.host)
for portR in sorted(hostR.portResults, key=lambda r: r.port.port):
print(f' {portR.port.port}\\{portR.port.proto}'.ljust(20) + portR.status)
if portR.status == 'trace':
if type(portR.raw) == dict and portR.host in portR.raw:
for k in sorted(portR.raw[portR.host].keys()):
print(f' {k}'.ljust(6) + f'{portR.raw[portR.host][k][0]}')
# print to latex document
def latex(self, fname):
def header():
return r'''
\documentclass[paper=a4, fontsize=11pt,twoside]{scrartcl} % KOMA
\usepackage[a4paper,pdftex]{geometry} % A4paper margins
\setlength{\oddsidemargin}{5mm} % Remove 'twosided' indentation
\setlength{\evensidemargin}{5mm}
\usepackage[protrusion=true,expansion=true]{microtype}
\usepackage{amsmath,amsfonts}
\usepackage{graphicx}
\RedeclareSectionCommand[beforeskip=-5.5ex plus -1ex minus -.2ex,afterskip=4.3ex plus -.2ex]{section}
\RedeclareSectionCommand[beforeskip=-5.5ex plus -1ex minus -.2ex,afterskip=4.3ex plus -.2ex]{subsection}
\newcommand{\HRule}[1]{\rule{\linewidth}{#1}} % Horizontal rule
\makeatletter % Title
\def\printtitle{%
{\centering \@title\par}}
\makeatother
\makeatletter % Author
\def\printauthor{%
{\centering \large \@author}}
\makeatother
'''
def cover(title):
return r'''
\title{ \normalsize \textsc{} % Subtitle
\\[2.0cm] % 2cm spacing
\HRule{0.5pt} \\ % Upper rule
\LARGE \textbf{\uppercase{'''+title+r'''}} % Title
\HRule{2pt} \\ [0.5cm] % Lower rule + 0.5cm spacing
\normalsize \today % Todays date
}
\author{
This report was\\
generated from the git repo:\\
idraper/port\_scanner\\
}
\begin{document}
% ------------------------------------------------------------------------------
% Maketitle
% ------------------------------------------------------------------------------
\thispagestyle{empty} % Remove page numbering on this page
\printtitle % Print the title data as defined above
\vfill
\printauthor % Print the author data as defined above
\newpage
% ------------------------------------------------------------------------------
% Begin document
% ------------------------------------------------------------------------------
\setcounter{page}{1} % Set page numbering to begin on this page
'''
def done():
return r'''
\end{document}
'''
document = ''
document += header()
document += cover('Port Scan Report')
# print summary report
document += '\n\\section*{Summary}\n\n'
document += '{\n\LARGE\n'
document += r'\begin{tabular}{ll}' + '\n'
document += f'UDP Ports Scanned: & {sum(map(lambda hr: sum(map(lambda x: x.port.proto == "udp", hr.portResults)), self.results.hostResults.values()))} \\\\\n'
document += f'TCP Ports Scanned: & {sum(map(lambda hr: sum(map(lambda x: x.port.proto == "tcp", hr.portResults)), self.results.hostResults.values()))} \\\\\n'
document += f'Total Ports Scanned: & {sum(map(lambda hr: len(hr.portResults), self.results.hostResults.values()))} \\\\\n'
document += f'Open Ports: & {sum(map(lambda hr: sum(map(lambda x: "open" in x.status, hr.portResults)), self.results.hostResults.values()))} \n'
document += r'\end{tabular} \vfill' + '\n'
document += '}\n\n'
# print table with details report
document += '\\section*{Details}\n\n'
for hostR in sorted(self.results.hostResults.values(), key=lambda hr: hr.host):
document += f'\\subsection*{{{hostR.host}}}\n\n'
document += r'\begin{tabular}{lll}'
document += '\\hline\n'
document += r'Port & Protocol & Status \\'
document += '\\hline\n'
for portR in sorted(hostR.portResults, key=lambda r: r.port.port):
document += f'{portR.port.port} & {portR.port.proto} & {portR.status} \\\\'
if portR.status == 'trace':
if type(portR.raw) == dict and portR.host in portR.raw:
document += '\n' + r'& & \begin{tabular}{ll}'
for k in sorted(portR.raw[portR.host].keys()):
document += f'\n{k} & {portR.raw[portR.host][k][0]} \\\\'
document += r'\end{tabular} \\' + '\n'
document += '\\hline\n'
document += r'\end{tabular} \vfill' + '\n\n'
document += done()
# create and open the pdf
with open(fname, 'wb') as file:
file.write(bytes(build_pdf(document)))
os.system(f'start {fname}')