2020-12-17 14:17:27 +00:00
|
|
|
package process
|
2020-07-19 05:17:05 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"net"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
2021-06-15 13:03:47 +00:00
|
|
|
|
|
|
|
"golang.org/x/sys/unix"
|
2020-07-19 05:17:05 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
procpidpathinfo = 0xb
|
|
|
|
procpidpathinfosize = 1024
|
|
|
|
proccallnumpidinfo = 0x2
|
|
|
|
)
|
|
|
|
|
2020-12-17 14:17:27 +00:00
|
|
|
func findProcessName(network string, ip net.IP, port int) (string, error) {
|
2020-07-29 03:27:18 +00:00
|
|
|
var spath string
|
2020-12-17 14:17:27 +00:00
|
|
|
switch network {
|
|
|
|
case TCP:
|
2020-07-29 03:27:18 +00:00
|
|
|
spath = "net.inet.tcp.pcblist_n"
|
2020-12-17 14:17:27 +00:00
|
|
|
case UDP:
|
2020-07-19 05:17:05 +00:00
|
|
|
spath = "net.inet.udp.pcblist_n"
|
2020-07-29 03:27:18 +00:00
|
|
|
default:
|
|
|
|
return "", ErrInvalidNetwork
|
2020-07-19 05:17:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 03:27:18 +00:00
|
|
|
isIPv4 := ip.To4() != nil
|
|
|
|
|
2020-07-19 05:17:05 +00:00
|
|
|
value, err := syscall.Sysctl(spath)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := []byte(value)
|
|
|
|
|
2020-07-29 03:27:18 +00:00
|
|
|
// from darwin-xnu/bsd/netinet/in_pcblist.c:get_pcblist_n
|
|
|
|
// size/offset are round up (aligned) to 8 bytes in darwin
|
|
|
|
// rup8(sizeof(xinpcb_n)) + rup8(sizeof(xsocket_n)) +
|
|
|
|
// 2 * rup8(sizeof(xsockbuf_n)) + rup8(sizeof(xsockstat_n))
|
|
|
|
itemSize := 384
|
2020-12-17 14:17:27 +00:00
|
|
|
if network == TCP {
|
2020-07-29 03:27:18 +00:00
|
|
|
// rup8(sizeof(xtcpcb_n))
|
|
|
|
itemSize += 208
|
|
|
|
}
|
2020-09-03 02:27:20 +00:00
|
|
|
// skip the first xinpgen(24 bytes) block
|
|
|
|
for i := 24; i+itemSize <= len(buf); i += itemSize {
|
2020-07-29 03:27:18 +00:00
|
|
|
// offset of xinpcb_n and xsocket_n
|
|
|
|
inp, so := i, i+104
|
2020-07-19 05:17:05 +00:00
|
|
|
|
|
|
|
srcPort := binary.BigEndian.Uint16(buf[inp+18 : inp+20])
|
|
|
|
if uint16(port) != srcPort {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// xinpcb_n.inp_vflag
|
|
|
|
flag := buf[inp+44]
|
|
|
|
|
|
|
|
var srcIP net.IP
|
2020-07-29 03:27:18 +00:00
|
|
|
switch {
|
|
|
|
case flag&0x1 > 0 && isIPv4:
|
2020-07-19 05:17:05 +00:00
|
|
|
// ipv4
|
|
|
|
srcIP = net.IP(buf[inp+76 : inp+80])
|
2020-07-29 03:27:18 +00:00
|
|
|
case flag&0x2 > 0 && !isIPv4:
|
2020-07-19 05:17:05 +00:00
|
|
|
// ipv6
|
|
|
|
srcIP = net.IP(buf[inp+64 : inp+80])
|
2020-07-29 03:27:18 +00:00
|
|
|
default:
|
2020-07-19 05:17:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-02-23 06:04:47 +00:00
|
|
|
if !ip.Equal(srcIP) && (network == TCP || !srcIP.IsUnspecified()) {
|
2020-07-19 05:17:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// xsocket_n.so_last_pid
|
2020-07-29 03:27:18 +00:00
|
|
|
pid := readNativeUint32(buf[so+68 : so+72])
|
2020-07-19 05:17:05 +00:00
|
|
|
return getExecPathFromPID(pid)
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:17:27 +00:00
|
|
|
return "", ErrNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
func getExecPathFromPID(pid uint32) (string, error) {
|
|
|
|
buf := make([]byte, procpidpathinfosize)
|
|
|
|
_, _, errno := syscall.Syscall6(
|
|
|
|
syscall.SYS_PROC_INFO,
|
|
|
|
proccallnumpidinfo,
|
|
|
|
uintptr(pid),
|
|
|
|
procpidpathinfo,
|
|
|
|
0,
|
|
|
|
uintptr(unsafe.Pointer(&buf[0])),
|
|
|
|
procpidpathinfosize)
|
|
|
|
if errno != 0 {
|
|
|
|
return "", errno
|
|
|
|
}
|
|
|
|
|
2022-03-12 11:07:53 +00:00
|
|
|
return unix.ByteSliceToString(buf), nil
|
2020-07-19 05:17:05 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 03:27:18 +00:00
|
|
|
func readNativeUint32(b []byte) uint32 {
|
|
|
|
return *(*uint32)(unsafe.Pointer(&b[0]))
|
2020-07-19 05:17:05 +00:00
|
|
|
}
|