-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.py
More file actions
69 lines (54 loc) · 2.08 KB
/
server.py
File metadata and controls
69 lines (54 loc) · 2.08 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
#
# Copyright (c) 2020 Raul Caro.
#
# This file is part of ICMPack
# (see https://github.com/rcaroncd/ICMPack).
#
# 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 <http://www.gnu.org/licenses/>.
#
from icmp import Packet, Offsets, Sizes
from sys import argv
import socket
# It is necessary to be linked to a network interface,
# so you have to go through arguments
iface = argv[1]
# If data is passed, it is added to the data section of the ICMP package,
# but if it is not passed, the default values used by PING in the data field are used
data = bytes(argv[2],'utf-8') if len(argv) == 3 else None
s = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_ICMP)
s.setsockopt(socket.SOL_SOCKET,25,str(iface+'\0').encode('utf-8'))
rec_packet, addr = s.recvfrom(65535)
# Transforming the received bytes into an ICMP packet structure
request_packet = None
if data:
request_packet = Packet(ping=False)
else:
request_packet = Packet(ping=True)
request_packet.unpack(rec_packet)
print("Request Packet:")
print(request_packet)
print("[*] DATA Sent: ", request_packet.data)
# Generating the icmp response based on the icmp request received
response_packet = None
if data:
response_packet = Packet(ping=False)
response_packet.pack_response(request_packet, data)
else:
response_packet = Packet(ping=True)
response_packet.pack_response(request_packet)
response_raw_packet = response_packet.toBytes()
print("Response Packet:")
print(response_packet)
print("[*] DATA Recv: ", response_packet.data)
s.sendto(response_raw_packet, (addr[0], 1))