해당 취약점은 2011년 7월에 도입 되어 2021년 1월에 취약점이 공개 되었습니다. 취약점은 계속 있었으나 약 10년만에 발견되었던 만큼 취약점이 해당하는 버전이 많아 대부분의 시스템이 취약할 것으로 생각됩니다.
시연 영상을 보시면 아시겠지만 계정 패스워드를 알지 못해도 루트 권한을 획득할 수 있으며, Ubuntu 20.04 (sudo 1.8.31), Debian 10 (Sudo 1.8.27), Fedora 33 (Sudo 1.9.2) 등 여러 배포판 리눅스에서 전체 루트 권한을 획득할 수 있었다고 합니다.
취약점을 발견한 Qualys 연구팀에서는 취약점에 대한 악용 코드를 게시하지 않겠다고 하여 당장 Baron Samedit 취약점에 대해 문제가 발생하지는 않겠지만, 기술적인 내용들을 게시했으므로 추후 악용될 가능성은 충분해 보입니다.
1) 취약점 테스트
아래 명령어를 입력하여 취약한지 테스트가 가능 합니다.
sudoedit -s /
취약한 경우 : "sudoedit :" 으로 시작하는 오류 응답
패치된 경우 : "usage :" 으로 시작하는 오류 응답
2-1) 임시 패치 방법 (RedHat)
스크립트를 사용해 sudoedit 바이너리의 작동을 중지 시키는 방법이며, 이 방법은 임시로 스크립트를 돌리는 방식이기 때문에 재부팅할 때 마다 적용 시켜주어야 하는 번거로움이 있습니다.
RedHat에서는 아래와 같은 방법을 제공 했는데, 그냥 전체 복사 붙여넣기 해서는 안되니 아래 내용 참고하여 적용 하셔야 합니다.
* 버전 차이(7, 8)에 따라 debuginfo-install 에서 설치받는 패키지가 다르니 참고 해주세요.
* 보안 패치 후 systemtap script를 중지 시키는 라인(맨 마지막)이 있습니다.
## 1. Install required systemtap packages and dependencies, example:
yum install systemtap yum-utils kernel-devel-"$(uname -r)"
# Then for RHEL 7 install kernel debuginfo, using:
debuginfo-install -y kernel-"$(uname -r)"
# Then for RHEL 8 install sudo debuginfo, using:
debuginfo-install sudo
## 2. Create the following systemtap script: (call the file as sudoedit-block.stap)
probe process("/usr/bin/sudo").function("main") {
command = cmdline_args(0,0,"");
if (strpos(command, "edit") >= 0) {
raise(9);
}
}
## 3. Install the script using the following command: (using root)
# (This should output the PID number of the systemtap script)
# This script will cause the vulnerable sudoedit command to stop working. The sudo command will still work as usual.
# The above change does not persist across reboots and must be applied after each reboot.
nohup stap -g sudoedit-block.stap &
## 4. Once the new fixed packages are installed, the systemtap script can be removed by killing the systemtap process. For example, by using:
# Warning: Do not attempt to disable sudoedit by removing the symlink as this is not a sufficient mitigation option.
kill -s SIGTERM 7590 (where 7590 is the PID of the systemtap process)
룰셋 적용 후 차단하는 방식(IPS)가 아니라, 탐지된 내용을 로깅만 하는 방식(IDS)으로 구성 합니다.
룰셋 적용 자체에 큰 어려움은 없기에 하나하나 자세히 설명하기보다는 코드블럭에 주석처리로 간단히 설명만 해두었습니다. 적용 이후에 공격을 시도하면 /var/log/httpd/modsec.log 파일에서 공격이 탐지되는것을 확인할 수 있습니다.
## OWASP 룰셋 설치
cd /etc/http
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs.git
mkdir /etc/http/modsecurity.d
mkdir /etc/http/modsecurity.d/activated_rules
cd owasp-modsecurity-crs
mv crs-setup.conf.example /etc/http/modsecurity.d/crs-setup.conf
cd rules
mv ./* /etc/http/modsecurity.d/activated_rules/
cd /etc/http/modsecurity.d/activated_rules/
mv REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example REQUEST-900-EXCLUSION-RULES-BEFORE-CRS.conf
mv RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf.example RESPONSE-999-EXCLUSION-RULES-AFTER-CRS.conf
## OWASP 룰셋 적용
vim /etc/http/conf.d/mod_security.conf
<IfModule mod_security2.c>
Include /etc/httpd/modsecurity.d/crs-setup.conf # 반드시 아래 *.conf보다 위로 올라와야 합니다.
Include /etc/httpd/modsecurity.d/activated_rules/*.conf
SecRuleEngine DetectionOnly # On으로 설정할 경우 차단까지 되며, DetectionOnly로 해주어야 탐지만 합니다.
SecAuditEngine On
SecAuditLog /var/log/httpd/modsec.log
SecAuditLogType Serial
SecRequestBodyAccess on
SecAuditLogParts ABIJDFHZ
SecDataDir /tmp/mod_data
</IfModule>
:wq
vim /etc/http/conf/httpd.conf
LoadModule security2_module modules/mod_security2.so ## 전 57라인에 넣었습니다.
:wq
systemctl restart httpd
아파치 modsecurity2 소스 설치 (apache modsecurity2 install)
아래와 같이 의존성 및 컴파일을 위한 패키지를 설치 해줍니다.
yum -y install gcc gcc-c++ httpd-devel pcre-devel
wget https://ftp.pcre.org/pub/pcre/pcre-8.43.tar.gz
tar xvfz pcre-8.00.tar.gz
cd pcre-8.43
./configure --prefix=/usr/local/pcre
make
make install
이후 아래와 같이 modsecurity2를 직접 설치 합니다. 만약, libxml 에러가 발생할 경우 아래 링크를 참고하여 해결해주신 후 진행 하시면 됩니다.
yum -y install gcc-c++ httpd-devel pcre-devel libxml2-devel
wget https://www.modsecurity.org/tarball/2.9.1/modsecurity-2.9.1.tar.gz
tar -zxvf modsecurity-2.9.1.tar.gz
cd modsecurity-2.9.1
./configure
make
make install