NGupta728957 (Community Member) asked a question.

Data validation not helping to resolve CWE ID 78 flaw

We are facing issue in resolving Command or Argument Injection Flaw [CWE ID 78].

1In above code in line no 848 veracode is throwing issue for untrusted input because linktext we are passing in the function contains dynamic values.

I tried to validate the dynamic input using uri libraries and Regex pattern. But that doesn't seems to be helping.

Validation using URI library :

2Validation using Regex patter :

3Need guidance on how to resolve these flaws.


  • Hi @NGupta728957 (Community Member)​,

     

    It seems to me that you are trying to open a web browser and navigate to the provided URL. the only code change Veracode Static Analysis would automatically detect here is validating the untrusted parts of the URL against an allow-list. The following Knowledge Article describes how to implement an allow-list in a way Veracode Static Analysis can detect: https://community.veracode.com/s/article/Using-an-Allow-list-in-a-Way-Static-Analysis-can-Detect.

     

    In any other case, I would recommend the following:

     

    • Do not validate the URL as a whole but only the potentially untrusted parts of it. In your case, I would recommend making sure that `gal[0].AttributeValue` is alphanumeric.
    • Make sure that the program is running with the lowest required privileges. This is always recommended but even more important for programs executing raw OS commands.
    • Your program seems to be using the default program on this machine associated with URLs. Be aware that, while you expect this to be a web browser, an attacker might be able to change this to literally anything and potentially execute arbitrary (potentially malicious) binaries with the user the application is running as. This can be an attack vector for privilege escalation.

     

    For the last point, it might be a good idea to explicitly specify that `explorer.exe` may be used (this should work for your use case). The resulting code would be something like this: `Process.Start("explorer.exe", url);`.

     

    However, even then there is a potential risk of a malicious user attempting to put a malicious binary (which they would also name "explorer.exe") in a directory higher up the PATH. This would then potentially be executed instead of the intended `explorer.exe`. The safest way to deal with this is probably something like this:

     

      String windir = Environment.GetEnvironmentVariable("windir");

      String explorer = $"{windir}\\explorer.exe";

      System.Diagnostics.Process.Start(explorer, url);

     

    This uses the `windir` variable (has the value `%SystemRoot%` which usually points to `C:\Windows`, where `explorer.exe` is located). The remaining risk here would be that an attacker with admin privileges might be able to change the environment variable `windir` to another path with their own malicious `explorer.exe` inside. But that risk is probably very acceptable.

     

    Please note that you would have to propose a mitigation by design for this and discuss it with your organization's Security Team. For more information on this, please refer to https://docs.veracode.com/r/improve_mitigation.

     

    Thank you,

    Florian Walter

    Expand Post

Topics (6)

No articles found
Loading

Ask the Community

Get answers, share a use case, discuss your favorite features, or get input from the community.