httpd-2.2.3, php-5.2.0 + ssl 의 설치내용입니다.
ssl설치하다 아파치의 conf파일설정이 2.2.0부터는 변경되 삽질을 좀 했습니다.
그래서 변경된 설정내용을 적었습니다.
httpd.conf 가 여러개로 나누어졌다는 것이 주내용입니다.
conf/extra 아래 여러 conf 파일이 있습니다.
기본적인 설치내용이며 ssl은 rpm으로 기본설치된 상태에서 했습니다.
php 컴파일시 xml관련 필요한 프로그램을 요구하는데,
설치하거나 옵션을 disable 해야합니다.
------------------------------------------------------------------------------------
위치 : /usr/local/src/httpd-2.2.3
./configure --prefix=/usr/local/apache2 --enable-so --enable-ssl --with-ssl=/usr
make
make install
위치 : /usr/local/src/php-5.2.0
./configure --prefix=/usr/local/php5 --with-mysql --with-apxs2=/usr/local/apache2/bin/apxs
make
make install
cp php.ini-dist /usr/local/php5/lib/php.ini
위치 : /usr/local/apache2/
vim conf/httpd.conf
DocumentRoot "/www/htdocs"
ServerAdmin ADMIN@DOMAIN.COM
# <Directory "/">
# Order deny,allow
# Deny from all
# </Directory>
# 이런설정이 디폴트여서 사용하는 디렉토리의 Allow를 해주지 않으면
# 웹에서 접근시 403에러가 난다.
# 웹에서 사용되는 가상호스트의 디렉토리에 대해 Allow from all 한다.
<Directory "/www/htdocs">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
<Directory "/www/htdocs2">
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
AddType application/x-httpd-php .php
# 가상호스트사용을 위해서는 주석을 제거
Include conf/extra/httpd-vhosts.conf
# ssl사용을 위해서는 주석을 제거
Include conf/extra/httpd-ssl.conf
vim conf/extra/httpd-vhosts.conf
# 가상호스트설정 내용이 파일에 들어 있으니 수정해야 한다.
vim conf/extra/httpd-ssl.conf
# DocumentRoot, ServerName, SSLCertificateFile, SSLCertificateKeyFile
# 이와 관련된 내용만 수정하면 됨
# 인증관련파일은 인증서 구입한 곳에서 인증서생성 메뉴얼을 주더군요.
# 구입한 인증서 crt, key 파일 위치로 변경
http://a.site 로 접속 하는 주소를 http://a.site:8000 으로 포워딩 하고 싶을때. 뭐... a.site 로 들어와도 프로그램의 변수로 구분을 해서 들어가도 되겠지만 특별한 경우 a.site 로 들어왔는데 포트를 달아줘야 할경우 사용하면 된다. html의 meta 테그를 써도 되지만 Apache 차원의 포워딩을 해보자
ModSecurity 는 아파치(apache)에서 사용하는 대표적인 웹방화벽 모듈이다. 아파치에 모듈을 설치하고, 룰(Rule) 설정을 통해 설정한 조건에 맞는 경우 차단을 할 수 있다. modsecurity 2.x 을 기준으로한 간단한 예이다.
- 웹서버명을 숨기거나 속인다.
SecServerSignature "lighttpd"
- 특정 메소드의 사용만 허용한다. (POST, GET, OPTIONS, HEAD 메소드만 허용)
SecRule REQUEST_METHOD "!^((?:(?:POS|GE)T|OPTIONS|HEAD))$" \ "phase:1,log,auditlog,status:501,msg:'Method is not allowed by policy', severity:'2',,id:'960032',"
- 요청한 HTTP 프로토콜 버전이 1.0, 1.1이 아닐 경우 차단한다.
SecRule REQUEST_PROTOCOL "!^HTTP/(1\.[01])$" \ "t:none, deny,log,auditlog,status:505,msg:'HTTP protocol version is not allowed by policy', severity:'2',,id:'960034',"
- GET, HEAD 메소드는 Content-Length가 0이 아닌 경우는 차단하고, POST는 Content-Length header가 없으면 차단한다.
SecRule REQUEST_METHOD "^(GET|HEAD)$" "chain,deny,log,auditlog,status:400,msg:'GET or HEAD requests with bodies', severity:'2',,id:'960011'," SecRule REQUEST_HEADERS:Content-Length "!^0?$"
SecRule REQUEST_METHOD "^POST$" "chain,deny,log,auditlog,status:400,msg:'POST request must have a Content-Length header',,id:'960012',severity:'4'" SecRule &REQUEST_HEADERS:Content-Length "@eq 0"
오픈소스 IDS인 snort에 기본 룰을 제공하는 것처럼 ModSecurity 에서도 modsecurity-core-rules 이름으로 룰 파일을 제공하고 있으니 참고하기 바란다. 룰에 대해서는 이만하고 원래 꺼내려한 얘기거리로 들어가자.
REMO를 사용하기 위해서는 ruby 1.8.2이상, irb, sqlite3-ruby 환경이 필요하다. 또한 ModSecurity 모듈이 설치되지 않은 테스트나 개발 서버, 개인 PC 등에 설치해도 무관하다. 다음과 같이 실행한 후 http://서버:3000/main/index 로 접속하면 설정화면을 볼 수 있다.
wget http://remo.netnea.com/files/remo-0.2.0.tar.gz tar xvzf remo-0.2.0.tar.gz cd remo-0.2.0 ruby script/server
REMO화면에서 메소드와 URI등을 새로 입력한 다음, 원하는 조건을 정의한다. 그 후 'generate' 버튼을 누르면 파일로 룰셋 파일을 다운로드 받을 수 있다. 받은 룰셋을 다음과 같이 apache 설정에서 include해주면 된다.
<IfModule mod_security2.c> Include /파일경로/rulefile.conf </IfModule>
REMO툴은 ModSecurity 룰 생성의 모든 것을 제공해주지는 않는다. modsecurity-core-rules 룰 파일을 보면 룰 설정이 쉽지않다는 것을 알 수 있다. 이 툴은 이런 복잡한 룰을 보고 고개를 설레설레 젓지 않도록 보조적인 역할을 하는 툴로 여기면 된다.
AuthName WebLog ## 인증창의 이름 AuthType Basic ## 인증 타입 보통 Basic를 사용 AuthUserFile /usr/local/apache/htdocs/weblog/.htpasswd ## htpasswd를 이용하여 생성되는 .htpasswd의 위치 AuthGroupFile /dev/null ## 그룹으로 인증할경우는 그룹 파일을 아닐경우 null 을 넣는다. <Limit GET POST> require valid-user ## 인증된 사용자만 접속 허용 하기 </Limit>
htpasswd -n[mdps] username htpasswd -nb[mdps] username password -c Create a new file. -n Don't update file; display results on stdout. -m Force MD5 encryption of the password. -d Force CRYPT encryption of the password (default). -p Do not encrypt the password (plaintext). -s Force SHA encryption of the password. -b Use the password from the command line rather than prompting for it. -D Delete the specified user. On Windows, NetWare and TPF systems the '-m' flag is used by default. On all other systems, the '-p' flag will probably not work.
#이미지가 새창을 열경우 엑박(X)으로 보이는걸 방지하기 위한 레퍼럴이 존재하지 않는 요청 SerEnvIfNoCase Referer ^$ goout
# 파일 확장자 지정하기 <Files ~ "\.(gif|jpe?g|png|bmp|zip|tar|rar|alz|a00|ace|txt|mp3|mpe?g|wav|asf|wma|wmv|swf|exe)$"> Order deny,allow deny from all allow from env=goout
## 무단 링크시 띄워줄 에러 ErrorDocument 403 http:// 경고를 보여줄 Img 나 html </Files> </Directory>
제 목 : 아파치 설정 활용 예(서비스 제한측면) 작성자 : 좋은진호(truefeel, http://coffeenix.net/ ) 작성일 : 2003.2~ 정리일 : 2005.1.28(금)
아파치 웹서버의 활용에 있어, 보다나은 보안 설정 방법 몇 가지를 소개한다.
특정 Agent가 자꾸 사이트에 들어와서 긁어간다든지, 이미지 파일을 외부에서 링크하지 못하도록 한다든지, 특정 IP에서는 인증없이, 그 이외 IP에서는 아파치 인증을 통해서만 페이지를 보게하는 등의 응용적인 측면에서 방법들이다. 이 방법들은 제가 사용하고 있는 몇가지 방법을 정리하는 의미에서 적은 것이다.
<Directory /usr/local/apache/htdocs> <Limit GET POST> Order Allow,Deny Allow from all Deny from env=dont_want </Limit> </Directory> [/quote] --------------------------------------------------------------------
The 'SetEnvIfNoCase' simply sets an enviornment (SetEnv) variable called 'bad_bot' If (SetEnvIf) the 'User-Agent' string contains Wget, EmailSiphon, or EmailWolf, regardless of case (SetEnvIfNoCase). In english, anytime a browser with a name containing 'wget, emailsiphon, or emailwolf' accesses our website, we set a variable called 'bad_bot'. We'd also want to add a line for the User-Agent string of any other Spidert we want to deny.
Now we tell Apache which directories to block the Spiderts from with the <Directory> directive:
<Directory "/home/evolt/public_html/users/"> Order Allow,Deny Allow from all Deny from env=bad_bot </Directory>
2. 다른 곳에서 images를 link하지 못하도록
1) 디렉토리를 제한하는 방법
referer 체크를 통해 외부에서 /images 이하디렉토리의 파일을 link하지 못하도록 한다. 이를테면 truefeel.kr 이라는 사이트가 있을 때 truefeel.kr내에 있는 동영상을 이 도메인에서는 링크할 수 있지만 외부 사이트에서는 이 동영상을 링크해도 권한이 없도록 할 수 있다.
<Directory /usr/local/apache/htdocs/images> Order Deny,Allow Deny from all Allow from env=local_referal </Directory> --------------------------------------------------------------------
2) 이미지 파일 확장자로 제한하는 방법 (서비스 페이지가 ????.truefeel.kr 일 때)
<Files ~ "\.(gif|jpg|jpeg|png|bmp)$"> Order deny,allow Deny from all Allow from env=local_referal </Files> --------------------------------------------------------------------
3. 특정 IP에서만 접근 가능하게
너무나도 흔한 방법이긴 한데, 개인적으로 저에게 이런 부분을 물어보시는 분이 많았다. 아래의 예는 /usr/local/apache/htdocs 디렉토리를 211.111.222.0/24 대역에서만 접근가능한 설정이다.
-------------------------------------------------------------------- <Directory "/usr/local/apache/htdocs"> Options FollowSymLinks MultiViews AllowOverride AuthConfig Order deny,allow Deny from all Allow from 211.111.222.0/255.255.255.0 </Directory> --------------------------------------------------------------------
반대로 아래와 같이 하면 지정한 IP대역에서만 접근할 수 없다.
-------------------------------------------------------------------- ... 생략 ... Order allow,deny Allow from all Deny from 211.111.222.0/255.255.255.0 ... 생략 ... --------------------------------------------------------------------
4. 특정페이지를 인증된 사용자만 볼 수 있는 아파치 인증 설정
이번 글은 5번의 인증 설정을 알기 전에 아파치 인증에 대해 모르는 분을 위해 적은 것으로 아파치 인증이 무엇인지 안다면 5번으로 바로 넘어가기 바란다.
아파치에서는 특정 페이지를 접근할 때 ID와 비밀번호를 알아야만 접근하도록 설정할 수 있다. 다음 예를 보자.
위 처럼 AllowOverride AuthConfig 설정을 하면 ~truefeel/public_html 디렉토리에 인증 과정을 거치는 기본 설정은 된 것이다. 만약 ~truefeel/public_html/manager/ 아래의 디렉토리에 인증을 걸려고 한다면 그 디렉토리에 다음과 같은 형식의 .htaccess 파일을 생성한다.
[ ~truefee/public_html/manger/.htaccess 파일 내용 ] -------------------------------------------------------------------- AuthType Basic AuthName User <-- "User" 는 인증창에 표시될 메시지 중의 하나일 뿐 신경쓸 필요 없다. AuthUserFile /home/truefeel/manage/.htpasswd AuthGroupFile /dev/null <Limit GET POST> require valid-user </Limit> --------------------------------------------------------------------
require valid-user order deny,allow deny from all allow from 211.111.222.0/255.255.255.0 Satisfy any </Location> --------------------------------------------------------------------
위의 설정은 - 211.111.222.0/24 IP 대역에 있는 접속자는 인증없이 통과하고, - 그 이외의 IP 대역에서 접속하면 인증을 거쳐야하는 설정이다. 이 설정은 Satisfy any 지시자를 통해서 이뤄진다. IP가 맞거나 ID/PW가 맞으면 되는 것이다. (OR)
만약 Satisfy all 이라고 적었다면 - IP대역이 211.111.222.0/24 이면서 - 인증까지 모두 통과 해야 해당 페이지에 접근이 가능하다. (AND)
6. 업로드 디렉토리의 .php파일은 text처럼 인식하기
php나 cgi 등으로 파일 업로드 가능하도록 구현된 경우 .php나 .html 등의 파일을 업로드할 수 있는 실수를 범할 수 있다.
이런 실수를 하더라도 지정한 업로드 디렉토리에 있는 .php나 .html 파일을 php프로그램으로 인식하지 않고 일반 텍스트 파일로 판단하도록 설정하여 보다 안전한 웹페이지 구성이 가능하다. 다음은 /upload/ 와 /files/ 디렉토리 아래의 php파일은 일반 텍스트로 인식하라는 설정이다.