-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfileGenerator.ts
More file actions
173 lines (141 loc) · 5.18 KB
/
fileGenerator.ts
File metadata and controls
173 lines (141 loc) · 5.18 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* File Generator
* Generates markdown content for host documentation files
*/
import { ParsedHost, ScanInfo } from "./nmapParser";
interface FileGeneratorSettings {
includeHostnames: boolean;
includeOsDetection: boolean;
includeMacAddress: boolean;
}
export class FileGenerator {
private app: any;
private settings: FileGeneratorSettings;
constructor(app: any, settings: FileGeneratorSettings) {
this.app = app;
this.settings = settings;
}
/**
* Generate markdown content for a host
*/
generateHostContent(host: ParsedHost, scanInfo?: ScanInfo): string {
const lines: string[] = [];
// Get best OS guess (highest accuracy)
const bestOs = this.getBestOsGuess(host);
// Title with OS if available
if (this.settings.includeOsDetection && bestOs) {
lines.push(`# Guessed OS: ${bestOs}`);
}
lines.push("");
// Host Information
lines.push("## Host Information");
lines.push("");
lines.push(`- **IP Address:** ${host.ipAddress}`);
lines.push(`- **Status:** ${host.status}`);
if (this.settings.includeHostnames && host.hostname) {
lines.push(`- **Hostname:** ${host.hostname}`);
}
if (this.settings.includeMacAddress && host.macAddress) {
lines.push(`- **MAC Address:** ${host.macAddress}`);
if (host.vendor) {
lines.push(`- **Vendor:** ${host.vendor}`);
}
}
// Add guessed OS to host info section as well
if (this.settings.includeOsDetection && bestOs) {
lines.push(`- **Guessed OS:** ${bestOs}`);
}
lines.push("");
// OS Detection
if (this.settings.includeOsDetection && host.osMatches && host.osMatches.length > 0) {
lines.push("## Operating System");
lines.push("");
for (const os of host.osMatches) {
lines.push(`- ${os.name} (${os.accuracy}% confidence)`);
}
lines.push("");
}
// Open Ports
if (host.ports.length > 0) {
lines.push("## Open Ports");
lines.push("");
lines.push("| Port | Protocol | Service | Version |");
lines.push("|------|----------|---------|---------|");
for (const port of host.ports) {
const version = this.formatVersion(port);
lines.push(
`| ${port.portId} | ${port.protocol} | ${port.service} | ${version} |`
);
}
lines.push("");
// Detailed port information
lines.push("### Port Details");
lines.push("");
for (const port of host.ports) {
lines.push(`#### ${port.portId}/${port.protocol} - ${port.service}`);
lines.push("");
if (port.product) {
lines.push(`- **Product:** ${port.product}`);
}
if (port.version) {
lines.push(`- **Version:** ${port.version}`);
}
if (port.extraInfo) {
lines.push(`- **Extra Info:** ${port.extraInfo}`);
}
lines.push("");
}
} else {
lines.push("## Open Ports");
lines.push("");
lines.push("*No open ports detected*");
lines.push("");
}
// Scan Information
if (scanInfo) {
lines.push("## Scan Information");
lines.push("");
if (scanInfo.type) {
lines.push(`- **Scan Type:** ${scanInfo.type}`);
}
if (scanInfo.startTime) {
const date = new Date(scanInfo.startTime * 1000);
lines.push(`- **Scan Time:** ${date.toLocaleString()}`);
}
if (scanInfo.args) {
lines.push(`- **Command:** \`${scanInfo.args}\``);
}
lines.push("");
}
// Notes section for user
lines.push("## Notes");
lines.push("");
lines.push("*Add your notes here*");
lines.push("");
return lines.join("\n");
}
/**
* Format version string from port info
*/
private formatVersion(port: { product?: string; version?: string }): string {
const parts: string[] = [];
if (port.product) {
parts.push(port.product);
}
if (port.version) {
parts.push(port.version);
}
return parts.length > 0 ? parts.join(" ") : "-";
}
/**
* Get the best OS guess (highest accuracy) from osMatches
*/
private getBestOsGuess(host: ParsedHost): string | null {
if (!host.osMatches || host.osMatches.length === 0) {
return null;
}
// Sort by accuracy descending and get the best one
const sorted = [...host.osMatches].sort((a, b) => b.accuracy - a.accuracy);
return sorted[0].name;
}
}