Avoid parsing ps output for process matching

이 제품은 선택한 Datadog 사이트에서 지원되지 않습니다. ().
이 페이지는 아직 한국어로 제공되지 않습니다. 번역 작업 중입니다.
현재 번역 프로젝트에 대한 질문이나 피드백이 있으신 경우 언제든지 연락주시기 바랍니다.

Metadata

ID: bash-security/avoid-parsing-ps-output

Language: Bash

Severity: Warning

Category: Security

CWE: 88

Description

Text from ps depends on locale, column layout, and flags, so piping it into tools that match or reshape lines is easy to get wrong and can match unrelated fields (CWE-88). Prefer pgrep / pkill when you only need PIDs or simple patterns, or read /proc where appropriate. If you need fields ps provides, combine pgrep to select PIDs with a focused ps -p (or similar) instead of grepping full ps listings.

Non-Compliant Code Examples

#!/bin/bash
ps ax | grep foo
/bin/ps -ef | grep -v grep
ps aux | awk '{print $2}'
ps -eo pid,cmd | sed -n '1p'
ps | cut -d' ' -f1
ps | perl -ne 'print if /foo/'
ps |& grep bar

Compliant Code Examples

#!/bin/bash
ps
pgrep -f myservice
pkill -f oldworker
ps -p "$(pgrep -x sshd)" -o user=
psql -c 'select 1' | grep foo
ps -eo user,pid,%cpu,comm | awk -v usr="$user" '$1==usr'
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

원활한 통합. Datadog Code Security 사용해 보기