File: module.js

package info (click to toggle)
bettercap-caplets 0%2Bgit20240106-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,028 kB
  • sloc: javascript: 2,112; sh: 29; makefile: 8; python: 1
file content (108 lines) | stat: -rw-r--r-- 3,119 bytes parent folder | download
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
var enumerated_hosts = []

var red    = "\033[31m",
    yellow = "\033[33m",
    green  = "\033[32m",
    bold   = "\033[1;37m",
    reset  = "\033[0m"

function configure() {
	if ( !readFile( env("enumerate.hosts.output") ) ) {
		log_info("(" + green + "enumerate.hosts" + reset + ") " + bold + "enumerate.hosts.output" + reset + " file was not found, creating one ...")
		writeFile( env("enumerate.hosts.output"), "" )
	}

	if ( !readFile( env("events.stream.output") ) ) {
		log_error("Error: " + bold + "events.stream.output" + reset + " file not found (got " + env("events.stream.output") + ")")
	}
}

function extractHosts() {
	logs = readFile( env("events.stream.output") ).split("\n")
	extracted_hosts = []

	for (var i = 0; i < logs.length; i++) {
		if ( logs[i].match(/\[.*?net\.sniff.*?\]/i) ) {
			host = logs[i].replace(/.*\033\[33m(https:\/\/|)(.*?)\033\[0m.*/g, "$2")
			extracted_hosts.indexOf(host) == -1 ? extracted_hosts.push(host) : ""
		}
	}

	return extracted_hosts
}

function compareHosts(old_hosts, new_hosts) {
	difference = []

	for (var i = 0; i < new_hosts.length; i++) {
		old_hosts.indexOf(new_hosts[i]) == -1 ? difference.push(new_hosts[i]) : ""
	}

	return difference
}

function saveHosts(new_hosts) {
	saved_hosts = readFile( env("enumerate.hosts.output") ).split("\n")

	for (var i = 0; i < new_hosts.length; i++) {
		saved_hosts.indexOf(new_hosts[i]) == -1 ? saved_hosts.push(new_hosts[i]) : ""
	}

	writeFile( env("enumerate.hosts.output"), saved_hosts.join("\n") )
}

function printHosts(hosts) {
	if (hosts.length != 0) {
		log_string = ""

		for (var i = 0; i < hosts.length; i++) {
			log_string += "  " + yellow + hosts[i] + reset + "\n"
			enumerated_hosts.indexOf(hosts[i]) == -1 ? enumerated_hosts.push(hosts[i]) : ""
		}

		console.log("\n" + log_string)
	} else {
		console.log("\n  No hosts to display.\n")
	}
}

function onCommand(cmd) {
	if (cmd == "enumerate.hosts.all") {
		saved_hosts = readFile( env("enumerate.hosts.output") ).split("\n")
		printHosts(saved_hosts)
		return true
	}

	if (cmd == "enumerate.hosts.new") {
		new_hosts = compareHosts( enumerated_hosts, extractHosts() )
		printHosts(new_hosts)
		return true
	}

	if ( cmd.match(/^enumerate\.hosts\.regexp ./) ) {
		regexp = new RegExp( cmd.replace("enumerate.hosts.regexp ", "") )
		saved_hosts = readFile( env("enumerate.hosts.output") ).split("\n")
		found_hosts = []

		for (var i = 0; i < saved_hosts.length; i++) {
			saved_hosts[i].match(regexp) ? found_hosts.push(saved_hosts[i]) : ""
		}

		printHosts(found_hosts)
		return true
	}

	if (cmd == "enumerate.hosts.save") {
		saveHosts( extractHosts() )
		return true
	}
}

function onLoad() {
	console.log("\n" + bold + "  Commands" + reset + "\n")
	console.log("       " + yellow + "enumerate.hosts.all" + reset + " : Enumerate all hosts.")
	console.log("       " + yellow + "enumerate.hosts.new" + reset + " : Enumerate new hosts.")
	console.log("    " + yellow + "enumerate.hosts.regexp" + reset + " : Enumerate hosts with regexp value.\n")
	configure()
	log_info("(" + green + "enumerate.hosts" + reset + ") Module successfully loaded.")
}