2020-06-30 12:48:07 +02:00
|
|
|
include::../description.adoc[]
|
|
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
|
|
|
|
----
|
|
|
|
using System.Diagnostics;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace WebApplicationDotNetCore.Controllers
|
|
|
|
{
|
|
|
|
public class RSPEC2076OSCommandInjectionNoncompliantController : Controller
|
|
|
|
{
|
|
|
|
public IActionResult Index()
|
|
|
|
{
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
|
|
|
public IActionResult Run(string binary)
|
|
|
|
{
|
|
|
|
// If the value "/sbin/shutdown" is passed as binary and the web server is running as root,
|
|
|
|
// then the machine running the web server will be shut down and become unavailable for future requests
|
|
|
|
|
|
|
|
Process p = new Process();
|
|
|
|
p.StartInfo.FileName = binary; // Noncompliant
|
|
|
|
p.StartInfo.RedirectStandardOutput = true;
|
|
|
|
p.Start();
|
|
|
|
string output = p.StandardOutput.ReadToEnd();
|
|
|
|
p.Dispose();
|
2020-12-21 15:38:52 +01:00
|
|
|
|
|
|
|
return View();
|
2020-06-30 12:48:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
|
|
|
|
----
|
|
|
|
using System.Diagnostics;
|
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
namespace WebApplicationDotNetCore.Controllers
|
|
|
|
{
|
|
|
|
public class RSPEC2076OSCommandInjectionCompliantControllerController : Controller
|
|
|
|
{
|
|
|
|
public IActionResult Index()
|
|
|
|
{
|
|
|
|
return View();
|
|
|
|
}
|
|
|
|
|
|
|
|
public IActionResult Run(string binary)
|
|
|
|
{
|
2020-12-21 15:38:52 +01:00
|
|
|
if (binary.Equals("/usr/bin/ls") || binary.Equals("/usr/bin/cat"))
|
2020-06-30 12:48:07 +02:00
|
|
|
{
|
2020-12-21 15:38:52 +01:00
|
|
|
// only ls and cat commands are authorized
|
|
|
|
|
|
|
|
Process p = new Process();
|
|
|
|
p.StartInfo.FileName = binary; // Compliant
|
|
|
|
p.StartInfo.RedirectStandardOutput = true;
|
|
|
|
p.Start();
|
|
|
|
string output = p.StandardOutput.ReadToEnd();
|
|
|
|
p.Dispose();
|
2020-06-30 12:48:07 +02:00
|
|
|
}
|
|
|
|
|
2020-12-21 15:38:52 +01:00
|
|
|
return View();
|
2020-06-30 12:48:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
|
|
|
|
include::../see.adoc[]
|
2021-06-02 20:44:38 +02:00
|
|
|
|
2021-06-03 09:05:38 +02:00
|
|
|
ifdef::env-github,rspecator-view[]
|
2021-06-08 15:52:13 +02:00
|
|
|
'''
|
2021-06-02 20:44:38 +02:00
|
|
|
== Comments And Links
|
|
|
|
(visible only on this page)
|
|
|
|
|
|
|
|
include::../comments-and-links.adoc[]
|
2021-06-03 09:05:38 +02:00
|
|
|
endif::env-github,rspecator-view[]
|