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.
CGI scripts/applications are typically used for a few reasons:
If the webserver must dynamically interact with the user
When a user submits data to the web server by filling out a form. The CGI application would process the data and return the result to the user via the webserver
A graphical depiction of how CGI works can be seen below.
Broadly, the steps are as follows:
A directory is created on the web server containing the CGI scripts/applications. This directory is typically called CGI-bin.
The web application user sends a request to the server via a URL, i.e, https://acme.com/cgi-bin/newchiscript.pl
The server runs the script and passed the resultant output back to the web client
Every CGI program starts a new process for each HTTP request which takes a lot of server memory, a new db connection is opened every time
CGI Attacks
Shellshock (Bash Bug) via CGI - https://nvd.nist.gov/vuln/detail/CVE-2014-6271
security flaw in the Bash shell (GNU Bash up until version 4.3) that can be used to execute unintentional commands using environment variables.
Shellshock via CGI
The Shellshock vulnerability allows an attacker to exploit old versions of Bash that save environment variables incorrectly.