1)
자원 삽입
외부 입력값을
검증하지 않고 시스템 자원(resource)에 대한 식별자로 사용하는 경우, 공격자는 입력값 조작을 통해 시스템이 보호되는 자원에 임의로 접근하거나 수정할 수 있고, 잘못된 입력값으로 인해 시스템 자원 사이에 충돌이 발생할 수 있다.
n 안전한 코딩
기법
①
외부의 입력을 자원(파일) 식별자로 사용하는 경우, 적절한 검증을 거치도록 하거나 사전에 정의된
적합한 리스트에서 선택되도록 작성한다.
② 경로순회(Directory Travelsal)를 수행할 수 있는 문자를 제거한다.
(../, ;, \, /)
1.
// 경로순회를 수행할 수 있는 문자 제거
2. private String makeSecurePath(String path) {
3. path = path.replaceAll("../", "");
4. path = path.replaceAll(";", "");
5.
6. return path;
7. }
1.
// 화이트 리스트를 통한 시스템 자원의 보호
2. public final String[] availableIP = {
3. "127.0.0.1",
4. "192.168.0.1",
5. "192.168.0.2",
6. "192.168.0.3"
7. };
8. private void createClientSocket(String ip){
9. Socket socket;
10. int port=7777;
11. boolean isValid = false;
12.
13. try{
14. for(int i=0; i<availableIP .length; i++) {
15. if(availableIP [i].equals(ip)) {
16. isValid = true;
17. break;
18. }
19. }
20. if(isValid) socket = new Socket(ip, port);
21. else socket = new Socket(availableIP[0], port);
22. } catch (IOException e) {
23. System.out.println("소켓 생성에 실패하였습니다.");
24. }
25. }
- public void fileUpload(HttpServletRequest request,
- String ADDFILE_ROOT_PATH, int ADDFILE_MAXSIZE) throws Exception {
- if (!new File(ADDFILE_ROOT_PATH).exists())
- new File(ADDFILE_ROOT_PATH).mkdir();
- // [commons-fileupload-1-2.jar] DiskFileItemFactory, ServletFileUpload, FileItem
- DiskFileItemFactory factory = new DiskFileItemFactory();
- factory.setSizeThreshold(ADDFILE_MAXSIZE);
- factory.setRepository(new File(ADDFILE_ROOT_PATH));
- ServletFileUpload upload = new ServletFileUpload(factory);
- upload.setSizeMax(ADDFILE_MAXSIZE);
- String fullDir = "";
- String dir = "";
- try {
- List items = upload.parseRequest(request);
- Iterator iter = items.iterator();
- for (int i = 0; i < items.size(); i++) {
- FileItem item = (FileItem) items.get(i);
- if (item.isFormField()) {
- if (item.getFieldName().equals("UPLOAD_DIR"))
- dir = item.getString();
- }
- }
- if (dir.equals("")) {
- fullDir = ADDFILE_ROOT_PATH + "/temp";
- } else {
- fullDir = ADDFILE_ROOT_PATH + "/" + dir;
- }
- if (!new File(fullDir).exists())
- new File(fullDir).mkdir();
- int tmpCnt = 0;
- while (iter.hasNext()) {
- FileItem item = (FileItem) iter.next();
- if (!item.isFormField()) {
- if (item.getName().length() != 0) {
- String fieldName = item.getFieldName();
- String itemName = item.getName();
- itemName = itemName.substring(itemName.lastIndexOf("/") + 1);
- // check available file extension
- String orgFileName = itemName;
- String sysFileName = "______"; // generate random name for save
- File sysFile = new File(fullDir + "/" + sysFileName);
- item.write(sysFile);
- }
- }
- tmpCnt++;
- }
- } catch (IOException ex) {
- throw ex;
- } catch (Exception ex) {
- throw ex;
- }
- }