File: bittorrent-discovery.nse

package info (click to toggle)
nmap 7.40-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 50,080 kB
  • ctags: 26,777
  • sloc: ansic: 98,862; cpp: 64,063; python: 17,751; sh: 14,584; xml: 11,448; makefile: 2,635; perl: 2,585; yacc: 660; lex: 457; asm: 372; java: 45; objc: 43
file content (133 lines) | stat: -rw-r--r-- 3,879 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
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
local stdnse = require "stdnse"
local table = require "table"
local target = require "target"


local bittorrent = stdnse.silent_require "bittorrent"

description = [[
Discovers bittorrent peers sharing a file based on a user-supplied
torrent file or magnet link.  Peers implement the Bittorrent protocol
and share the torrent, whereas the nodes (only shown if the
include-nodes NSE argument is given) implement the DHT protocol and
are used to track the peers. The sets of peers and nodes are not the
same, but they usually intersect.

If the <code>newtargets</code> script-arg is supplied it adds the discovered
peers as targets.
]]

---
-- @usage
-- nmap --script bittorrent-discovery --script-args newtargets,bittorrent-discovery.torrent=<torrent_file>
--
-- @args bittorrent-discovery.torrent a string containing the filename of the torrent file
-- @args bittorrent-discovery.magnet a string containing the magnet link of the torrent
-- @args bittorrent-discover.timeout desired (not actual) timeout for the DHT discovery (default = 30s)
-- @args bittorrent-discover.include-nodes boolean selecting whether to show only nodes
--
-- @output
-- | bittorrent-peers:
-- |   Peers:
-- |     97.88.178.168
-- |     89.100.184.36
-- |     86.185.55.212
-- |     Total of 3 peers discovered
-- |   Nodes:
-- |     68.103.0.189
-- |     67.164.32.71
-- |     24.121.13.69
-- |     207.112.100.224
-- |     Total of 4 nodes discovered
-- |_  Use the newtargets script-arg to add the results as targets
--

author = "Gorjan Petrovski"
license = "Same as Nmap--See https://nmap.org/book/man-legal.html"
categories = {"discovery","safe"}


prerule = function()
  if not stdnse.get_script_args(SCRIPT_NAME..".torrent") and
      not stdnse.get_script_args(SCRIPT_NAME..".magnet") then
    stdnse.debug3("Skipping '%s' %s, No magnet link or torrent file arguments.", SCRIPT_NAME, SCRIPT_TYPE)
    return false
  end
  return true
end

action = function()
  local timeout = stdnse.parse_timespec(stdnse.get_script_args(SCRIPT_NAME..".timeout"))
  local filename = stdnse.get_script_args(SCRIPT_NAME..".torrent")
  local magnet = stdnse.get_script_args(SCRIPT_NAME..".magnet")
  local include_nodes = stdnse.get_script_args(SCRIPT_NAME..".include-nodes")

  local t = bittorrent.Torrent:new()
  if filename then
    t:load_from_file(filename)
  elseif magnet then
    t:load_from_magnet(magnet)
  end
  t:trackers_peers()
  t:dht_peers(timeout)

  local output = {}
  local peers = {}
  peers.name = "Peers:"
  local nodes = {}
  nodes.name = "Nodes:"

  -- add peers
  if target.ALLOW_NEW_TARGETS then
    for peer_ip in pairs(t.peers) do
      target.add(peer_ip)
      table.insert(peers, peer_ip)
    end
    if #peers>0 then
      table.insert(peers, "Total of "..#peers.." peers discovered")
    end
  else
    for peer_ip in pairs(t.peers) do
      table.insert(peers, peer_ip)
    end
    if #peers>0 then
      table.insert(peers, "Total of "..#peers.." peers discovered")
    end
  end

  -- add nodes
  if target.ALLOW_NEW_TARGETS and include_nodes then
    for node_ip in pairs(t.nodes) do
      target.add(node_ip)
      table.insert(nodes, node_ip)
    end
    if #nodes >0 then
      table.insert(nodes, "Total of "..#nodes.." nodes discovered")
    end
  elseif include_nodes then
    for node_ip in pairs(t.nodes) do
      table.insert(nodes, node_ip)
    end
    if #nodes >0 then
      table.insert(nodes, "Total of "..#nodes.." nodes discovered")
    end
  end

  local print_out = false

  if #peers > 0 then
    table.insert(output, peers)
    print_out = true
  end

  if include_nodes and #nodes > 0 then
    table.insert(output, nodes)
    print_out = true
  end

  if print_out and not target.ALLOW_NEW_TARGETS then
    table.insert(output,"Use the newtargets script-arg to add the results as targets")
  end

  return stdnse.format_output( print_out , output)
end