Repository navigation
Expand file tree
/
Copy pathphp2shell-check
More file actions
executable file
·907 lines (824 loc) · 34.4 KB
/
Copy pathphp2shell-check
File metadata and controls
executable file
·907 lines (824 loc) · 34.4 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
#!/usr/bin/env bash
#
# php2shell-check — CVE-2026-109618, CVE-2026-109619 & CVE-2026-115560 Audit & Fingerprinting Tool
# https://github.com/Anton-Barinov/php2shell-patches
#
# Zero-dependency, non-destructive CLI scanner to discover PHP runtimes (CLI, FPM,
# panels like aaPanel, HestiaCP, Plesk, cPanel, Remi), audit configurations,
# detect backported security patches, and identify vulnerable instances.
#
# Usage:
# ./php2shell-check [OPTIONS]
# curl -fsSL https://raw.githubusercontent.com/Anton-Barinov/php2shell-patches/main/php2shell-check | bash
#
# Exit codes:
# 0 - All scanned runtimes have allocator evidence and upload progress disabled
# 1 - CLI probe observed legacy OOM without recognized allocator fix evidence
# 2 - Partial mitigation, unknown evidence, or no runtimes found
set -o pipefail 2>/dev/null || true
VERSION="1.3.0"
REPO_URL="https://github.com/Anton-Barinov/php2shell-patches"
# Flags
OPT_ALL=0
OPT_FIX=0
OPT_JSON=0
OPT_QUIET=0
OPT_URL=""
OPT_BINARY=""
OPT_PROBE_OOM=0
# Colors (auto-disabled if not a tty or NO_COLOR / JSON mode)
setup_colors() {
if [ "$OPT_JSON" -eq 1 ] || [ "$OPT_QUIET" -eq 1 ] || [ -n "${NO_COLOR:-}" ] || [ ! -t 1 ]; then
C_RESET=""
C_BOLD=""
C_RED=""
C_GREEN=""
C_YELLOW=""
C_BLUE=""
C_CYAN=""
C_MAGENTA=""
C_GRAY=""
else
C_RESET="\033[0m"
C_BOLD="\033[1m"
C_RED="\033[1;31m"
C_GREEN="\033[1;32m"
C_YELLOW="\033[1;33m"
C_BLUE="\033[1;34m"
C_CYAN="\033[1;36m"
C_MAGENTA="\033[1;35m"
C_GRAY="\033[0;90m"
fi
}
show_help() {
cat <<EOF
php2shell-check — CVE-2026-109618, CVE-2026-109619 & CVE-2026-115560 Audit Tool (v${VERSION})
Repository: ${REPO_URL}
Usage:
./php2shell-check [OPTIONS]
Options:
--binary <path> Inspect one specific executable (CLI, CGI or FPM)
--probe-oom Opt in to a bounded CLI-only allocator behaviour probe
-a, --all Deep filesystem scan for hidden/custom PHP binaries
-f, --fix-config Generate emergency config commands to disable upload_progress
-u, --url <URL> Observe HTTP response (cannot establish security; returns exit 2)
-j, --json Output results in JSON format (for CI/CD, SIEM & monitoring)
-q, --quiet Quiet mode (output only critical errors and final exit code)
-v, --version Show utility version
-h, --help Show this help message
Exit Codes:
0 - Every discovered runtime has allocator evidence and upload progress disabled
1 - CLI probe observed legacy OOM without recognized allocator fix evidence
2 - Partial mitigation, unknown evidence, or no runtimes found
Examples:
# One-liner quick server audit:
curl -fsSL https://raw.githubusercontent.com/Anton-Barinov/php2shell-patches/main/php2shell-check | sudo bash
# Full audit with remediation command generation:
sudo ./php2shell-check -a -f
# Safe external remote web test:
./php2shell-check -u https://example.com/
EOF
}
# Parse command line options
while [ $# -gt 0 ]; do
case "$1" in
--binary)
[ -n "${2:-}" ] && [ -x "$2" ] || { echo "--binary requires an executable path" >&2; exit 2; }
OPT_BINARY="$2"; shift 2 ;;
--probe-oom) OPT_PROBE_OOM=1; shift ;;
-a|--all)
OPT_ALL=1
shift
;;
-f|--fix|--fix-config)
OPT_FIX=1
shift
;;
-u|--url)
if [ -z "${2:-}" ]; then
echo "Error: option --url requires a target URL argument" >&2
exit 1
fi
OPT_URL="$2"
shift 2
;;
-j|--json)
OPT_JSON=1
shift
;;
-q|--quiet)
OPT_QUIET=1
shift
;;
-v|--version)
echo "php2shell-check v${VERSION}"
exit 0
;;
-h|--help)
show_help
exit 0
;;
*)
echo "Unknown option: $1 (use --help for usage)" >&2
exit 1
;;
esac
done
setup_colors
# Portable realpath resolver
resolve_path() {
local target="$1"
if command -v realpath >/dev/null 2>&1; then
realpath "$target" 2>/dev/null || echo "$target"
elif command -v readlink >/dev/null 2>&1; then
readlink -f "$target" 2>/dev/null || echo "$target"
else
echo "$target"
fi
}
# Semver comparison: version_ge "$v1" "$v2" returns 0 if v1 >= v2, 1 if v1 < v2
version_ge() {
local v1="$1" v2="$2"
awk -v v1="$v1" -v v2="$v2" '
BEGIN {
n1 = split(v1, a, "[.-]");
n2 = split(v2, b, "[.-]");
max = (n1 > n2) ? n1 : n2;
for (i = 1; i <= max; i++) {
c1 = (i <= n1) ? a[i] + 0 : 0;
c2 = (i <= n2) ? b[i] + 0 : 0;
if (c1 > c2) exit 0;
if (c1 < c2) exit 1;
}
exit 0;
}'
}
# Version less than: version_lt "$v1" "$v2"
version_lt() {
if version_ge "$1" "$2"; then
return 1
else
return 0
fi
}
# Detect running web servers
detect_web_server() {
local servers=()
if pgrep -x nginx >/dev/null 2>&1 || pgrep -f "nginx:" >/dev/null 2>&1; then
servers+=("nginx")
fi
if pgrep -x apache2 >/dev/null 2>&1 || pgrep -x httpd >/dev/null 2>&1 || pgrep -f "apache2|httpd" >/dev/null 2>&1; then
servers+=("apache")
fi
if pgrep -x caddy >/dev/null 2>&1; then
servers+=("caddy")
fi
if pgrep -x lighttpd >/dev/null 2>&1; then
servers+=("lighttpd")
fi
if pgrep -x openlitespeed >/dev/null 2>&1 || pgrep -f "openlitespeed|lsws" >/dev/null 2>&1; then
servers+=("openlitespeed")
fi
if [ ${#servers[@]} -eq 0 ]; then
echo "none_or_proxy"
else
local IFS=", "
echo "${servers[*]}"
fi
}
# Detect hosting control panel
detect_control_panel() {
if [ -d "/www/server/php" ] || [ -f "/www/server/panel/class/common.py" ]; then
echo "aaPanel / Baota (/www/server/php)"
elif [ -d "/usr/local/hestia" ]; then
echo "HestiaCP (/usr/local/hestia)"
elif [ -d "/usr/local/vesta" ]; then
echo "VestaCP (/usr/local/vesta)"
elif [ -d "/opt/plesk/php" ] || [ -d "/usr/local/psa" ]; then
echo "Plesk (/opt/plesk/php)"
elif [ -d "/opt/cpanel" ] || [ -d "/var/cpanel" ]; then
echo "cPanel / EasyApache (/opt/cpanel)"
elif [ -d "/opt/remi" ]; then
echo "Remi Repository (/opt/remi)"
elif [ -d "/usr/local/directadmin" ]; then
echo "DirectAdmin (/usr/local/directadmin)"
elif [ -d "/usr/local/mgr5" ]; then
echo "ISPmanager (/usr/local/mgr5)"
else
echo "None (Standard OS / Distribution)"
fi
}
# Find all PHP binaries
discover_php_binaries() {
local candidates=()
local seen=()
if [ -n "$OPT_BINARY" ]; then
resolve_path "$OPT_BINARY"
return
fi
# 1. Standard PATH / system locations
for p in /usr/bin/php* /usr/local/bin/php* /usr/bin/lsphp* /usr/local/bin/lsphp* \
/usr/sbin/php-fpm* /usr/local/sbin/php-fpm* \
/opt/php*/bin/php* /opt/php*/bin/lsphp* /opt/php*/sbin/php-fpm* \
/opt/fphp*/bin/php* /opt/fphp*/bin/lsphp* /opt/fphp*/sbin/php-fpm* \
/opt/alt/php*/usr/bin/php* /opt/alt/php*/usr/bin/lsphp* /opt/alt/php*/usr/sbin/php-fpm* \
/opt/rh/rh-php*/root/usr/bin/php* /opt/rh/rh-php*/root/usr/sbin/php-fpm* \
/usr/local/php*/bin/php* /usr/local/php*/bin/lsphp* /usr/local/php*/sbin/php-fpm* \
/usr/local/lsws/lsphp*/bin/*php* /usr/local/lsws/fcgi-bin/lsphp* \
/opt/keyhelp/php/*/bin/php* /opt/keyhelp/php/*/sbin/php-fpm* \
/opt/homebrew/bin/php /usr/local/hestia/php/bin/php /usr/local/hestia/php/sbin/php-fpm; do
if [ -f "$p" ] && [ -x "$p" ]; then
candidates+=("$p")
fi
done
# 2. aaPanel / Baota locations
if [ -d "/www/server/php" ]; then
for p in /www/server/php/*/bin/php* /www/server/php/*/bin/lsphp* /www/server/php/*/sbin/php-fpm*; do
if [ -f "$p" ] && [ -x "$p" ]; then
candidates+=("$p")
fi
done
fi
# 3. Plesk locations
if [ -d "/opt/plesk/php" ]; then
for p in /opt/plesk/php/*/bin/php* /opt/plesk/php/*/bin/lsphp* /opt/plesk/php/*/sbin/php-fpm*; do
if [ -f "$p" ] && [ -x "$p" ]; then
candidates+=("$p")
fi
done
fi
# 4. cPanel / EasyApache locations
if [ -d "/opt/cpanel" ]; then
for p in /opt/cpanel/ea-php*/root/usr/bin/php* /opt/cpanel/ea-php*/root/usr/sbin/php-fpm; do
if [ -f "$p" ] && [ -x "$p" ]; then
candidates+=("$p")
fi
done
fi
# 5. Remi repository locations
if [ -d "/opt/remi" ]; then
for p in /opt/remi/php*/root/usr/bin/php* /opt/remi/php*/root/usr/sbin/php-fpm; do
if [ -f "$p" ] && [ -x "$p" ]; then
candidates+=("$p")
fi
done
fi
# 6. Active processes in memory (ps aux)
if command -v ps >/dev/null 2>&1; then
local proc_bins
proc_bins=$(ps -eo args 2>/dev/null | grep -E "(php-fpm|php-cgi|bin/php)" | grep -v "grep" | awk '{print $1}' | sort -u)
for p in $proc_bins; do
if [ -f "$p" ] && [ -x "$p" ]; then
candidates+=("$p")
fi
done
fi
# 7. Deep scan if requested (-a, --all)
if [ "$OPT_ALL" -eq 1 ]; then
while IFS= read -r p; do
if [ -f "$p" ] && [ -x "$p" ]; then
candidates+=("$p")
fi
done < <(find /usr /opt /www /usr/local /home /var -maxdepth 8 -type f \( -name "php" -o -name "php[0-9]*" -o -name "php-fpm*" -o -name "php-cgi*" -o -name "lsphp*" \) -perm -111 2>/dev/null || true)
fi
# Filter out backup files (*.bak*), non-executables, and deduplicate realpaths
local deduped=()
for bin in "${candidates[@]}"; do
# Skip backup and temporary files
case "$bin" in
*.bak*|*.swapbak*|*~|*.orig|*.old|*.old/*|*.orig/*|*~/*)
continue
;;
esac
# Broad globs also match phpize/php-config and extension utilities.
# Filter the discovered name before resolving aliases to its realpath.
local runtime_name="${bin##*/}"
printf '%s\n' "$runtime_name" | LC_ALL=C grep -E '^(php|php-cgi|php-fpm|lsphp)([0-9]+(\.[0-9]+)*)?(-cgi|-fpm)?$' >/dev/null || continue
local rpath
rpath=$(resolve_path "$bin")
if [ ! -f "$rpath" ] || [ ! -x "$rpath" ]; then
continue
fi
# Check if already in seen list
local already_seen=0
for s in "${seen[@]}"; do
if [ "$s" = "$rpath" ]; then
already_seen=1
break
fi
done
if [ "$already_seen" -eq 0 ]; then
seen+=("$rpath")
deduped+=("$rpath")
fi
done
printf '%s\n' "${deduped[@]}"
}
# Consume the entire match stream (grep -q can cause SIGPIPE with pipefail).
binary_has() { LC_ALL=C grep -a -E "$2" "$1" >/dev/null 2>&1; }
json_escape() {
local value="$1"
value="${value//\\/\\\\}"
value="${value//\"/\\\"}"
value="${value//$'\n'/\\n}"
value="${value//$'\r'/\\r}"
value="${value//$'\t'/\\t}"
printf '%s' "$value"
}
# Audit a single PHP runtime instance
audit_php_runtime() {
local bin="$1"
# 1. Version extraction
local v_output
v_output=$("$bin" -n -v 2>&1 || true)
local raw_ver
raw_ver=$(echo "$v_output" | sed -n 's/.*PHP \([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/p')
if [ -z "$raw_ver" ]; then
raw_ver=$(echo "$v_output" | sed -n 's/.*PHP \([0-9][0-9]*\.[0-9][0-9]*\).*/\1/p')
fi
if [ -z "$raw_ver" ]; then
raw_ver="unknown"
fi
# Determine major and minor
local major minor
major=$(echo "$raw_ver" | cut -d. -f1)
minor=$(echo "$raw_ver" | cut -d. -f2)
local branch="${major}.${minor}"
# Determine SAPI type
local sapi="cli"
case "$bin" in
*php-fpm*|*php[0-9]*-fpm) sapi="fpm" ;;
*php-cgi*|*php[0-9]*-cgi) sapi="cgi" ;;
*lsphp*) sapi="litespeed" ;;
*)
local sapi_check
sapi_check=$("$bin" -r "echo PHP_SAPI;" 2>/dev/null || echo "cli")
sapi="$sapi_check"
;;
esac
# Match the actual executable on Linux; version-number matches in ps
# can accidentally select an unrelated process or another PHP build.
local fpm_active="NO" fpm_listen="" fpm_conf=""
if [ "$sapi" = "fpm" ]; then
local process_exe
for process_exe in /proc/[0-9]*/exe; do
if [ "$bin" -ef "$process_exe" ] 2>/dev/null; then
fpm_active="YES"
break
fi
done
fi
# Read configuration using -i: FPM/CGI do not support CLI -r/--ini.
# These are SAPI defaults, not the effective settings of every FPM pool.
local php_info ini_path upload_progress_val upload_progress
php_info=$("$bin" -i 2>/dev/null || true)
ini_path=$(printf '%s\n' "$php_info" | sed -n 's/^Loaded Configuration File => //p' | head -n 1)
ini_path="${ini_path:-none}"
upload_progress_val=$(printf '%s\n' "$php_info" | sed -n 's/^session.upload_progress.enabled => \([^ ]*\).*/\1/p' | head -n 1)
upload_progress="UNKNOWN"
case "$upload_progress_val" in
0|Off|off) upload_progress="DISABLED" ;;
1|On|on) upload_progress="ENABLED" ;;
esac
local stream_transports ssl_transport="UNKNOWN" tls_transport="UNKNOWN" serializer
stream_transports=$(printf '%s\n' "$php_info" | sed -n 's/^Registered Stream Socket Transports => //p' | head -n 1)
if [ -n "$stream_transports" ]; then
ssl_transport="NO"; tls_transport="NO"
local transport
while IFS= read -r transport; do
case "$transport" in ssl) ssl_transport="YES" ;; tls) tls_transport="YES" ;; esac
done < <(printf '%s\n' "$stream_transports" | tr ',' '\n' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
fi
serializer=$(printf '%s\n' "$php_info" | sed -n 's/^session.serialize_handler => \([^ ]*\).*/\1/p' | head -n 1)
serializer="${serializer:-unknown}"
# Fingerprints are evidence, not proof of all CVEs being fixed.
# Multipart limits predate this incident and session-only fixes do not
# establish allocator safety. Absence also cannot disprove vendor backports.
local allocator="NO" session_guard="NO" patch_applied="NO"
local patch_flavor="none"
if binary_has "$bin" 'zend_mm_safe_error_ex'; then
allocator="YES"; patch_flavor="exit_allocator_fingerprint"
elif binary_has "$bin" 'zend_handle_delayed_oom|memory_hard_limit_percent'; then
allocator="YES"; patch_flavor="soft_oom_fingerprint"
fi
if binary_has "$bin" '__PHP_Incomplete_Class_Serialized'; then
session_guard="YES"
fi
local probe_result="not_requested"
if [ "$OPT_PROBE_OOM" -eq 1 ] && [ "$sapi" = "cli" ]; then
local probe_out probe_rc
# No system configuration, no extensions, 8M limit and one 32M
# allocation. The process has no request data or application hooks.
probe_out=$("$bin" -n -d memory_limit=8M -d display_errors=stderr -d log_errors=0 -r 'register_shutdown_function(function () { echo "P2S_SHUTDOWN\n"; }); echo "P2S_BEFORE\n"; str_repeat("x", 32 * 1024 * 1024); echo "P2S_AFTER\n";' 2>&1)
probe_rc=$?
probe_result="inconclusive"
if [ "$probe_rc" -eq 1 ] && [[ "$probe_out" == *P2S_BEFORE* ]] && [[ "$probe_out" == *"Allowed memory size"* ]] && [[ "$probe_out" != *P2S_SHUTDOWN* ]] && [[ "$probe_out" != *P2S_AFTER* ]]; then
allocator="YES"; patch_flavor="observed_exit_on_oom"; probe_result="exit_on_oom"
elif [[ "$probe_out" == *P2S_SHUTDOWN* ]] && [[ "$probe_out" == *"Allowed memory size"* ]]; then
probe_result="graceful_oom"
fi
fi
local upstream="NO"
case "$branch" in
8.2) version_ge "$raw_ver" 8.2.35 && upstream="YES" ;;
8.3) version_ge "$raw_ver" 8.3.36 && upstream="YES" ;;
8.4) version_ge "$raw_ver" 8.4.27 && upstream="YES" ;;
8.5) version_ge "$raw_ver" 8.5.12 && upstream="YES" ;;
esac
if printf '%s\n' "$v_output" | grep -E 'PHP [0-9]+\.[0-9]+\.[0-9]+(RC|alpha|beta|-dev)' >/dev/null; then
upstream="NO"
fi
local status="UNKNOWN" status_msg
status_msg="No conclusive patch evidence. Missing symbols do not prove vulnerability; verify vendor provenance and run controlled SAPI checks."
if [ "$raw_ver" = "unknown" ]; then
status_msg="Could not read PHP version; inspection failed."
elif [ "$upstream" = "YES" ] || [ "$allocator" = "YES" ]; then
patch_applied="YES"
if [ "$upstream" = "YES" ]; then patch_flavor="upstream_version_evidence"; fi
if [ "$upload_progress" = "DISABLED" ]; then
status="PATCHED"
status_msg="Allocator fix evidence: ${patch_flavor}; upload progress disabled in SAPI defaults. Verify pool and application overrides separately."
else
status="MITIGATED"
status_msg="Allocator fix evidence: ${patch_flavor}; upload progress ${upload_progress}; serializer protection not confirmed (session guard fingerprint: ${session_guard}). Verify CVE-2026-115560 on the actual SAPI."
fi
elif [ "$upload_progress" = "DISABLED" ]; then
status="MITIGATED"
status_msg="Upload progress disabled in SAPI defaults; allocator fix unverified. This is partial mitigation only."
fi
if [ "$probe_result" = "graceful_oom" ] && [ "$allocator" = "NO" ] && [ "$upstream" = "NO" ]; then
status="VULNERABLE"
status_msg="Observed graceful OOM shutdown with no known allocator-fix evidence. Legacy bailout behaviour requires remediation; this is not an RCE exploit test."
fi
# Output structured data
echo "${bin}|${raw_ver}|${branch}|${sapi}|${fpm_active}|${fpm_listen}|${ini_path}|${upload_progress}|${patch_applied}|${status}|${status_msg}|${ssl_transport}|${tls_transport}|${serializer}"
}
# Remote Web Audit Mode (--url)
run_remote_audit() {
local target_url="$OPT_URL"
if ! command -v curl >/dev/null 2>&1; then
echo "Error: remote audit mode requires 'curl' utility" >&2
exit 1
fi
if [ "$OPT_QUIET" -eq 0 ] && [ "$OPT_JSON" -eq 0 ]; then
echo -e "${C_CYAN}========================================================================${C_RESET}"
echo -e "${C_BOLD} php2shell-check — Remote Non-Destructive Web Audit (v${VERSION})${C_RESET}"
echo -e " ${C_GRAY}${REPO_URL}${C_RESET}"
echo -e "${C_CYAN}========================================================================${C_RESET}"
echo
echo -e "[*] Auditing remote target: ${C_BOLD}${target_url}${C_RESET}..."
fi
# Step 1: Probe HTTP response headers
local header_file
header_file=$(mktemp 2>/dev/null || mktemp -t 'p2s_hdr')
trap 'rm -f "$header_file" "${post_body:-}" "${probe_hdr:-}"' EXIT
local http_code
http_code=$(curl -sS -L --max-time 15 -D "$header_file" -o /dev/null -w "%{http_code}" "$target_url" 2>/dev/null || true)
if [ -z "$http_code" ] || [ "$http_code" = "000" ]; then
if [ "$OPT_JSON" -eq 1 ]; then
echo "{\"error\": \"Failed to connect to target URL: $(json_escape "$target_url")\"}"
else
echo -e "${C_RED}[!] Error: Failed to establish connection to $target_url${C_RESET}"
fi
exit 1
fi
local server_hdr powered_by set_cookie
server_hdr=$(grep -i "^server:" "$header_file" | head -n 1 | awk -F': ' '{print $2}' | tr -d '\r\n')
powered_by=$(grep -i "^x-powered-by:" "$header_file" | head -n 1 | awk -F': ' '{print $2}' | tr -d '\r\n')
set_cookie=$(grep -i "^set-cookie:" "$header_file" | head -n 1 | tr -d '\r\n')
# Detect WAF / CDN
local waf_detected="NONE"
if echo "$server_hdr" | grep -qi "cloudflare"; then
waf_detected="Cloudflare"
elif grep -qi "cf-ray" "$header_file"; then
waf_detected="Cloudflare"
elif grep -qi "x-amz-cf-id" "$header_file"; then
waf_detected="AWS CloudFront / WAF"
elif grep -qi "x-sucuri-id" "$header_file"; then
waf_detected="Sucuri CloudProxy WAF"
elif grep -qi "akamai" "$header_file"; then
waf_detected="Akamai"
fi
# Step 2: Safe, tiny multipart probe (under 250 bytes)
# Does the server start a session when receiving PHP_SESSION_UPLOAD_PROGRESS?
local boundary="----php2shellcheck$$"
local post_body
post_body=$(mktemp 2>/dev/null || mktemp -t 'p2s_post')
{
printf -- '--%s\r\n' "$boundary"
printf 'Content-Disposition: form-data; name="PHP_SESSION_UPLOAD_PROGRESS"\r\n\r\np2s_probe\r\n'
printf -- '--%s\r\n' "$boundary"
printf 'Content-Disposition: form-data; name="probe_file"; filename="probe.txt"\r\nContent-Type: text/plain\r\n\r\nOK\r\n'
printf -- '--%s--\r\n' "$boundary"
} > "$post_body"
local probe_hdr
probe_hdr=$(mktemp 2>/dev/null || mktemp -t 'p2s_probe_hdr')
curl -sS --max-time 15 -X POST \
-H "Content-Type: multipart/form-data; boundary=$boundary" \
--data-binary "@$post_body" \
-D "$probe_hdr" -o /dev/null "$target_url" 2>/dev/null || true
local probe_cookie
probe_cookie=$(grep -i "^set-cookie:" "$probe_hdr" | grep -iE "(PHPSESSID|session)" | head -n 1 | tr -d '\r\n' || true)
rm -f "$post_body" "$probe_hdr"
# Headers, cookies, WAFs and ordinary multipart responses cannot prove
# allocator or session serializer safety, including on backported builds.
local remote_status="INCONCLUSIVE"
local remote_details="HTTP observations cannot confirm these CVEs or effective PHP configuration. Use local inspection and the token-gated SAPI validator."
local parsed_ver=""
parsed_ver=$(printf '%s\n' "$powered_by" | sed -n 's/.*PHP\/\([0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\).*/\1/p')
[ -n "$parsed_ver" ] && remote_details="Reported PHP ${parsed_ver}; version headers cannot verify vendor backports or serializer protection. Run local and controlled SAPI checks."
# Output
for field in target_url server_hdr powered_by; do
printf -v "$field" '%s' "$(json_escape "${!field}")"
done
if [ "$OPT_JSON" -eq 1 ]; then
cat <<EOF
{
"target": "$target_url",
"http_code": $http_code,
"server_header": "$server_hdr",
"x_powered_by": "$powered_by",
"php_version": "$parsed_ver",
"waf": "$waf_detected",
"status": "$remote_status",
"details": "$remote_details"
}
EOF
else
echo -e " [+] HTTP Response: ${C_BOLD}${http_code}${C_RESET}"
[ -n "$server_hdr" ] && echo -e " [+] Web Server Header: ${server_hdr}"
[ -n "$powered_by" ] && echo -e " [+] X-Powered-By: ${powered_by}"
[ "$waf_detected" != "NONE" ] && echo -e " [+] WAF / CDN Detected: ${C_YELLOW}${waf_detected}${C_RESET}"
echo
case "$remote_status" in
SAFE)
echo -e " Result: [ ${C_GREEN}OK: SAFE${C_RESET} ]"
echo -e " Details: ${remote_details}"
;;
MITIGATED)
echo -e " Result: [ ${C_YELLOW}WARNING: PARTIALLY MITIGATED${C_RESET} ]"
echo -e " Details: ${remote_details}"
;;
UNKNOWN)
echo -e " - Status: UNKNOWN: ${status_msg}"
;;
VULNERABLE)
echo -e " Result: [ ${C_RED}!! LEGACY OOM BEHAVIOUR OBSERVED !!${C_RESET} ]"
echo -e " Details: ${remote_details}"
echo
echo -e "${C_BOLD}RECOMMENDED ACTION:${C_RESET}"
echo -e " Apply backported security patches to the web server immediately:"
echo -e " -> ${C_CYAN}${REPO_URL}${C_RESET}"
;;
*)
echo -e " Result: [ ${C_GRAY}INCONCLUSIVE${C_RESET} ]"
echo -e " Details: ${remote_details}"
;;
esac
echo -e "${C_CYAN}========================================================================${C_RESET}"
fi
if [ "$remote_status" = "VULNERABLE" ]; then
exit 1
elif [ "$remote_status" = "MITIGATED" ]; then
exit 2
else
exit 2
fi
}
# If remote audit requested, run it now
if [ -n "$OPT_URL" ]; then
run_remote_audit
fi
# ============================================================================
# Local System Audit Engine
# ============================================================================
# Check UID
if [ "$(id -u 2>/dev/null || echo 1000)" -ne 0 ] && [ "$OPT_QUIET" -eq 0 ] && [ "$OPT_JSON" -eq 0 ]; then
echo -e "${C_YELLOW}[!] Warning: Running as non-root user (uid=$(id -u)).${C_RESET}"
echo -e " For complete audit of FPM pools and system directories, rerun with sudo.\n"
fi
WEB_SERVER=$(detect_web_server)
CONTROL_PANEL=$(detect_control_panel)
# Find all PHP binaries
PHP_BINS=()
while IFS= read -r line; do
if [ -n "$line" ]; then
PHP_BINS+=("$line")
fi
done < <(discover_php_binaries)
TOTAL_COUNT=${#PHP_BINS[@]}
SAFE_COUNT=0
PATCHED_COUNT=0
MITIGATED_COUNT=0
VULN_COUNT=0
UNKNOWN_COUNT=0
AUDIT_RESULTS=()
FIX_COMMANDS=()
for bin in "${PHP_BINS[@]}"; do
res=$(audit_php_runtime "$bin")
AUDIT_RESULTS+=("$res")
# Parse status
status=$(echo "$res" | cut -d'|' -f10)
case "$status" in
SAFE) SAFE_COUNT=$((SAFE_COUNT + 1)) ;;
PATCHED) PATCHED_COUNT=$((PATCHED_COUNT + 1)) ;;
MITIGATED) MITIGATED_COUNT=$((MITIGATED_COUNT + 1)) ;;
UNKNOWN) UNKNOWN_COUNT=$((UNKNOWN_COUNT + 1)) ;;
VULNERABLE) VULN_COUNT=$((VULN_COUNT + 1)) ;;
esac
# If vulnerable or mitigated and fix requested, prepare commands
if [ "$status" = "VULNERABLE" ] || [ "$status" = "MITIGATED" ]; then
ini_file=$(echo "$res" | cut -d'|' -f7)
branch=$(echo "$res" | cut -d'|' -f3)
if [ -n "$ini_file" ] && [ "$ini_file" != "none" ] && [ -f "$ini_file" ]; then
FIX_COMMANDS+=("sed -i 's/^[; ]*session.upload_progress.enabled\s*=.*/session.upload_progress.enabled = Off/' \"$ini_file\"")
fi
fi
done
# Render JSON output
if [ "$OPT_JSON" -eq 1 ]; then
cat <<EOF
{
"scanner": "php2shell-check",
"version": "${VERSION}",
"repository": "${REPO_URL}",
"timestamp": "$(date -u +"%Y-%m-%dT%H:%M:%SZ" 2>/dev/null || date)",
"web_server": "${WEB_SERVER}",
"control_panel": "${CONTROL_PANEL}",
"summary": {
"total": ${TOTAL_COUNT},
"safe": ${SAFE_COUNT},
"patched": ${PATCHED_COUNT},
"mitigated": ${MITIGATED_COUNT},
"unknown": ${UNKNOWN_COUNT},
"vulnerable": ${VULN_COUNT}
},
"installations": [
EOF
idx=0
for res in "${AUDIT_RESULTS[@]}"; do
bin=$(echo "$res" | cut -d'|' -f1)
ver=$(echo "$res" | cut -d'|' -f2)
branch=$(echo "$res" | cut -d'|' -f3)
sapi=$(echo "$res" | cut -d'|' -f4)
fpm_active=$(echo "$res" | cut -d'|' -f5)
fpm_listen=$(echo "$res" | cut -d'|' -f6)
ini_path=$(echo "$res" | cut -d'|' -f7)
upload_prog=$(echo "$res" | cut -d'|' -f8)
patch_app=$(echo "$res" | cut -d'|' -f9)
status=$(echo "$res" | cut -d'|' -f10)
status_msg=$(echo "$res" | cut -d'|' -f11)
ssl_transport=$(echo "$res" | cut -d'|' -f12)
tls_transport=$(echo "$res" | cut -d'|' -f13)
serializer=$(echo "$res" | cut -d'|' -f14)
idx=$((idx + 1))
comma=","
if [ "$idx" -eq "$TOTAL_COUNT" ]; then
comma=""
fi
for field in bin ver branch sapi fpm_listen ini_path status_msg serializer; do
printf -v "$field" '%s' "$(json_escape "${!field}")"
done
cat <<EOF
{
"path": "$bin",
"version": "$ver",
"branch": "$branch",
"sapi": "$sapi",
"fpm_active": $([ "$fpm_active" = "YES" ] && echo "true" || echo "false"),
"fpm_listen": $([ -n "$fpm_listen" ] && echo "\"$fpm_listen\"" || echo "null"),
"ini_path": "$ini_path",
"upload_progress": $(case "$upload_prog" in ENABLED) echo true ;; DISABLED) echo false ;; *) echo null ;; esac),
"ssl_transport": $(case "$ssl_transport" in YES) echo true ;; NO) echo false ;; *) echo null ;; esac),
"tls_transport": $(case "$tls_transport" in YES) echo true ;; NO) echo false ;; *) echo null ;; esac),
"serialize_handler": "$serializer",
"patch_applied": $([ "$patch_app" = "YES" ] && echo "true" || echo "false"),
"status": "$status",
"details": "$status_msg"
}$comma
EOF
done
cat <<EOF
]
}
EOF
# Exit with code
if [ "$VULN_COUNT" -gt 0 ]; then
exit 1
elif [ "$MITIGATED_COUNT" -gt 0 ] || [ "$UNKNOWN_COUNT" -gt 0 ] || [ "$TOTAL_COUNT" -eq 0 ]; then
exit 2
else
exit 0
fi
fi
# Render Human Terminal Output
if [ "$OPT_QUIET" -eq 0 ]; then
echo -e "${C_CYAN}========================================================================${C_RESET}"
echo -e "${C_BOLD} php2shell-check — CVE-2026-109618, CVE-2026-109619 & CVE-2026-115560 Audit (v${VERSION})${C_RESET}"
echo -e " ${C_GRAY}${REPO_URL}${C_RESET}"
echo -e "${C_CYAN}========================================================================${C_RESET}"
echo
echo -e "[*] Scanning active web services and PHP runtimes..."
echo -e " [+] Web server detected: ${C_BOLD}${WEB_SERVER}${C_RESET}"
echo -e " [+] Control panel: ${C_BOLD}${CONTROL_PANEL}${C_RESET}"
echo
echo -e "[+] Found ${C_BOLD}${TOTAL_COUNT}${C_RESET} PHP installation(s):"
echo
idx=0
for res in "${AUDIT_RESULTS[@]}"; do
idx=$((idx + 1))
bin=$(echo "$res" | cut -d'|' -f1)
ver=$(echo "$res" | cut -d'|' -f2)
branch=$(echo "$res" | cut -d'|' -f3)
sapi=$(echo "$res" | cut -d'|' -f4)
fpm_active=$(echo "$res" | cut -d'|' -f5)
fpm_listen=$(echo "$res" | cut -d'|' -f6)
ini_path=$(echo "$res" | cut -d'|' -f7)
upload_prog=$(echo "$res" | cut -d'|' -f8)
patch_app=$(echo "$res" | cut -d'|' -f9)
status=$(echo "$res" | cut -d'|' -f10)
status_msg=$(echo "$res" | cut -d'|' -f11)
ssl_transport=$(echo "$res" | cut -d'|' -f12)
tls_transport=$(echo "$res" | cut -d'|' -f13)
serializer=$(echo "$res" | cut -d'|' -f14)
fpm_note=""
if [ "$fpm_active" = "YES" ]; then
if [ -n "$fpm_listen" ]; then
fpm_note=" (FPM active on ${fpm_listen})"
else
fpm_note=" (FPM process active in memory)"
fi
fi
echo -e "[${idx}] ${C_BOLD}${bin}${C_RESET}${C_GRAY}${fpm_note}${C_RESET}"
echo -e " - Version: ${ver}"
echo " - upload_progress: ${upload_prog} (SAPI defaults; pool overrides unverified)"
echo " - SSL/TLS streams: ssl=${ssl_transport}, tls=${tls_transport} (availability only; no connection attempted)"
echo " - Serializer: ${serializer} (SAPI default)"
echo -e " - Patch applied: $([ "$patch_app" = "YES" ] && echo -e "${C_GREEN}YES${C_RESET}" || echo "NO")"
if [ "$ini_path" != "none" ]; then
echo -e " - Loaded ini: ${ini_path}"
fi
case "$status" in
SAFE)
echo -e " - Status: [ ${C_GREEN}OK: SAFE${C_RESET} ] (${status_msg})"
;;
PATCHED)
echo -e " - Status: [ ${C_GREEN}OK: PATCHED${C_RESET} ] (${status_msg})"
;;
MITIGATED)
echo -e " - Status: [ ${C_YELLOW}WARNING: PARTIALLY MITIGATED${C_RESET} ]"
echo -e " ${C_GRAY}${status_msg}${C_RESET}"
;;
UNKNOWN)
echo -e " - Status: UNKNOWN: ${status_msg}"
;;
VULNERABLE)
echo -e " - Status: [ ${C_RED}!! LEGACY OOM BEHAVIOUR OBSERVED !!${C_RESET} ]"
echo -e " ${C_RED}${status_msg}${C_RESET}"
;;
esac
echo
done
echo -e "${C_CYAN}------------------------------------------------------------------------${C_RESET}"
echo -e "${C_BOLD}SUMMARY:${C_RESET}"
echo -e " Total installations scanned: ${C_BOLD}${TOTAL_COUNT}${C_RESET}"
echo -e " Safe / Patched: ${C_GREEN}$((SAFE_COUNT + PATCHED_COUNT))${C_RESET}"
echo " Unknown / unverified: ${UNKNOWN_COUNT}"
echo -e " Partially verified: ${C_YELLOW}${MITIGATED_COUNT}${C_RESET}"
echo -e " Critically Vulnerable: $([ "$VULN_COUNT" -gt 0 ] && echo -e "${C_RED}${VULN_COUNT}${C_RESET}" || echo -e "${C_GREEN}0${C_RESET}")"
echo
if [ "$VULN_COUNT" -gt 0 ] || [ "$MITIGATED_COUNT" -gt 0 ] || [ "$UNKNOWN_COUNT" -gt 0 ] || [ "$TOTAL_COUNT" -eq 0 ] || [ "$OPT_FIX" -eq 1 ]; then
echo -e "${C_BOLD}ACTION REQUIRED / REMEDIATION:${C_RESET}"
# aaPanel hint
if [[ "$CONTROL_PANEL" == *"aaPanel"* ]]; then
echo -e " ${C_BOLD}[aaPanel detected]${C_RESET} Apply automated in-place build & swap:"
echo -e " -> ${C_CYAN}./deploy/swap-aapanel-php.sh install <ver>${C_RESET} (e.g. 74, 80, 81)"
echo
fi
# Distro package / community backport hints
echo -e " ${C_BOLD}[Recommended]${C_RESET} Rebuild/patch your vulnerable EOL runtimes with repository patches:"
echo -e " -> ${C_CYAN}${REPO_URL}#available-patches${C_RESET}"
echo
# Emergency configuration mitigation
if [ ${#FIX_COMMANDS[@]} -gt 0 ]; then
echo -e " ${C_BOLD}[Emergency Mitigation]${C_RESET} Disable upload_progress in configuration right now:"
for cmd in "${FIX_COMMANDS[@]}"; do
echo -e " -> ${C_YELLOW}${cmd}${C_RESET}"
done
echo
echo -e " Then restart your PHP-FPM service (e.g. ${C_CYAN}systemctl restart php*-fpm${C_RESET} or ${C_CYAN}service php-fpm restart${C_RESET})."
fi
echo -e "${C_CYAN}========================================================================${C_RESET}"
else
echo -e "${C_GREEN}All discovered runtimes have patch evidence; verify effective web SAPI settings.${C_RESET}"
echo -e "${C_CYAN}========================================================================${C_RESET}"
fi
fi
# Exit code: 0 if all safe/patched, 1 if vulnerable, 2 if mitigated
if [ "$VULN_COUNT" -gt 0 ]; then
exit 1
elif [ "$MITIGATED_COUNT" -gt 0 ] || [ "$UNKNOWN_COUNT" -gt 0 ] || [ "$TOTAL_COUNT" -eq 0 ]; then
exit 2
else
exit 0
fi