본문 바로가기
개발/PHP

[시큐어 코딩] PHP - 인가취약점 예방, form 변수 값 자동 넘기기,document.getElementById().submit() 사용.

by m_.9m 2022. 2. 20.

 

 

인가 취약점으로 허가되지 않은 글을 보지 못하게 하기 위해서 pw검증을 하면서 다음 페이지로 인가 검증용 변수를 발급했다. 변수를 넘겨주면서 해당 글안에 수정, 삭제 등 일련의 과정을 하도록 구현하였다.

 

자동으로 값을 넘겨주게 하기 위해서 자바의 document.getElementById().submit(); 함수를 사용하였다.

pw 검증이 되었는지 확인하기 위한 pw_chk 값,

idx 넘버 개별 구분을 위한 idx_chk 값을 넘겨주었다.

 

[read_check.php]

 

 

잠김글일 시 Pw검증 값과 검증 값이 해당 글 넘버가 맞는지 검사한다. 

 

[read.php]

 

 

잠김글이 아닐 시 별다른 권한 없이 조회

 

 

이외의 경우는 접근을 허용하지 않았다.

 

 

 

[비밀번호 검증 페이지 코드]

 

 

<?php
	 $bpw = $board['pw']; 
	if(isset($_POST['pw_chk']))
	{
	 	$pwk = $_POST['pw_chk']; 
		if(password_verify($pwk, $bpw))
		{
			?>
				
	<form id="sample_form" action="/web/board/read.php?idx=<?php echo $bno;?>&hit=<?php echo $hit; ?>" method="post"> 
	<input type="hidden" name="pw_chk" value="1"> 
	<input type="hidden" name="idx_chk" value="<?php echo $bno;?>"> 
	</form> 
	<script type="text/javascript"> 
	this.document.getElementById("sample_form").submit(); 
	</script> 
		<?php 
		}else{ ?>
		<script type="text/javascript">alert('비밀번호가 틀립니다');</script>
		<?php } } 
        
		?>

 

 

[게시글 보기 코드]

 

 

<?php 
include  $_SERVER['DOCUMENT_ROOT']."/web/board/db.php"; ?>


<!doctype html>
<head>
<meta charset="UTF-8">
   <style>
    a { text-decoration: none; color: dodgerblue; } 
    a:visited { text-decoration: none; }
    a:hover { text-decoration: none; }
    a:focus { text-decoration: none; }
    a:hover, a:active { text-decoration: none; color: royalblue;} 
  </style>

<title>게시판</title>
<link href="./style.css?after" rel="stylesheet" type="text/css" />
<link href="jquery-ui.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/jquery-3.2.1.min.js"></script>
<script type="text/javascript" src="./js/jquery-ui.js"></script>
<script type="text/javascript" src="./js/common.js"></script>
</head>
<body>
	<?php
		$bno = $_GET['idx']; 
		$hit = mq("SELECT hit from board where idx ='".$bno."'");
		$hit = $_GET['hit']+1; 
		$fet = mq("UPDATE board set hit = '".$hit."' where idx = '".$bno."'");
		$sql = mq("SELECT * from board where idx='".$bno."'"); 
		$board = $sql->fetch_array();

	if($board['lo_post']==1 && $_POST['pw_chk']==1 && $_POST['idx_chk']==$bno)
{	
	?>
<!-- 글 불러오기 -->

<div id="board_box">
	<h1><?php echo $board['title']; ?></h1>
		<div id="user_info">
			<h4>
			<?php echo $board['name']; ?>
			<?php echo $board['date']; ?> 
			조회:<?php echo $board['hit']; ?>
			<img src='/web/board/img/like1.png' alt='lock' title='lock' width='20' height='20' />:<?php echo $board['thumbup']; ?>
			<div>
파일 : <a href="/upload/<?php echo $board['file'];?>" download> <?php echo $board['file'];?> </a>


</div>
		</h4>
				<div id="bo_line"></div>
			</div>
			<hr><br><br>
			<div id="bo_content">
				<?php echo nl2br("$board[content]"); ?>
			</div>
			<br><br>
	<!-- 목록, 수정, 삭제 -->
	<div style="border: 1px;" id="bo_ser">
		<ul>
			<a href="/web/main.php">[목록으로]</a>
			<?php
			 $idx = $board['idx']; $thumbup = $board['thumbup'];
          echo "<a href='./thumbup.php?idx=$idx&thumbup=$thumbup'><img src='./img/like.png' alt='lock' title='lock' width='30' height='30' /></a>"; ?>
          	<?php
			 $idx = $board['idx']; $thumbup = $board['thumbup'];
          echo "<a href='./thumbup_cancel.php?idx=$idx&thumbup=$thumbup'>[좋아요 취소]</a>"; ?>
			<?php	
			 $idx = $board['idx']; $hit = $board['hit'];
          echo "<a href='./modify.php?idx=$idx'>[수정]</a>"; ?>
			<a href="./delete.php?idx=<?php echo $board['idx']; ?>">[삭제]</a>
		</ul>
	</div> <br>
	<!--- 댓글 불러오기 -->
<div  class="reply_view" style="align-content: center;">
	<h3>댓글목록</h3>
		<?php
			$sql3 = mq("SELECT * from reply where con_num='".$bno."' order by idx desc limit 0,5");
			while($reply = $sql3->fetch_array()){
		?>
		<div class="dap_lo" style="margin-top: 10px; padding: 10 10 10 10px;">
			<div><b style="color: red; margin-right: 15px;"> ID:  <?php echo $reply['name'];?></b> <b style="color: deeppink; margin-right: 15px;">Comment: <?php echo nl2br("$reply[content]");?></b> 
      <a href="./reply_del.php?idx=<?php echo $board["idx"];?>&rno=<? echo $reply["idx"];?> ">[삭제]</a>
		</div>
	<?php } ?>
	</div><br>
   
	<!--- 댓글 입력 폼 -->
	<div class="dap_ins"> 
		<form action="./reply.php?idx=<?php echo $bno; ?>" method="post">
			<h3 style="margin-top: 20px; margin-bottom: 10px;">댓글 입력</h3>
			<textarea style="resize: none; width: 300px; border-radius: 4px; padding: 5px 0 0 5px; height: 30px;" name="content" id="re_content" ></textarea>
				<button id="rep_bt" class="re_bt" style="margin-left: 10px; margin-bottom: 30px; vertical-align: middle;">댓글</button>
		</form>
</div>
<div id="foot_box"></div>
		</div>
</body>
</html>
<?php
}
elseif($board['lo_post']==0) { ?>

<div id="board_box">
	<h1><?php echo $board['title']; ?></h1>
		<div id="user_info">
			<h4>
			<?php echo $board['name']; ?>
			<?php echo $board['date']; ?> 
			조회:<?php echo $board['hit']; ?>
			<img src='/web/board/img/like1.png' alt='lock' title='lock' width='20' height='20' />:<?php echo $board['thumbup']; ?>
			<div>
파일 : <a href="../../../../../../../upload/".<?php echo $board['file'];?> download> <?php echo $board['file'];?> </a>


</div>
		</h4>
				<div id="bo_line"></div>
			</div>
			<hr><br><br>
			<div id="bo_content">
				<?php echo nl2br("$board[content]"); ?>
			</div>
			<br><br>
	<!-- 목록, 수정, 삭제 -->
	<div style="border: 1px;" id="bo_ser">
		<ul>
			<a href="/web/main.php">[목록으로]</a>
			<?php
			 $idx = $board['idx']; $thumbup = $board['thumbup'];
          echo "<a href='./thumbup.php?idx=$idx&thumbup=$thumbup'><img src='./img/like.png' alt='lock' title='lock' width='30' height='30' /></a>"; ?>
          	<?php
			 $idx = $board['idx']; $thumbup = $board['thumbup'];
          echo "<a href='./thumbup_cancel.php?idx=$idx&thumbup=$thumbup'>[좋아요 취소]</a>"; ?>
			<?php	
			 $idx = $board['idx']; $hit = $board['hit'];
          echo "<a href='./modify.php?idx=$idx'>[수정]</a>"; ?>
			<a href="./delete.php?idx=<?php echo $board['idx']; ?>">[삭제]</a>
		</ul>
	</div> <br>
	<!--- 댓글 불러오기 -->
<div  class="reply_view" style="align-content: center;">
	<h3>댓글목록</h3>
		<?php
			$sql3 = mq("SELECT * from reply where con_num='".$bno."' order by idx desc limit 0,5");
			while($reply = $sql3->fetch_array()){
		?>
		<div class="dap_lo" style="margin-top: 10px; padding: 10 10 10 10px;">
			<div><b style="color: red; margin-right: 15px;"> ID:  <?php echo $reply['name'];?></b> <b style="color: deeppink; margin-right: 15px;">Comment: <?php echo nl2br("$reply[content]");?></b> 
      <a href="./reply_del.php?idx=<?php echo $board["idx"];?>&rno=<? echo $reply["idx"];?> ">[삭제]</a>
		</div>
	<?php } ?>
	</div><br>
   
	<!--- 댓글 입력 폼 -->
	<div class="dap_ins"> 
		<form action="./reply.php?idx=<?php echo $bno; ?>" method="post">
			<h3 style="margin-top: 20px; margin-bottom: 10px;">댓글 입력</h3>
			<textarea style="resize: none; width: 300px; border-radius: 4px; padding: 5px 0 0 5px; height: 30px;" name="content" id="re_content" ></textarea>
				<button id="rep_bt" class="re_bt" style="margin-left: 10px; margin-bottom: 30px; vertical-align: middle;">댓글</button>
		</form>
</div>
<div id="foot_box"></div>
		</div>
</body>
</html>

<?php
}
else{
  echo "<script>alert(' 잘못된 접근입니다. '); location.href='./main.php';</script>";
}
?>