.htaccess 파일은 Apache 웹 서버에서 디렉토리별 설정을 제어하는 강력한 도구로, 보안, 접근 제어, URL 재작성, 캐싱 등 다양한 설정을 지원합니다. 웹쉘 공격 방어와 같은 파일 업로드 관련 보안 조치를 포함한 .htaccess 파일의 사용법을 아래에 통합하여 설명하겠습니다.
접근 제어:
URL 재작성:
디렉토리 인덱싱:
캐싱:
MIME 타입 설정:
보안 설정:
.htaccess 파일은 웹 서버의 디렉토리에 위치하며, 설정 구문을 추가하여 다양한 동작을 정의합니다. 예를 들어:
# 접근 제어 예제: 특정 IP 주소만 접근 가능하게 설정
<RequireAny>
Require ip 192.168.1.1
Require ip 10.0.0.0/8
</RequireAny>
# URL 재작성 예제: 사용자 친화적인 URL 설정
RewriteEngine On
RewriteRule ^about$ about.php [L]
# 디렉토리 인덱싱 비활성화
Options -Indexes
# 캐싱 설정
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 1 month"
ExpiresByType image/png "access plus 1 month"
ExpiresByType text/css "access plus 1 week"
</IfModule>
# MIME 타입 설정
AddType image/svg+xml svg svgz
AddEncoding gzip svgz
# 보안 설정: .htaccess 파일에 대한 접근 차단
<Files .htaccess>
Order allow,deny
Deny from all
</Files>
웹쉘은 서버에 악성 스크립트를 업로드하여 원격에서 명령을 실행할 수 있는 공격 기법입니다. 이를 방어하기 위해 다음과 같은 설정을 적용할 수 있습니다.
# PHP 파일 및 기타 스크립트 파일 업로드 차단
<FilesMatch "\.(php|php3|php4|php5|phtml|pl|py|jsp|asp|aspx|sh|cgi)$">
Order allow,deny
Deny from all
</FilesMatch>
# 업로드 디렉토리 내 모든 파일에 대해 실행 방지
<Directory "/path/to/upload/directory">
<FilesMatch ".*">
SetHandler none
Options -ExecCGI
</FilesMatch>
</Directory>
# 허용된 파일 형식만 업로드 허용
<FilesMatch "\.(jpg|jpeg|png|gif|pdf)$">
Order allow,deny
Allow from all
</FilesMatch>
# 다른 모든 파일 형식 업로드 차단
<FilesMatch "\.(?!jpg|jpeg|png|gif|pdf$)[^.]+$">
Order allow,deny
Deny from all
</FilesMatch>
<Directory "/path/to/upload/directory">
AllowOverride None
Options -ExecCGI -Indexes
AddType text/plain .php .php3 .php4 .php5 .phtml .pl .py .jsp .asp .aspx .sh .cgi
</Directory>
.htaccess 파일을 통해 Apache 웹 서버의 다양한 설정을 제어할 수 있습니다. 웹쉘 공격 방어를 위해서는 PHP 및 기타 스크립트 파일의 업로드를 차단하고, 업로드된 파일이 실행되지 않도록 설정하며, MIME 타입 검증을 통해 허용된 파일 형식만 업로드할 수 있도록 설정하는 것이 중요합니다. 이를 통해 서버의 보안을 강화하고 잠재적인 보안 위협을 줄일 수 있습니다.