Attack & Defense CTFs are a less common kind of CTF with more moving parts. They’re rarely done for the general public because of their complexity.
In an A&D (sometimes AWD) CTF, teams are each given the same set of vulnerable server software. Teams are to setup & audit this software before the competition. At the start of the competition, teams will connect their servers to an isolated network to join the CTF.
Within this network, teams will launch attacks against each others servers hoping to exploit the vulnerabilities they’ve found. Likewise, teams will need to properly patch their software so that it is protected against these exploits and functions normally.
Teams receive points for extracting flags, properly defending their flags, and keeping their servers operating normally.
Preperation
This passage is mainly for web A&D, so all is about protecting your web application and break others.
Backup
At the real start, we have to backup src
dir and db
data, so that we are able to recover the server if being attacked without knowing the vulns.
1 | # backup files |
And to recover:
1 | # recover mysql database |
Secure
Then we need to change password for ssh
, database and the admin page of the site, leaving no backdoors and weak passwords.
1 | # change ssh passwd |
Easy ways to detect backdoors (some tools will introduce below) :
1 | $ find . -name '*.php' | xargs grep -n 'eval(' |
Make sure service ports opend and other ports closed (netstat
, ss
, kill
) , and the key files privileges under control (lsattr
, chattr
) .
Visit Linux Security Response for detailed commands.
Attack (Control)
The web A&D is like tiny but fierce pentest.
Code Audit
It is the basic way to attack. Normally we use “off-the-shelf” scanners to check the “artificial” backdoors and identify vulnerabilities quickly.
Persistence
After we take over the target, we have to make it persistent, and here are some methods.
- Reverse shell
To manage shells: Reverse-Shell-Manager
Other shell collections like: webshell, php-webshells … all on github.
- Memory webshell
It is a program inside the PHP process, producing webshell all the time. Here gives some examples:
1 |
|
But you can be better:
1 |
|
Or you can bomb the memory:
1 |
|
To delete them:
1 |
|
Or to change them:
1 |
|
About worm php webshell, visit awd_worm_phpwebshell_framework (in Chinese)
- Apache Thread Injection
A faster way than php memory webshell. A Linux C
memory shell is even faster for sure.
Defence
Except securing that we mentioned in Preperation, we can use some “off-the-shelf” scripts to help defending.
Traffic & File Monitor
Some of my collection: filemon.py, logger.php, logger1.php, logger2.php, waf.php, waf1.php.
Write require_once('xxx.php');
to the page you want to protect, better be the one required by many other pages.
Kill Memory Webshell
Use kill -9 -1
to kill each process of current user, so as the memory webshell process.
Or refer to Persistence part to kill them using similar methods (delete them constantly).
Create directory or file with the same name of memory webshell (writing empty file constantly) to prevent generating.
Google this you can also get a bunch of results.
Sometimes we meet file names starting with /
or -
, which will mess your bash
.
For file names like /abc
, which will be regarded as path, just rm '/abc'
to delete.
For file names like -abc
, which is treated as an invalid option, add --
ahead to delete, like rm -- -abc
White List Control
For Apache
it would be .htaccess
, while for Nginx
it would be nginx.conf
, write white list config to it for better access control.
Other Tools & Scripts
Actually many github repos like AWD-Predator-Framework, Prepare-for-AWD, AoiAWD and CTFDefense are about Attack with defence
(in Chinese), concerning WAF building, and automatic flag uploading.
An interesting awd-platform is to set up your awd environment(web challenges only).