Repository navigation
Expand file tree
/
Copy patharpping.py
More file actions
141 lines (112 loc) · 4.4 KB
/
Copy patharpping.py
File metadata and controls
141 lines (112 loc) · 4.4 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
######################################################################
# arpping -- Tool for the discovery of machines in local networks
# through ARP pings.
#
# Copyright (c) 2021, Scan0r
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# @Author Scan0r
# @Date 14/02/2022
# @Version 0.1
######################################################################
######################################################################
#
# Global Imports
#
######################################################################
import argparse
import ipaddress
import pprint
from scapy.all import *
######################################################################
#
# Global Definitions
#
######################################################################
# Script config
AUTHOR = "drv"
DATE = "14/02/2022"
VERSION = "0.1"
# Global variables
DEFAULT_RETRY = 0
DEFAULT_TIMEOUT = 1
DEFAULT_VERBOSE = False
# Parser
parser = argparse.ArgumentParser(description='Tool for the discovery of machines in local networks through ARP pings.', epilog='Bye! Don\'t be evil.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('addresses', metavar='<Target>', type=str, nargs='+', help='target IP address or network')
parser.add_argument('-r', '--retry', type=int, default=DEFAULT_RETRY, action='store', help='number of times to retry a packet send')
parser.add_argument('-t', '--timeout', type=int, default=DEFAULT_TIMEOUT, help='number of seconds to wait for a packet response')
parser.add_argument('-v', '--verbose', default=DEFAULT_VERBOSE, action='store_true', help='enables scapy verbose mode')
parser.add_argument('-V', '--version', action='version', version=f'%(prog)s {VERSION}')
######################################################################
#
# Auxiliary functions
#
######################################################################
def am_i_root():
return os.geteuid() == 0
def check_address(address: str):
ip_address_pattern = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')
ip_network_pattern = re.compile('^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/[0-9]{1,2}$')
if bool(ip_address_pattern.match(address)):
try:
ipaddress.ip_address(address)
except ValueError as ve:
raise ve
elif bool(ip_network_pattern.match(address)):
try:
ipaddress.ip_network(address)
except ValueError as ve:
raise ve
else:
raise ValueError(f"Invalid target address '{address}'")
return True
def do_arp_ping(target: str, retry: int = DEFAULT_RETRY, timeout: int = DEFAULT_TIMEOUT, verbose: bool = DEFAULT_VERBOSE):
ether = Ether(dst="ff:ff:ff:ff:ff:ff")
arp = ARP(pdst=target, hwdst="ff:ff:ff:ff:ff:ff")
pkt = ether / arp
answers, unanswers = srp(pkt, retry=retry, timeout=timeout, verbose=verbose)
print("")
print("-" * 34)
print(" " * 6 + "MAC Address" + " " * 2 + "IP Address")
print("-" * 34)
answers.summary(lambda s, r: r.sprintf("%Ether.src% %ARP.psrc%"))
print("")
######################################################################
#
# Main function
#
######################################################################
def main(argv: list):
if not am_i_root():
print("[-] You must be root to execute an ARP scan.")
sys.exit()
addresses = argv['addresses']
del argv['addresses']
for address in addresses:
check_address(address)
print(f"[+] Doing ARP Ping against address {address}")
do_arp_ping(address, **argv)
######################################################################
#
# Main call
#
######################################################################
if __name__ == '__main__':
args = parser.parse_args()
main(vars(args))