13. Gateway interface - Shellshock

A Common Gateway Interface (CGI) is used to help a web server render dynamic pages and create a customized response for the user making a request via a web application. CGI applications are primarily used to access other applications running on a web server. CGI is essentially middleware between web servers, external databases, and information sources. CGI scripts and programs are kept in the /CGI-bin directory on a web server and can be written in C, C++, Java, PERL, etc. CGI scripts run in the security context of the web server. They are often used for guest books, forms (such as email, feedback, registration), mailing lists, blogs, etc. These scripts are language-independent and can be written very simply to perform advanced tasks much easier than writing them using server-side programming languages.

![](/img/Pasted image 20250118001545.png)

broad steps:

Disadvantage:

Most well known CGI attack is exploiting shellshock aka bash bug https://nvd.nist.gov/vuln/detail/CVE-2014-6271

Shellshock via CGI Old versions of bash. When saving function as variable, shell function will stop where it is defined to end by creator. Vuln versions of bash will allow an attacker to execute OS cmds included after a function stored inside an environment variable. Simple example where we define an env var and include malicious command:

env y='() { :;}; echo vulnerable-shellshock' bash -c "echo not vulnerable"

example of leveraging shellshock in cgi script that processes user-agent header (Why? user agent field? not sure.) ![](/img/Pasted image 20250118132758.png)