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.

- Used when webserver must dynamically interact with user,
- when user submits data to webserver by filling out a form. CGI application would process the data and return result to user via webserver
broad steps:
- dir on webserv containing cgi scripts/apps
CGI-bin - webapp user sends req to server via url
domain.com/cgi-bin/script.pl - server executes and passes output to web client
Disadvantage:
- CGI program starts new process for each http request, can take a lot of server memory
- new db connection each time
- no caching between page loads
- risk outweighs benefits, cgi has not evolved to work well w/ modern webapps. superseded by faster and more secure tech
Most well known CGI attack is exploiting shellshock aka bash bug
https://nvd.nist.gov/vuln/detail/CVE-2014-6271
- relatively simple and still be found in the wild
- sec flaw in bash (gnu bash up until 4.3) to execute unintentional cmds using envvars - at time of discovery, it was a 25 year old bug
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"
-
y='() { :;};'- interpreted as function definition for variable
y - does nothing, returns exit code 0
- when imported it will execute
echo vulnerable-shellshockif bash is vuln This or any other command such as revshell one liner will be run in the context of the webserver user - most of the time this will bewww-dataand we have access to system but need to privesc
- interpreted as function definition for variable
example of leveraging shellshock in cgi script that processes user-agent header (Why? user agent field? not sure.) 