-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add CMD identifier pattern * CmdExec emulator * update DOCKER in requirements * add DOCKER service to travis * Use host-image from config * fix when image is unavailable * change default host_image * add logging and error catching * fix tests * change calling function location * Catch docker service exception * Add TODO * Support nested commands * add get & post support using different approach * fix merge conflicts * change indentation to spaces * remove unnecessary files
- Loading branch information
Showing
9 changed files
with
131 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
sudo: required | ||
services: | ||
- docker | ||
language: python | ||
python: | ||
- "3.5" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
aiohttp>=2.0 | ||
aiomysql | ||
elizabeth | ||
docker | ||
elizabeth==0.3.27 | ||
yarl | ||
redis | ||
asyncio_redis | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import asyncio | ||
import docker | ||
import yarl | ||
# TODO : Replace docker with aiodocker | ||
import logging | ||
|
||
from tanner.config import TannerConfig | ||
from tanner.utils import patterns | ||
|
||
class CmdExecEmulator: | ||
def __init__(self): | ||
try: | ||
self.docker_client = docker.from_env(version='auto') | ||
except docker.errors as docker_error: | ||
self.logger.error('Error while connecting to docker service %s', docker_error) | ||
self.host_image = TannerConfig.get('CMD_EXEC', 'host_image') | ||
self.logger = logging.getLogger('tanner.cmd_exec_emulator.CmdExecEmulator') | ||
|
||
async def setup_host_image(self): | ||
try: | ||
if not self.docker_client.images.list(self.host_image): | ||
self.docker_client.images.pull(self.host_image) | ||
except docker.errors as docker_error: | ||
self.logger.error('Error while pulling %s image %s', self.host_image, docker_error) | ||
|
||
async def get_container(self, container_name): | ||
container = None | ||
try: | ||
container_if_exists = self.docker_client.containers.list(all= True, | ||
filters= dict(name= container_name) | ||
) | ||
if container_if_exists: | ||
container = container_if_exists[0] | ||
except docker.errors.APIError as server_error: | ||
self.logger.error('Error while fetching container list %s', server_error) | ||
return container | ||
|
||
async def create_attacker_env(self, session): | ||
await self.setup_host_image() | ||
container_name = 'attacker_' + session.sess_uuid.hex | ||
container = await self.get_container(container_name) | ||
if not container: | ||
try: | ||
container = self.docker_client.containers.create(image= self.host_image, | ||
stdin_open= True, | ||
name= container_name | ||
) | ||
session.associate_env(container_name) | ||
except docker.errors as docker_error: | ||
self.logger.error('Error while creating a container %s', docker_error) | ||
return container | ||
|
||
async def get_cmd_exec_results(self, container, cmd): | ||
execute_result = None | ||
try: | ||
container.start() | ||
execute_result = container.exec_run(['sh', '-c', cmd]).decode('utf-8') | ||
container.kill() | ||
except docker.errors.APIError as server_error: | ||
self.logger.error('Error while executing command %s in container %s', cmd, server_error) | ||
result = dict(value= execute_result, page= '/index.html') | ||
return result | ||
|
||
async def delete_env(self, container_name): | ||
container = await self.get_container(container_name) | ||
try: | ||
if container: | ||
container.remove(force = True) | ||
except docker.errors.APIError as server_error: | ||
self.logger.error('Error while removing container %s', server_error) | ||
|
||
async def check_post_data(self, data): | ||
cmd_data = [] | ||
for (param_id, param_value) in data['post_data'].items(): | ||
if patterns.CMD_ATTACK.match(param_value): | ||
cmd_data.append((param_id, param_value)) | ||
return cmd_data | ||
|
||
async def check_get_data(self, path): | ||
cmd_data = [] | ||
query = yarl.URL(path).query_string | ||
params = query.split('&') | ||
for param in params: | ||
if len(param.split('=')) == 2: | ||
param_id, param_value = param.split('=') | ||
if patterns.CMD_ATTACK.match(param_value): | ||
cmd_data.append((param_id, param_value)) | ||
return cmd_data | ||
|
||
async def handle(self, value, session= None): | ||
container = await self.create_attacker_env(session) | ||
result = await self.get_cmd_exec_results(container, value) | ||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters