- login form
- mstg
- HacktheBox Mobile
- Frida
- Android 6.0
- CVE-2014-0094
- NoSQL
- Directory traversal
- Android Backup
- UnCrackable
- UnCrackable level 1
- JSP
- blind sql injection
- SeeTheSharpFlag
- nginx
- HackTheBox APKey
- CVE-2010-1622
- MariaDB
- XALZ 압축해제
- File Upload
- mongoDB
- Hackthebox cat
- HackTheBox
- DOM
- JAVA ClassLoader 취약점
- PortSwigger
- CVE-2022-22965
- Xamarin 분석
- getCachedIntrospectionResults
- xss
- Today
- Total
끄적끄적
[JSP 내장 객체] 웹 서버 구축하기 (14) 본문
개요
Jasper가 미리 만들어놓은 Servelt내의 객체로 jsp에서 코드 블럭을 이용해 사용 가능
- 입/출력 도구: requset, reponse
- 저장소: pageContext(Context 정보), session, applicaiton
- 기타: config, out, page
내장 객체와 X-Forwared-For 사용하여 Client IP 출력
환경
- WAS Server: CentOS Linux release 7.9 - Tomcat 7.0 - JDK 1.8.0_312
관련 경로
ROOT Directory | /[Tomcat Directory]/tomcat/webapps/ROOT/ |
JSP to class 경로 | /[Tomcat Directory]/tomcat/work/Catalina/localhost/ |
개념
내장 객체 requset: HttpServeltRequset
- getParamehterNames(): 사용자가 전달한 키를 Enumeration 객체로 반환
- getParameter(name): 사용자가 전달한 name과 일치하는 값을 반환
- getParameterValues(name): 사용자가 전달한 name과 일치하는 값을 배열형식으로 전환
- getCookies(): 클라이언트에서 전달한 쿠키를 배열 형식으로 반환
- getMethod(): 현재 요청방식이 GET인지 POST인지를 문자열로 반환
- getSession(): 현재 세션 객체를 반환
- getRemoteAddr(): 클라이언트의 IP 주소를 반환
- getProtool(): 현재 서버의 프로토콜을 문자열로 반환
- setCharacterEncoding(): 현재 JSP로 전달되는 내용을 지정한 문자셋으로 변환
- getHeaderNames(): 현재 요청이 가지는 헤더의 이름 반환
- getHeaders(name): 현재 요청한 헤더에서 지정한 이름의 모든 값들을 반환
- getQueryString(): 요청에 포함된 쿼리 문자열을 반환
내장 객체 response: HttpServeltResponse
- setContentType(type): 컨텐트 형식을 결정
- setHeader(name, value): 클라이언트에게 헤더로 전달할 값을 설정
- setDataHeader(name date): 클라이언트에게 헤더로 전달할 날짜를 설정
- sendError(status, msg): 클라이언트에게 에러코드와 메시지를 전달
- addCookie(cookie): 클라이언트에게 전달할 쿠기를 설정
- encodeURL(url): URL로 유효하지 않은 문자를 인코딩
- setStatus(sc): 상태 코드를 설정
내장 객체 out: javax.servlet.jsp.JspWriter
- getBufferSize(): output buffer의 크기를 byte로 반환
- getRemaining(): 남아있는 버퍼크기 중 사용가능한 비율 반환
- clearBuffer(): 버퍼에 있는 콘텐츠를 모두 삭제
- flush(): 버퍼를 지우고 output Stream을 비움
- close(): output stream을 닫고 버퍼를 지움
- println(content): content 내용을 newline과 함께 출력
- print(content): content 내용 출력
내장 객체 session: javax.servlet.http.HttpSession
- getID(): 각 접속에 대한 세션의 고유 id를 문자열 형태로 반환
- getCreationTime(): 세션이 생성된 시간을 밀리세컨트값으로 반환
- getLastAccessedTime(): 현재 세션으로 마지막 작업한 시간을 밀리케선트값으로 반환
- getMaxInactiveInterval(): 세션 유지 시간을 초로 반환
- setMaxInactionInterval(t): 세션 유효 시간을 t에 설정된 값으로 설정
- invaildate(): 세션과 관련한 값을 모두 지움
- getAttribute(attr): 문자열 attr로 설정된 세션 값을 object 형태로 반환
- setAttribute(name, attr): 문자열 name에 attr 값을 설정
- removeAttribute(name): 세션에 설정한 속성 값을 삭제
내장 객체 application: javax.servlet.ServletContext
- setAttribute(name, value): application 범위의 값 설정
- getAttribute(name, value): application 범위의 값 얻기
- getRealPath(path): 실제 물리 경로 반환
- getResource(path): path 경로의 리소스를 가리키는 URL 반환
- getServerInfo(): 현재 요청 방식이 GET인지 POST인지를 문자열로 반환
- getSession(): 현재 세션 객체를 반환
- getRemoteAddr(): 클라이언트의 IP주소를 반환
- getProtocol(): 현재 서버의 프로토콜을 문자열로 반환
- setCharacterEncoding(): 현재 JSP로 전달되는 내용을 지정한 문자셋으로 변환
실습
Requset 내장 객체를 사용하여 외부에서 접근하는 IP를 출력
Builtin_Function.jsp
<%
String ip = requset.getRemoteAddr(); //request 내장 객체 사용
%>
<html>
<head></head>
<body>
IP Checker
<form action = "add" method="get">
<ul>
<li><label for="접속한 IP는? ">IP: <%=ip%></label><imput name="x"/></li>
</ul>
</form>
</body>
</html>
결과: Web Server의 IP Address가 출력
WAS앞에 Web Server가 위치하고 있기 때문에 Client의 IP address를 알지 못함. 따라서 X-Forwared-For를 사용
Nginx 설정 파일에 구문 추가 후 서버 재기동(systemctl restart nginx)
Nginx.conf
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://[WAS IP Address:Port]/;
}
WAS(Tomcat) 설정 파일에 구문 추가 후 서버 재기동(systemctl restart tomcat)
server.xml
<Valve className="org.apache.catalina.valves.RemoteIpValve"
internalProxies="[WEB Server Address]"
remoteIpHeader="x-forwarded-for"
/>
x-forwared-for 헤더에서 값을 가져오도록 수정
Builtin_Function.jsp
<%
String ip = getIp(request); //Web Server의 코드 실행
%>
<%!
public String getIp(HttpServletRequest request){
String ip = request.getHeader("x-forwarded-for");
return ip != null ? ip : request.getRemoteAddr(); }
%>
<html>
<head></head>
<body>
IP Checker
<form action = "add" method="get">
<ul>
<li><label for="접속한 IP는? ">IP: <%=ip%></label><imput name="x"/></li>
</ul>
</form>
</body>
</html>
Reference
[1] https://www.youtube.com/watch?v=IWQlKJj3HVk&list=PLq8wAnVUcTFVOtENMsujSgtv2TOsMy8zd&index=43
[2] X-Forward-For: https://cornswrold.tistory.com/442
'Development > JSP' 카테고리의 다른 글
[Expression Language] 웹 서버 구축하기 (16) (0) | 2021.12.09 |
---|---|
[MVC Model] 웹 서버 구축하기 (15) (0) | 2021.12.09 |
[JSP] 웹 서버 구축하기 (13) (0) | 2021.12.06 |
[GET/POST Service Function] 웹 서버 구축하기 (12) (0) | 2021.12.03 |
[Delete Cookie] 웹 서버 구축하기 (11) (0) | 2021.12.03 |