2015년 11월 30일 월요일

입력데이터 검증 및 표현 - 자원 삽입

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.  }  



  1. public void fileUpload(HttpServletRequest request,  
  2.             String ADDFILE_ROOT_PATH, int ADDFILE_MAXSIZE) throws Exception {  
  3.   
  4.         if (!new File(ADDFILE_ROOT_PATH).exists())  
  5.             new File(ADDFILE_ROOT_PATH).mkdir();  
  6.   
  7.         // [commons-fileupload-1-2.jar] DiskFileItemFactory, ServletFileUpload, FileItem  
  8.         DiskFileItemFactory factory = new DiskFileItemFactory();  
  9.         factory.setSizeThreshold(ADDFILE_MAXSIZE);  
  10.         factory.setRepository(new File(ADDFILE_ROOT_PATH));  
  11.   
  12.         ServletFileUpload upload = new ServletFileUpload(factory);  
  13.         upload.setSizeMax(ADDFILE_MAXSIZE);  
  14.   
  15.         String fullDir = "";  
  16.         String dir = "";  
  17.         try {  
  18.             List items = upload.parseRequest(request);  
  19.             Iterator iter = items.iterator();  
  20.   
  21.             for (int i = 0; i < items.size(); i++) {  
  22.                 FileItem item = (FileItem) items.get(i);  
  23.   
  24.                 if (item.isFormField()) {  
  25.                     if (item.getFieldName().equals("UPLOAD_DIR"))  
  26.                         dir = item.getString();  
  27.                 }  
  28.             }  
  29.   
  30.             if (dir.equals("")) {  
  31.                 fullDir = ADDFILE_ROOT_PATH + "/temp";  
  32.             } else {  
  33.                 fullDir = ADDFILE_ROOT_PATH + "/" + dir;  
  34.             }  
  35.   
  36.             if (!new File(fullDir).exists())  
  37.                 new File(fullDir).mkdir();  
  38.   
  39.             int tmpCnt = 0;  
  40.             while (iter.hasNext()) {  
  41.                 FileItem item = (FileItem) iter.next();  
  42.   
  43.                 if (!item.isFormField()) {  
  44.                     if (item.getName().length() != 0) {  
  45.                         String fieldName = item.getFieldName();  
  46.   
  47.                         String itemName = item.getName();  
  48.                         itemName = itemName.substring(itemName.lastIndexOf("/") + 1);  
  49.   
  50.                         // check available file extension  
  51.                           
  52.                         String orgFileName = itemName;  
  53.                         String sysFileName = "______"// generate random name for save                               
  54.   
  55.                         File sysFile = new File(fullDir + "/" + sysFileName);  
  56.                         item.write(sysFile);
  57.                     }  
  58.                 }  
  59.                 tmpCnt++;  
  60.             }  
  61.         } catch (IOException ex) {  
  62.             throw ex;  
  63.         } catch (Exception ex) {  
  64.             throw ex;  
  65.         }  
  66.     }  

댓글 없음:

댓글 쓰기