IT 대장간
ChatGPT를 통한 웹 페이지 요청&코드 리뷰 본문
최근 기사가 쏟아져 나오는 ChatGPT 를 통한 간단한 코드들을 살펴보았다.
잽싸게 계정을 만들고 몇가지 요청을 던졌다.
결과는 꽤나 괜찮은 수준이었다.
아래와 같이 질문을 던지면 응답라인에 AI가 답을 준다.

★문제는 같은 질문을 던져도 매번 같은 응답을 주는게 아니다. 장점이 될 수도 단점이 될 수도..
흥미가 생겨 점점 질문 수준을 복합적으로하여 좀 더 높여 요청해봤다.
1. 로그인 페이지 요청
import java.sql.*;
import java.util.*;
public class LoginPage {
// JDBC driver name and database URL
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost/DATABASE_NAME";
// Database credentials
static final String USER = "username";
static final String PASS = "password";
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// Prompt the user for username and password
System.out.print("Username: ");
String username = in.nextLine();
System.out.print("Password: ");
String password = in.nextLine();
Connection conn = null;
Statement stmt = null;
try {
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
System.out.println("Connecting to a selected database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
System.out.println("Connected database successfully...");
// Execute a query
stmt = conn.createStatement();
// Test for SQL injection
String sql = "SELECT * FROM users WHERE username='" + username +
"' AND password='" + password + "'";
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while(rs.next()){
//Retrieve by column name
String usr = rs.getString("username");
String pwd = rs.getString("password");
// Display values
System.out.println("Username: " + usr);
System.out.println("Password: " + pwd);
}
rs.close();
} catch(SQLException se) {
//Handle errors for JDBC
se.printStackTrace();
} catch(Exception e) {
//Handle errors for Class.forName
e.printStackTrace();
공부용으로나 샘플용으로나 꽤나 괜찮은 코드다.
2. SQL Injection 공격 테스트 페이지
//sql injection test code
String sql = "SELECT * FROM users WHERE username = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, "username'); DROP TABLE users;--");
ResultSet result = statement.executeQuery();
//check if result set has been dropped
if(!result.next()) {
System.out.println("SQL injection vulnerability detected!");
}
else {
System.out.println("No SQL injection detected!");
}
//code for login
public void login(String username, String password) {
try {
//query the database for the user
String sql = "SELECT * FROM users WHERE username = ?";
PreparedStatement statement = conn.prepareStatement(sql);
statement.setString(1, username);
ResultSet result = statement.executeQuery();
//check if the user exists
if(result.next()) {
//get the hashed password from the db
String hashedPassword = result.getString("password");
//check if the passwords match
if(BCrypt.checkpw(password, hashedPassword)) {
System.out.println("Login successful!");
}
else {
System.out.println("Incorrect username or password.");
}
}
else {
System.out.println("Incorrect username or password.");
}
}
catch(SQLException e) {
e.printStackTrace();
}
}
위의 결과를 보고 AI 수준이 꽤나 높은것을 보았다
pstmt나 DB값과 pw값을 hash값으로 비교하는 식의 코드 수준은 기대한 그 이상이었다.
3. XSS 공격 테스트 페이지
<html>
<body>
<h3>Please Login</h3>
<form method="post" action="login.jsp">
Username: <input type="text" name="username" />
Password: <input type="password" name="password" />
<input type="submit" value="Login" />
</form>
<%
String username = request.getParameter("username");
String password = request.getParameter("password");
username = username.replaceAll("<","<").replaceAll(">",">");
password = password.replaceAll("<","<").replaceAll(">",">");
if (username != null && password != null) {
// Authenticate user
if (authenticateUser(username, password)) {
// Login successful
session.setAttribute("username", username);
response.sendRedirect("home.jsp");
}
else {
// Login failed
out.println("<p>Login failed.</p>");
}
}
%>
위 코드의 경우는 개떡같이 물어본 나의 의도를 찰떡같이 알아들은 것 같다는 느낌이었다.
Q. could you generate code for login page that vulnable XSS in java?
#후기
몇 가지 간단한 질문을 통해 확인해보긴 했으나
ChatGPT에 질문을 연쇄적으로 요청할 경우 이를 전부 반영한다는 점에서 활용도가 꽤 좋다고 보여진다
※ A 요청에 따른 코드 + B 요청 을 연이어 할 경우 A응답과 연동되는 B코드가 나온다는점.(변수, 함수, 라이브러리 등)
※ 완벽한 작문까지도 필요없는 적절한 수준(?) 영어 수준으로 대충 요청해도 찰떡같이 알아듣는 점
※ 보안측면에서도 꽤나 수준높은 코드가 나온다는점 (crypt, hash)
당장에 생각나는 것 몇 가지는
dirbuster, xsshunter, sqlmap에 대한 샘플코드를 요청해 스캐닝방식, 내부에 탑재된 쿼리 등을 깔끔한 UI로 응답받을 수 있지 않을까?
가능할진 모르겠으나 앱스캔이나 포티파이 등과 같이 자동화 솔루션에 대한 원리를 참고 할 수 있다면 꽤나 좋을 수도 있겠다라는 생각이 든다.
'지식++' 카테고리의 다른 글
| [Android] aab 파일 설치하기(abb > apks > apk) (0) | 2024.04.18 |
|---|---|
| VM Bridge 모드 연결 안될 때 해결 (1) | 2023.08.30 |
| AWS 기본 용어 (7) | 2021.11.30 |
| IIS 웹서버 _ Window 기능 활성화 및 설정 (2) | 2021.07.29 |
| 윈도우 10 주요 단축키 (2) | 2021.07.10 |