This commit is contained in:
2026-11-26 23:34:55 +03:00
parent 20afc2c790
commit a25f357314
167 changed files with 33202 additions and 89 deletions
+35
View File
@@ -0,0 +1,35 @@
#!/usr/bin/env python3
"""Inspect delivered flags and real PID 1 kernel state without target shell/tools."""
import json,os,subprocess
from pathlib import Path
ROOT=Path(__file__).resolve().parent.parent
DOCKER=[str(ROOT/'scripts/docker.sh')]
def run(*args):return subprocess.check_output(DOCKER+list(args),text=True,stderr=subprocess.PIPE)
ids=run('ps','--quiet','--filter','label=com.docker.compose.project=lab7','--filter','label=com.docker.compose.oneoff=False').split()
assert ids,'No running lab7 services'
containers=json.loads(run('inspect',*ids));result=[]
for c in containers:
service=c['Config']['Labels']['com.docker.compose.service'];h=c['HostConfig'];user=c['Config']['User']
assert user.split(':')[0] not in ('','0','root'),service
assert h['ReadonlyRootfs'] and 'ALL' in h['CapDrop'] and h['Memory']>0,service
assert any(x in ('no-new-privileges','no-new-privileges:true') for x in h['SecurityOpt']),service
assert h['NetworkMode']!='host' and not h['Privileged'],service
assert not any(m['Destination']=='/var/run/docker.sock' for m in c['Mounts']),service
bindings={k:v for k,v in (h.get('PortBindings') or {}).items() if v}
if bindings:
assert service in ('lb','grafana-tls','openbao'),service
if service!='lb':assert all(x['HostIp']=='127.0.0.1' for v in bindings.values() for x in v),service
# A trusted helper shares only the target PID namespace, no network or mounts.
# PID 1 remains the target service; inspect its kernel status, not the helper's.
status=run('run','--rm','--network','none','--pid','container:'+c['Id'],'--user',user,'--read-only','--cap-drop','ALL','--security-opt','no-new-privileges:true','--entrypoint','/bin/cat','lab7-api:local','/proc/1/status')
fields=dict(line.split(':',1) for line in status.splitlines() if ':' in line)
assert fields['NoNewPrivs'].strip()=='1',service
assert int(fields['CapEff'].strip(),16)==0,service
assert int(fields['Uid'].split()[0])!=0,service
result.append({'service':service,'container_id':c['Id'],'user':user,'read_only':True,'cap_drop':['ALL'],'no_new_privs':1,'effective_capabilities':0,'seccomp':fields['Seccomp'].strip(),'apparmor':c.get('AppArmorProfile'),'memory_limit_bytes':h['Memory'],'nano_cpus':h['NanoCpus'],'published_ports':bindings})
expected={'api','api2','worker','api-tls','api2-tls','db','cache','minio','openbao','lb','otel-collector','otel-collector-tls','loki','loki-tls','tempo','tempo-tls','prometheus','prometheus-tls','grafana','grafana-tls'}
assert {x['service'] for x in result}==expected,'The complete stack must be running'
server=json.loads(run('version','--format','{{json .Server}}'))
output={'status':'PASS','docker_version':server['Version'],'docker_root_dir':run('info','--format','{{.DockerRootDir}}').strip(),'context':'main compose.yaml, no diagnostic override','services':result}
(ROOT/'evidence/hardening-native.json').write_text(json.dumps(output,indent=2)+'\n')
print('PASS: all 20 real service PID 1 processes are non-root, NoNewPrivs=1, CapEff=0; read_only/resources/ports verified')