계: API Abuse

API는 호출자와 피호출자 간의 계약입니다. 가장 흔한 형태의 API 오용은 호출자가 이 계약에서 자신의 몫을 이행하지 못하기 때문에 발생합니다. 예를 들어, 프로그램이 chroot()를 호출한 후 chdir()을 호출하지 못하면 활성 루트 디렉터리를 안전하게 변경하는 방법을 지정하는 계약을 위반하는 것입니다. 라이브러리 오용의 또 다른 좋은 예는 피호출자가 호출자에게 신뢰할 만한 DNS 정보를 반환할 것으로 예상하는 것입니다. 이 경우, 호출자는 자신의 행동에 대해 특정한 가정을 함으로써(반환 값이 인증 목적으로 사용될 것으로 예상) 피호출자 API를 오용합니다. 다른 쪽에서 호출자-피호출자 계약을 위반할 수도 있습니다. 예를 들어, 코더가 하위 클래스 SecureRandom을 지정하고 임의 값이 아닌 값을 반환하는 경우 계약을 위반하는 것입니다.

81 개 항목 찾음
취약점
Abstract
Equals()Equals()를 구현하지 않는 개체에서 호출됩니다.
Explanation
개체 비교 시, 개발자는 대개 개체의 속성을 비교하려고 합니다. 그러나 Equals()를 명시적으로 구현하지 않는 클래스(또는 임의의 수퍼 클래스/인터페이스)에서 Equals()를 호출하면 System.Object에서 상속된 Equals() 메서드를 호출하게 됩니다. 개체 멤버 필드 또는 기타 속성을 비교하는 대신, Object.Equals()는 동일한지 알아보기 위해 두 개의 개체 인스턴스를 비교합니다. Object.Equals()를 올바르게 사용하더라도 때때로 버그가 있는 코드가 있음을 나타낼 수 있습니다.

예제 1:

public class AccountGroup
{
private int gid;

public int Gid
{
get { return gid; }
set { gid = value; }
}
}
...
public class CompareGroup
{
public bool compareGroups(AccountGroup group1, AccountGroup group2)
{
return group1.Equals(group2); //Equals() is not implemented in AccountGroup
}
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 2
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
desc.structural.dotnet.code_correctness_class_does_not_implement_equals
Abstract
equals() 메서드는 equals()를 구현하지 않는 개체에서 호출됩니다.
Explanation
개체 비교 시, 개발자는 대개 개체의 속성을 비교하려고 합니다. 그러나 equals()를 명시적으로 구현하지 않는 클래스(또는 임의의 수퍼 클래스/인터페이스)에서 equals()를 호출하면 java.lang.Object에서 상속된 equals() 메서드를 호출하게 됩니다. 개체 멤버 필드 또는 기타 속성을 비교하는 대신, Object.equals()는 동일한지 알아보기 위해 두 개의 개체 인스턴스를 비교합니다. Object.equals()를 올바르게 사용하더라도 때때로 버그가 있는 코드가 있음을 나타낼 수 있습니다.

예제 1:

public class AccountGroup
{
private int gid;

public int getGid()
{
return gid;
}

public void setGid(int newGid)
{
gid = newGid;
}
}
...
public class CompareGroup
{
public boolean compareGroups(AccountGroup group1, AccountGroup group2)
{
return group1.equals(group2); //equals() is not implemented in AccountGroup
}
}
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 2
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
desc.structural.java.code_correctness_class_does_not_implement_equals
Abstract
finalize() 메서드는 super.finalize()를 호출해야 합니다.
Explanation
Java Language Specification은 finalize() 메서드로 super.finalize()를 호출하는 것이 좋은 방법이라고 설명합니다[1].

예제 1: 다음 메서드는 super.finalize() 호출이 생략된 것입니다.


protected void finalize() {
discardNative();
}
References
[1] J. Gosling, B. Joy, G. Steele, G. Bracha The Java Language Specification, Second Edition Addison-Wesley
[2] MET12-J. Do not use finalizers CERT
[3] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[4] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[5] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[6] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[7] Standards Mapping - Common Weakness Enumeration CWE ID 568
desc.structural.java.code_correctness_erroneous_finalize_method
Abstract
serialization에서 사용되는 메서드에 대해 잘못된 메서드 서명을 사용하면 해당 메서드가 호출되지 않게 될 수 있습니다.
Explanation
코드 정확성: serializable 클래스가 serialization 또는 deserialization 함수를 작성하지만 올바른 서명을 따르지 않는 경우 잘못된 Serializable 메서드 서명 이슈가 발생합니다.


private void writeObject(java.io.ObjectOutputStream out) throws IOException;
private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException;
private void readObjectNoData() throws ObjectStreamException;


serialization에 필요한 메서드 서명이 아닌 서명을 사용하는 경우 serialization/deserialization 중에 메서드가 호출되지 않게 될 수 있습니다. 그러면 serialization/deserialization이 완료되지 않거나, 신뢰할 수 없는 코드가 개체에 접근할 수 있습니다.
발생(throw)하지 않은 예외가 있는 경우 serialization/deserialization이 실패하여 응용 프로그램이 중단되거나 그러한 개체가 부분적으로만 올바르게 생성되었을 수 있습니다. 이로 인해 디버깅하기 매우 어려운 취약점이 발생할 수 있습니다. 호출자가 이러한 예외를 캐치(catch)해야 프로그램이 중단되거나 개체가 부분적으로 생성되는 일 없이 그러한 잘못된 serialization/deserialization이 적절하게 처리될 수 있습니다.
References
[1] SER01-J. Do not deviate from the proper signatures of serialization methods CERT
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
desc.structural.java.code_correctness_incorrect_serializable_method_signature
Abstract
서블릿의 출력 스트림이 이미 커밋되고 나면, 스트림 버퍼를 재설정하거나 스트림에 다시 커밋하는 다른 작업을 수행하지 마십시오. 마찬가지로, getOutputStream 호출 후 getWriter()를 호출하거나 그 반대로 수행하는 경우도 잘못입니다.
Explanation
HttpServletRequest을 전달하거나, HttpServletResponse를 리디렉션하거나 서블릿의 출력 스트림 버퍼를 플러시하면 연결된 스트림이 커밋됩니다. 추가 플러시 또는 리디렉션 등, 이어지는 버퍼 재설정 또는 스트림 커밋은 IllegalStateException으로 이어질 수 있습니다.

또한 Java 서블릿을 사용하면 ServletOutputStream 또는 PrintWriter 중 하나를 사용하여(두 가지 모두는 안 됨) 응답 스트림에 데이터를 쓸 수 있습니다. getOutputStream()을 호출한 후 getWriter()를 호출하거나 또는 그 반대의 경우에도 IllegalStateException이 발생합니다.



런타임 시, IllegalStateException은 응답 핸들러가 실행 완료되는 것을 막아 응답을 취소합니다. 이로 인해 서버 불안정이 발생할 수 있는데, 이는 서블릿이 잘못 구현되었다는 의미입니다.

예제 1: 다음 코드는 해당 출력 스트림 버퍼가 플러시되고 나면 서블릿 응답을 리디렉션합니다.

public class RedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
...
OutputStream out = res.getOutputStream();
...
// flushes, and thereby commits, the output stream
out.flush();
out.close(); // redirecting the response causes an IllegalStateException
res.sendRedirect("http://www.acme.com");
}
}
예제 2: 반대로 다음 코드는 요청이 전달되고 나면 PrintWriter의 버퍼에 쓰거나 플러시를 시도합니다.

public class FlushServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
...
// forwards the request, implicitly committing the stream
getServletConfig().getServletContext().getRequestDispatcher("/jsp/boom.jsp").forward(req, res);
...

// IllegalStateException; cannot redirect after forwarding
res.sendRedirect("http://www.acme.com/jsp/boomboom.jsp");

PrintWriter out = res.getWriter();

// writing to an already-committed stream will not cause an exception,
// but will not apply these changes to the final output, either
out.print("Writing here does nothing");

// IllegalStateException; cannot flush a response's buffer after forwarding the request
out.flush();
out.close();
}
}
References
[1] IllegalStateException in a Servlet - when & why do we get?
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 2
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 398
[7] Standards Mapping - DISA Control Correlation Identifier Version 2 CCI-001094
[8] Standards Mapping - NIST Special Publication 800-53 Revision 4 SC-5 Denial of Service Protection (P1)
[9] Standards Mapping - NIST Special Publication 800-53 Revision 5 SC-5 Denial of Service Protection
[10] Standards Mapping - Security Technical Implementation Guide Version 4.1 APSC-DV-002400 CAT II
[11] Standards Mapping - Security Technical Implementation Guide Version 4.2 APSC-DV-002400 CAT II
[12] Standards Mapping - Security Technical Implementation Guide Version 4.3 APSC-DV-002400 CAT II
[13] Standards Mapping - Security Technical Implementation Guide Version 4.4 APSC-DV-002400 CAT II
[14] Standards Mapping - Security Technical Implementation Guide Version 4.5 APSC-DV-002400 CAT II
[15] Standards Mapping - Security Technical Implementation Guide Version 4.6 APSC-DV-002400 CAT II
[16] Standards Mapping - Security Technical Implementation Guide Version 4.7 APSC-DV-002400 CAT II
[17] Standards Mapping - Security Technical Implementation Guide Version 4.8 APSC-DV-002400 CAT II
[18] Standards Mapping - Security Technical Implementation Guide Version 4.9 APSC-DV-002400 CAT II
[19] Standards Mapping - Security Technical Implementation Guide Version 4.10 APSC-DV-002400 CAT II
[20] Standards Mapping - Security Technical Implementation Guide Version 4.11 APSC-DV-002400 CAT II
[21] Standards Mapping - Security Technical Implementation Guide Version 5.1 APSC-DV-002400 CAT II
[22] Standards Mapping - Security Technical Implementation Guide Version 5.2 APSC-DV-002400 CAT II
[23] Standards Mapping - Security Technical Implementation Guide Version 5.3 APSC-DV-002400 CAT II
desc.controlflow.java.code_correctness_multiple_stream_commits
Abstract
Content-Length 헤더가 음수로 설정됩니다.
Explanation
대부분의 경우 요청의 Content-Length 헤더를 설정한다는 것은 개발자가
서버로 전송되는 POST 데이터의 길이를 전달하고자 한다는 뜻입니다. 하지만 이 헤더는 0 또는
양의 정수여야 합니다.

예제 1: 다음 코드는 잘못된 Content-Length를 설정합니다.

URL url = new URL("http://www.example.com");
HttpURLConnection huc = (HttpURLConnection)url.openConnection();
huc.setRequestProperty("Content-Length", "-1000");
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 3
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
[6] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[7] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[8] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
desc.structural.java.api_abuse_code_correctness_negative_content_length
Abstract
Content-Length 헤더가 음수로 설정됩니다.
Explanation
대부분의 경우 요청의 Content-Length 헤더를 설정한다는 것은 개발자가
서버로 전송되는 POST 데이터의 길이를 전달하고자 한다는 뜻입니다. 하지만 이 헤더는 0 또는
양의 정수여야 합니다.

예제 1: 다음 코드는 Content-Length 헤더를 음수로 잘못 설정합니다.

xhr.setRequestHeader("Content-Length", "-1000");
References
[1] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[2] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 5
[3] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 3
[4] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[5] Standards Mapping - Common Weakness Enumeration CWE ID 398
[6] Standards Mapping - Payment Card Industry Data Security Standard Version 3.0 Requirement 6.5.6
[7] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2 Requirement 6.5.6
[8] Standards Mapping - Payment Card Industry Data Security Standard Version 3.2.1 Requirement 6.5.6
[9] Standards Mapping - Payment Card Industry Data Security Standard Version 3.1 Requirement 6.5.6
desc.structural.javascript.api_abuse_code_correctness_negative_content_length
Abstract
ToString()이 배열에서 호출됩니다.
Explanation
대부분의 경우, 배열에서 ToString()의 호출은 개발자가 배열의 내용을 문자열로 반환하는 데 관심이 있다는 의미입니다. 그러나 배열에서 ToString()을 직접 호출하면 배열의 형식을 포함하는 문자열 값이 반환됩니다.

예제 1: 다음 코드는 System.String[]을 출력합니다.

String[] stringArray = { "element 1", "element 2", "element 3", "element 4" };
System.Diagnostics.Debug.WriteLine(stringArray.ToString());
References
[1] Class Arrays Microsoft
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 398
desc.structural.dotnet.code_correctness_tostring_on_array
Abstract
toString()이 배열에서 호출됩니다.
Explanation
대부분의 경우, 배열에서 toString()의 호출은 개발자가 배열의 내용을 문자열로 반환하는 데 관심이 있다는 의미입니다. 그러나 배열에서 toString()을 직접 호출하면 배열의 형식 및 메모리의 해시코드를 포함하는 문자열 값이 반환됩니다.
예제 1: 다음 코드는 [Ljava.lang.String;@1232121을 출력합니다.

String[] strList = new String[5];
...
System.out.println(strList);
References
[1] Class Arrays Sun Microsystems
[2] Standards Mapping - CIS Azure Kubernetes Service Benchmark 1
[3] Standards Mapping - CIS Amazon Elastic Kubernetes Service Benchmark 4
[4] Standards Mapping - CIS Amazon Web Services Foundations Benchmark 1
[5] Standards Mapping - CIS Google Kubernetes Engine Benchmark normal
[6] Standards Mapping - Common Weakness Enumeration CWE ID 398
desc.structural.java.code_correctness_tostring_on_array
Abstract
필드는 위험한 것으로 주석 추가되었습니다. 모든 사용은 플래그 지정됩니다.
Explanation
FortifyDangerous 주석이 이 필드에 적용되었습니다. 이는 위험하다는 것을 표시하는 데 사용되며 모든 사용은 안전을 위해 검토해야 합니다.
desc.structural.java.dangerous_field