File size: 1,567 Bytes
06cfa3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Prove that a listening TCP socket belongs to the attested candidate process."""
from __future__ import annotations

import os
from pathlib import Path
import sys


def owns_tcp_listener(pid: int, port: int, parent_pid: int) -> bool:
    process = Path('/proc') / str(pid)
    try:
        state = (process / 'stat').read_text().rsplit(')', 1)[1].split()
        if state[0] == 'Z' or int(state[1]) != parent_pid:
            return False
        sockets = set()
        for entry in (process / 'fd').iterdir():
            try:
                target = os.readlink(entry)
            except FileNotFoundError:
                continue
            if target.startswith('socket:[') and target.endswith(']'):
                sockets.add(target[8:-1])
        if not sockets:
            return False
        for table in ('tcp', 'tcp6'):
            try:
                rows = (process / 'net' / table).read_text().splitlines()[1:]
            except FileNotFoundError:
                continue
            for row in rows:
                fields = row.split()
                if (len(fields) > 9 and fields[3] == '0A'
                        and int(fields[1].rsplit(':', 1)[1], 16) == port
                        and fields[9] in sockets):
                    return True
        return False
    except FileNotFoundError:
        return False


if __name__ == '__main__':
    try:
        matched = owns_tcp_listener(*(int(value) for value in sys.argv[1:]))
    except (OSError, ValueError):
        raise SystemExit(2)
    raise SystemExit(0 if matched else 1)