コンテンツにスキップ

Web XML Injection

Web XMLインジェクション

概要

XMLインジェクションの脆弱性は、ユーザー入力がサーバー側のXMLドキュメントまたはSOAPメッセージに不適切に組み込まれた場合に発生します。この脆弱性の悪用には、XMLメタ文字を操作してXML構造を潜在的に変更することが含まれます。影響はXMLドキュメントを利用する機能によって異なり、アプリケーションロジックの妨害から、不正なアクションや機密データへの不正アクセスにまで及びます。

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringWriter;

public class XMLInjectionExample {

    public static void main(String[] args) {
  // Simulated user input (this should come from a user or external source)
  String userInput = "<maliciousTag>Payload</maliciousTag>";

  // Vulnerable XML construction without proper input validation
  String xmlData = "<data>" + userInput + "</data>";

  // Process the XML data (vulnerable code)
  processXmlData(xmlData);
    }

    public static void processXmlData(String xmlData) {
  try {
      // Parse the XML data
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse(new org.xml.sax.InputSource(new java.io.StringReader(xmlData)));

      // Extract information from the XML (vulnerable code)
      Element root = document.getDocumentElement();
      String content = root.getTextContent();
      System.out.println("Processed XML data: " + content);
  } catch (Exception e) {
      System.err.println("Error processing XML data: " + e.getMessage());
  }
    }
}
const userInput = '<maliciousTag>Payload</maliciousTag>';

// Vulnerable XML construction without proper input validation
const xmlData = '<data>' + userInput + '</data>';

// Process the XML data (vulnerable code)
processXmlData(xmlData);

function processXmlData(xmlData) {
    try {
  // Parse the XML data
  const parser = new DOMParser();
  const xmlDoc = parser.parseFromString(xmlData, 'text/xml');

  // Extract information from the XML (vulnerable code)
  const content = xmlDoc.getElementsByTagName('data')[0].textContent;
  console.log('Processed XML data: ' + content);
    } catch (error) {
  console.error('Error processing XML data: ' + error.message);
    }
}
<?php
$userInput = '<maliciousTag>Payload</maliciousTag>';

// Vulnerable XML construction without proper input validation
$xmlData = '<data>' . $userInput . '</data>';

// Process the XML data (vulnerable code)
processXmlData($xmlData);

function processXmlData($xmlData) {
    try {
  // Create a new DOMDocument
  $doc = new DOMDocument();

  // Load the XML data
  $doc->loadXML($xmlData);

  // Extract information from the XML (vulnerable code)
  $content = $doc->getElementsByTagName('data')->item(0)->textContent;
  echo 'Processed XML data: ' . $content . PHP_EOL;
    } catch (Exception $e) {
  echo 'Error processing XML data: ' . $e->getMessage() . PHP_EOL;
    }
}
?>

推奨事項

  • 直接の連結を避ける: XMLドキュメント内でユーザー入力を直接連結することは避けてください。
  • ユーザー入力の無害化: XMLドキュメントに挿入する前に、ユーザー入力を無害化してください。
  • 堅牢なXMLパーサー: XML仕様に準拠した、確立された安全なXMLパーサーを使用してください。脆弱性を持つ可能性のあるカスタムパーサーや古いパーサーには注意してください。
  • 危険なXML機能の無効化: 必要ない場合は、外部エンティティの拡張を無効にして攻撃対象領域を減らし、XXEの脆弱性のリスクを軽減してください。
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.StringReader;

public class MitigatedXMLInjectionExample {

    public static void main(String[] args) {
  // Simulated user input (this should come from a user or external source)
  String userInput = "<maliciousTag>Payload</maliciousTag>";

  // Mitigated XML construction with proper input validation
  String sanitizedInput = sanitizeUserInput(userInput);
  String xmlData = "<data>" + sanitizedInput + "</data>";

  // Process the XML data (mitigated code)
  processXmlData(xmlData);
    }

    public static String sanitizeUserInput(String input) {
  // Perform proper input validation and sanitization
  // For example, you can use a library or a regex to remove invalid characters
  return input.replaceAll("[&<>\"]", "");
    }

    public static void processXmlData(String xmlData) {
  try {
      // Parse the XML data
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      Document document = builder.parse(new org.xml.sax.InputSource(new StringReader(xmlData)));

      // Extract information from the XML (mitigated code)
      Element root = document.getDocumentElement();
      String content = root.getTextContent();
      System.out.println("Processed XML data: " + content);
  } catch (Exception e) {
      System.err.println("Error processing XML data: " + e.getMessage());
  }
    }
}
const userInput = '<maliciousTag>Payload</maliciousTag>';

// Mitigated XML construction with proper input validation
const sanitizedInput = sanitizeUserInput(userInput);
const xmlData = '<data>' + sanitizedInput + '</data>';

// Process the XML data (mitigated code)
processXmlData(xmlData);

function sanitizeUserInput(input) {
    // Perform proper input validation and sanitization
    // For example, you can use a library like DOMPurify for HTML/XML sanitization
    // Here, we are using a simple approach to remove invalid characters
    return input.replace(/[&<>"']/g, '');
}

function processXmlData(xmlData) {
    try {
  // Parse the XML data
  const parser = new DOMParser();
  const xmlDoc = parser.parseFromString(xmlData, 'text/xml');

  // Extract information from the XML (mitigated code)
  const content = xmlDoc.getElementsByTagName('data')[0].textContent;
  console.log('Processed XML data: ' + content);
    } catch (error) {
  console.error('Error processing XML data: ' + error.message);
    }
}
<?php
$userInput = '<maliciousTag>Payload</maliciousTag>';

// Mitigated XML construction with proper input validation
$sanitizedInput = sanitizeUserInput($userInput);
$xmlData = '<data>' . $sanitizedInput . '</data>';

// Process the XML data (mitigated code)
processXmlData($xmlData);

function sanitizeUserInput($input) {
    // Perform proper input validation and sanitization
    // For example, you can use functions like htmlspecialchars to sanitize XML content
    return htmlspecialchars($input, ENT_XML1, 'UTF-8');
}

function processXmlData($xmlData) {
    try {
  // Create a new DOMDocument
  $doc = new DOMDocument();

  // Load the XML data
  $doc->loadXML($xmlData);

  // Extract information from the XML (mitigated code)
  $content = $doc->getElementsByTagName('data')->item(0)->textContent;
  echo 'Processed XML data: ' . $content . PHP_EOL;
    } catch (Exception $e) {
  echo 'Error processing XML data: ' . $e->getMessage() . PHP_EOL;
    }
}
?>

リンク

基準

  • GDPR:
    • ART_25
    • ART_32
  • PCI_STANDARDS:
    • REQ_6_4
    • REQ_6_5
  • SOC2_CONTROLS:
    • CC_2_1
    • CC_4_1
    • CC_7_1
    • CC_7_2
    • CC_7_4
    • CC_7_5
  • HIPAA_CONTROLS:
    • SECURITY212
    • SECURITY213
    • SECURITY255