MCPcopy Create free account
hub / github.com/HexHive/NASS / show_connections

Method show_connections

tools/gef.py:5104–5149  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

5102 return socket.inet_ntoa(struct.pack("<I", int(ip, 16))), int(port, 16)
5103
5104 def show_connections(self) -> None:
5105 # https://github.com/torvalds/linux/blob/v4.7/include/net/tcp_states.h#L16
5106 tcp_states_str = {
5107 0x01: "TCP_ESTABLISHED",
5108 0x02: "TCP_SYN_SENT",
5109 0x03: "TCP_SYN_RECV",
5110 0x04: "TCP_FIN_WAIT1",
5111 0x05: "TCP_FIN_WAIT2",
5112 0x06: "TCP_TIME_WAIT",
5113 0x07: "TCP_CLOSE",
5114 0x08: "TCP_CLOSE_WAIT",
5115 0x09: "TCP_LAST_ACK",
5116 0x0A: "TCP_LISTEN",
5117 0x0B: "TCP_CLOSING",
5118 0x0C: "TCP_NEW_SYN_RECV",
5119 }
5120
5121 udp_states_str = {
5122 0x07: "UDP_LISTEN",
5123 }
5124
5125 info("Network Connections")
5126 pid = gef.session.pid
5127 sockets = self.list_sockets(pid)
5128 if not sockets:
5129 gef_print("\tNo open connections")
5130 return
5131
5132 entries = dict()
5133 with open(f"/proc/{pid:d}/net/tcp", "r") as tcp:
5134 entries["TCP"] = [x.split() for x in tcp.readlines()[1:]]
5135 with open(f"/proc/{pid:d}/net/udp", "r") as udp:
5136 entries["UDP"] = [x.split() for x in udp.readlines()[1:]]
5137
5138 for proto in entries:
5139 for entry in entries[proto]:
5140 local, remote, state = entry[1:4]
5141 inode = int(entry[9])
5142 if inode in sockets:
5143 local = self.parse_ip_port(local)
5144 remote = self.parse_ip_port(remote)
5145 state = int(state, 16)
5146 state_str = tcp_states_str[state] if proto == "TCP" else udp_states_str[state]
5147
5148 gef_print(f"\t{local[0]}:{local[1]} {RIGHT_ARROW} {remote[0]}:{remote[1]} ({state_str})")
5149 return
5150
5151
5152@register

Callers 1

do_invokeMethod · 0.95

Calls 4

list_socketsMethod · 0.95
parse_ip_portMethod · 0.95
infoFunction · 0.85
gef_printFunction · 0.85

Tested by

no test coverage detected