원출처 : http://www.mungchung.com/mianamssi/zboard/view.php?id=protip&page=1&sn1=&divpage=1&category=3&sn=off&ss=on&sc=off&select_arrange=headnum&desc=asc&no=366





< %
'////////////////////////////////////////////////////////////////////
'//가. 명령어 삽입(Command Injection) 가능성
'////////////////////////////////////////////////////////////////////
Dim title, str
title = "What's Up!!! Oh my god!!!! & goodness"
str = ""
//변환을 수행할 함수
Sub ReplaceStr(content, byref str)
content = replace(content, "'", """)
content = replace(content, "&", "&")
content = replace(content, "<", "<")
content = replace(content, ">", ">")

str = content
End Sub

ReplaceStr title, str
response.write str

%>

'////////////////////////////////////////////////////////////////////
'//나. 크로스 사이트 스크립팅 (XSS) 가능성
'////////////////////////////////////////////////////////////////////
/include/config.inc.asp
< %
atag = "p,br" 'XSS 허용할 태그 리스트
UploadedPath = "/Uploaded_Files/" '업로드 기본 경로
fileext = "jpg,gif,png,pcx" '허용할 확장자 리스트
%>


/include/secure.inc.asp
< %
'공격 위험성이 존재하는 문자들을 필터링
'문자열 입력값을 검증
'숫자형은 데이터 타입을 별도로 체크하도록 한다.
Function sqlFilter(search)
Dim strSearch(5), strReplace(5), cnt, data

'SQL Injection 특수문자 필터링
'필수 필터링 문자 리스트
strSearch(0)="'"
strSearch(1)=""""
strSearch(2)="\"
strSearch(3)=null
strSearch(4)="#"
strSearch(5)="--"
strSearch(6)=";"

'변환될 필터 문자
strReplace(0)="''"
strReplace(1)=""""""
strReplace(2)="\\"
strReplace(3)="\"&amp;null
strReplace(4)="\#"
strReplace(5)="\--"
strReplace(6)="\;"

data = search
For cnt = 0 to 6 '필터링 인덱스를 배열 크기와 맞춰준다.
data = replace(data, LCASE(strSearch(cnt)), strReplace(cnt))
Next

sqlFilter = data
End Function

'XSS 출력 필터 함수
'XSS 필터 함수
'$str - 필터링할 출력값
'$avatag - 허용할 태그 리스트 예) $avatag = "p,br"
Function clearXSS(strString, avatag)
'XSS 필터링
strString = replace(strString, "<", "<")
strString = replace(strString, "\0", "")

'허용할 태그 변환
avatag = replace(avatag, " ", "") '공백 제거
If (avatag <> "") Then
taglist = split(avatag, ",")

for each p in taglist
strString = replace(strString, "<"&amp;p&amp;" ", "<"&amp;p&amp;" ", 1, -1, 1)
strString = replace(strString, "<"&amp;p&amp;">", "<"&amp;p&amp;">", 1, -1, 1)
strString = replace(strString, " next
End If

clearXSS = strString
End Function

'확장자 검사
'$filename: 파일명
'$avaext: 허용할 확장자 예) $avaext = "jpg,gif,pdf"
'리턴값: true-"ok", false-"error"
Function Check_Ext(filename,avaext)
Dim bad_file, FileStartName, FileEndName

If instr(filename, "\0") Then
Response.Write "허용하지 않는 입력값"
Response.End
End If

'업로드 금지 확장자 체크
bad_file = "asp,html,htm,asa,hta"

filename = Replace(filename, " ", "")
filename = Replace(filename, "%", "")

FileStartName = Left(filename,InstrRev(filename,".")-1)
FileEndName = Mid(filename, InstrRev(filename, ".")+1)

bad_file = split(bad_file, ",")

for each p in bad_file
if instr(FileEndName, p)>0 then
Check_Ext = "error"
Exit Function
end if
next

'허용할 확장자 체크
if avaext <> "" Then
ok_file = split(avaext, ",")

for each p in ok_file
if instr(FileEndName, p)>0 then
Check_Ext = "ok"
Exit Function
End If
next
End If

Check_Ext = "error"
End Function

'다운로드 경로 체크 함수
'$dn_dir - 다운로드 디렉토리 경로(path)
'$fname - 다운로드 파일명
'리턴 - true:파운로드 파일 경로, false: "error"
Function Check_Path(dn_dir, fname)
'디렉토리 구분자를 하나로 통일
dn_dir = Replace(dn_dir, "/", "\")
fname = Replace(fname, "/", "\")

strFile = Server.MapPath(dn_dir) &amp; "\" &amp; fname '서버 절대경로

strFname = Mid(fname,InstrRev(fname,"\")+1) '파일 이름 추출, ..\ 등의 하위 경로 탐색은 제거 됨
Response.Write strFname

strFPath = Server.MapPath(dn_dir) &amp; "\" &amp; strFname '웹서버의 파일 다운로드 절대 경로

If strFPath = strFile Then
Check_Path = strFile '정상일 경우 파일 경로 리턴
Else
Check_Path = "error"
End If
End Function

'IP 체크 함수
Function Check_IP(IP_Addr)
If Request.Servervariables("REMOTE_ADDR") = IP_Addr Then
Check_IP = "TRUE"
Else
Check_IP = "FALSE"
End If
End Function
%>



/head.asp
< %
'페이지에서 에러가 발생하여도 페이지 오류를 외부로 출력하지 않기위해 사용
On Error Resume Next
'On Error GoTo 0도 가능하나 2003에서는 실행되지 않음
if err.number <> 0 then
'Response.Write err.description &amp; "
" &amp; err.source &amp; "
"
err.clear
End if
%>


/content.asp
< !--#include virtual="/include/connection.inc.asp"--> <% 'DB연결 헤더 %>
< !--#include virtual="/include/secure.inc.asp"--> <% '보안관련라이브러리 %>
< !--#include virtual="/include/config.inc.asp"--> <% '전역변수리스트 %>
< !--#include virtual="/head.asp"--> <% '초기 설정 페이지(에러 메세지 미출력) %>
< %
Dim strSQL
Dim intSeq, strName, strEmail, strSubject, strContent, intCount, dtmReg_Date, intExist
Dim blnTag, strUserIP
Dim atag

'입력값이 숫자형인 경우 IsNumeric 함수를 사용한다.
If IsNumeric(seq) Then
intSeq = Request.QueryString("seq")
Else
Response.Write "허용하지 않는 입력값입니다."
Reponse.End
End If

'문자(열)인 경우 sqlfilter 사용
'intSeq = sqlFilter(Request.QueryString("seq")) 'SQL Injection 필터링

'읽은 횟수 검색
strSQL = "SELECT count(*) FROM board WHERE intSeq='" &amp;amp; intSeq &amp;amp; "'"

objRs.Open strSQL, objDBConn

intExist = objRs(0)
objRs.Close

If intExist <> 1 Then
Response.Write "해당글이 없습니다."
Else
'읽은 횟수 증가
strSQL = "UPDATE board SET intCount=intCount+1 WHERE intSeq='" &amp;amp; intSeq &amp;amp; "'"
objRs.Open strSQL, objDBConn

'게시물 SELECTZ
strSQL = "SELECT strName,strEmail,strSubject,strContent,intCount,strUserIP,blnTag,dtmReg_Date FROM board WHERE intSeq='" &amp;amp; intSeq &amp;amp; "'"
objRs.Open strSQL, objDBConn

strName = objRs(0)
strEmail = objRs(1)
strSubject = objRs(2)
strContent = objRs(3)
intCount = objRs(4)
strUserIP = objRs(5)
blnTag = objRs(6)
dtmReg_Date = objRs(7)

objRs.Close
Set objRs = Nothing

objDBConn.Close
Set objDBConn = Nothing

'게시물 출력값에 XSS 필터링
'사용자가 입력하는 출력되는 값은 strName, strEmail, strSubject, strContent으로 이 부분은 XSS 공격이 가능한 부분들이다.
'일반적으로 본문만 선택적으로 HTML 태그 사용을 허용하며 나머지 부분들은 사용할 수 없도록 하는것이 바람직하다.
strName = clearXSS(strName, atag)
strEmail = clearXSS(strEmail, atag)
strSubject = clearXSS(strSubject, atag)
strContent = clearXSS(strContent, atag)

'줄넘김 처리
strContent = replace(strContent, vbLf, vbLf &amp;amp; "
")
%>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ks_c_5601-1987">
<title>내용보기</title>
</head>

<body>
<div align=center>
<table border=1>
<tr>
<td>이름</td>
<td><%=strName%></td>
<td>등록일</td>
<td><%=dtmReg_Date%></td>
</tr>
<tr>
<td>이메일</td>
<td><%=strEmail%></td>
<td>조회</td>
<td><%=intCount%></td>
</tr>
<tr>
<td>제목</td>
<td colspan=3><%=strSubject%></td>
</tr>
<tr>
<td>내용</td>
<td colspan=3><%=strContent%></td>
</tr>
<tr>
<td colspan=4>
<a href="list.asp">목록으로</a> <a href="edit.asp?seq=<%=intSeq%>">수정하기</a> <a href="delete.asp?seq=<%=intSeq%>">삭제하기</a>
</td>
</tr>
</table>

</div>
</body>
</html>

< %
End If
%>



'////////////////////////////////////////////////////////////////////
'//다. SQL 구문 삽입 가능성
'////////////////////////////////////////////////////////////////////
SQL Injection은 쿼리문의 잘못 해석함에서 발생하는 문제이다. 이를 해결하기 위해서는 쿼리문을 생성시에 입력된 값에 대한 유효성 검사를 수행하면 된다. ‘, “ 문자를 \’, \”로 변경해 주거나 아예 공백으로 처리하는 방법이다.

삭제해야 할 프로시저
xp_cmdshell
xp_stratmail
xp_sendmail
xp_grantlogin
xp_makewebtask


'////////////////////////////////////////////////////////////////////
'//사. 다운로드 취약성
'////////////////////////////////////////////////////////////////////
< !--#include virtual="/include/connection.inc.asp"--> <% 'DB연결 헤더 %>
< !--#include virtual="/include/secure.inc.asp"--> <% '보안관련라이브러리 %>
< !--#include virtual="/include/config.inc.asp"--> <% '전역변수리스트 %>
< !--#include virtual="/head.asp"--> <% '초기 설정 페이지(에러 메세지 미출력) %>
< %
Dim dn_dir, fname, val_ok
Dim UploadedPath

dn_dir = Request("dir")
fname = Request("fname") '파일 이름

' IE 5.01에서는 이 방식을 사용할때 메모리 관련 문제가 발생할 수 있다.
strUA = Request.ServerVariables("HTTP_USER_AGENT")
If Instr(strUA, "MSIE") Then
intVersion = CDbl(mid(strUA, Instr(strUA, "MSIE")+5, 3))

If intVersion < 5.01 Then
Response.Write "error"
End If
End If

if fname = "" Then
Response.Write ""
End If

dn_dir = UploadedPath &amp;amp; dn_dir
val_ok = Check_Path(dn_dir, fname)

If val_ok <> "error" Then '사용자가 다운 받는 파일과 웹서버의 파일 다운로드 경로가 맞는지 비교
Set objStream = Server.CreateObject("ADODB.Stream") 'Stream 이용

Response.ContentType = "application/unknown" 'ContentType 선언
Response.AddHeader "Content-Disposition","attachment; filename=" &amp;amp; fname

objStream.Open
objStream.Type = 1
objStream.LoadFromFile val_ok

download = objStream.Read
Response.BinaryWrite download
End If

Set objstream = nothing '객체 초기화
%>
크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기

Posted by 홍반장

2008/10/23 10:09 2008/10/23 10:09
Response
No Trackback , No Comment
RSS :
http://tcbs17.cafe24.com/tc/rss/response/3702

php로 쉘스크립트 만들기

오늘날 동적 웹 페이지를 개발하는 최고의 언어가 PHP라는 것은 누구나 다 아는 사실입니다. 하지만 PHP가 쉘 스크립트 언어로도 사용할 수 있다는 것은 모르는 사람이 많은 것 같습니다. 쉘 스크립트 언어로써의 PHP는 Bash나 Perl만큼 튼튼하지는 않지만 많은 이접이 있습니다.
PHP를 쉘 언어로 사용하기 위해 요구되는 것은 PHP를 아파치 모듈 대신 CGI 바이너리로 설치해야 합니다. 보안 문제가 걸려 있기 때문에 PHP메뉴얼의 참고하기 바랍니다.
일반적인 PHP 웹 페이지와 PHP 쉘 스크립트 사이의 단 한가지 차이점은 PHP스크립트의 제일 첫 번째 줄에 다음과 같이 쉘 호출을 해주어야 하는 점입니다.

#!/usr/local/bin/php -q

-q 옵션은 HTTP헤더를 사용하지 않겠다는 뜻입니다. 또한 PHP태그를 사용해서 스크립트의 시작과 끝을 지정해 주어야 합니다.



이제 모든이가 알고 사랑하는 표준 예를 들어봅시다.

#!/usr/local/bin/php -q
print("Hello, world!\n");

?>

이 코드는 예상하다 시피 화면에 "Hello, world!"를 출력하게 됩니다.

-쉘 스크립트에 매개변수 전달하기(Passing arguments to the shell script)

일반적으로 쉘 스크립트에는 매개변수를 전달 할 수 있어야합니다. 매개변수를 전달하는 것은 다음과 $argv 배열을 통해서 이루어 집니다.

#!/usr/local/bin/php -q
$first_name = $argv[1];
$last_name = $argv[2];

print("Hello, $first_name $last_name! How are you today?\n");

?>

즉 이 예제에서는 스크립트로 전달된 두 매개변수를 출력합니다. 이 스크립트는 다음과 같이 사용될 수 있고

[dbrogdon@artemis dbrogdon]$ scriptname.ph Darrell Brogdon

다음과 같을 출력을 내게 될 것입니다.

Hello, Darrell Brogdon! How are you today?
[dbrogdon@artemis dbrogdon]$

쉘 스크립트와 웹 페이지에서 $argv 배열의 차이점은 쉘 스크립트의 $argv[0]은 실행된 스크립트 명이 들어 간다는 점입니다. 웹 페이지에서는 첫 번째 값($argv[0])은 query 문자열입니다.

-스크립트를 인터액티브하게 만들기(Making a script more interactive)

그러데 어떻게 해야 사용자의 입력을 받아들일 수 있을까요? 어떻게 인터액티브한 스크립트를 생성할 수 있을까요? PHP는 웹상에서 명령을 읽어오는 함수는 기본적으로 지원하지 않습니다. 하지만 다음과 같은 PHP 함수를 사용해서 에뮬레이트할 수 있습니다.

주 : 이 함수는 Unix상에서만 동작합니다.


function read() {
$fp=fopen("/dev/stdin", "r");
$input=fgets($fp, 255);
fclose($fp);

return $input;
}

?>

이 함수는 표준 입력(Linux에서 /dev/stdin)을 의미하는 파일 포인터를 오픈하고 \n이나 EOF를 만나거나 255문자까지 읽어 옵니다. 주로 \n가지 일어오게 될 것입니다. 그런 다음 파일 포인터를 닫고 데이터를 리턴합니다.

이제 앞서든 예제 스크립트를 수정해서 read()함수를 사용해서 사용자 입력을 기다리게 해봅시다

#!/usr/local/bin/php -q

function read() {
$fp=fopen("/dev/stdin", "r");
$input=fgets($fp, 255);
fclose($fp);

return $input;
}

print("What is your first name? ");
$first_name = read();

print("What is your last name? ");
$last_name = read();

print("\nHello, $first_name $last_name! Nice to meet you!\n");

?>

그러나 이 스크립트를 실행하게 되면 마지막 라인에서 한줄로 출력되는 대신에 세줄로 출력되게 됩니다. 이는 read()함수가 \n문자까지 취했기 때문입니다. 이는 read()함수가 데이터를 리턴할 때 \n을 제거해서 데이터를 리턴하게 하면 해결됩니다.


function read() {
$fp=fopen("/dev/stdin", "r");
$input=fgets($fp, 255);
fclose($fp);

return str_replace("\n", "", $input);
}

?>

- PHP 쉘 스크립트를 일반 쉘 스크립트에 포함시키기

때때로 PHP쉘 스크립트를 Bash나 다른 쉘 스크립트에 삽입하는 것이 편할 때도 있습니다. 이는 간단한 트릭으로 가능합니다.
PHP코드를 포함시키는 방법 :

#!/bin/bash
echo This is the Bash section of the code.

/usr/local/bin/php -q << EOF
print("This is the PHP section of the code\n");
?>
EOF

간단하지 않습니까? 변수를 추가하기 전까지는 간단합니다. 다음의 코드 부분을 실행해 볼까요?

#!/bin/bash
echo This is the Bash section of the code.

/usr/local/bin/php -q << EOF
$myVar = "PHP";
print("This is the $myVar section of the code.\n");
?>
EOF

아마 다음과 같은 에러를 발생하게 될 것입니다.

Parse error: parse error in - on line 2


이를 해결하기 위해서 PHP코드의 $앞에 \를 붙여 줍니다.

#!/bin/bash
echo This is the Bash section of the code.

/usr/local/bin/php -q << EOF

$myVar = "PHP";
print("This is the \$myVar section of the code.\n");
?>
EOF
크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기

Posted by 홍반장

2008/10/23 10:06 2008/10/23 10:06
Response
No Trackback , No Comment
RSS :
http://tcbs17.cafe24.com/tc/rss/response/3701

진실로 실패한 사람과 성공한 사람의 차이는 단지 그들의 습관에 있다.
좋은 습관은 모든 성공의 열쇠이다.
나쁜 습관은 실패로 가는 문이다.
그러므로 무엇보다 우리가 지켜야 할 제 1법칙은
좋은 습관을 만들어 좋은 습관의 노예가 되는 것이다.

- 오그 만디노, ‘이 세상에서 가장 위대한 세일즈맨의 비밀’에서



성격도 기본적으로 습관의 합(合)입니다.
습관적인 행동이 바로 성격이 됩니다.
그리고 습관이 우리의 미래를 결정합니다.
좋은 습관은 좋은 결과를 낳는 반면,
나쁜 습관은 나쁜 결과를 낳게 됩니다.
크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기

Posted by 홍반장

2008/10/23 09:42 2008/10/23 09:42
Response
No Trackback , No Comment
RSS :
http://tcbs17.cafe24.com/tc/rss/response/3700

"눈물 속에 잠이 든 때가
한두 번이 아니었다. 그러나 꿈속에서
더할 나위 없이 매력적인 존재들이 나를 위로하고
즐겁게 해주어서, 나는 늘 새로운 기분과
기쁜 마음으로 일어났다." (괴테)


- 스티븐 라버지의《루시드 드림》중에서 -


* 누구에게나 불면의 밤이 있습니다.
남모를 슬픔과 번민의 밤이 깊어도 새벽은 어김없이 찾아옵니다.
새벽 빛을 등에 지고 다가오는 '매력있는 사람'의 그림자!
꿈 같기도 하고, 환영(幻影)같기도 한 그 실루엣에
새로운 기운을 얻어, 어제보다 더 기쁘고
희망찬 하루를 다시 시작합니다.
크리에이티브 커먼즈 라이센스
Creative Commons License
이올린에 북마크하기

Posted by 홍반장

2008/10/23 09:41 2008/10/23 09:41
Response
No Trackback , No Comment
RSS :
http://tcbs17.cafe24.com/tc/rss/response/3699


블로그 이미지

- 홍반장

Archives

Recent Trackbacks

Calendar

«   2008/10   »
      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  
Statistics Graph

Site Stats

Total hits:
182271
Today:
267
Yesterday:
506