| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# |
| 4 |
# Eric Chou ([email protected]) |
| 5 |
# |
| 6 |
|
| 7 |
import sys |
| 8 |
from sys import stdin |
| 9 |
import optparse |
| 10 |
import logging, json |
| 11 |
from a10 import axapi_auth, axapi_action |
| 12 |
from json_configs.logoff import logoff_path |
| 13 |
from json_configs.write_memory import write_mem_path |
| 14 |
from json_configs.ddos_dst_zone import ddos_dst_zone_path, ddos_dst_zone |
| 15 |
|
| 16 |
LOG_FILE = "/var/log/fastnetmon-notify.log" |
| 17 |
|
| 18 |
|
| 19 |
logger = logging.getLogger("DaemonLog") |
| 20 |
logger.setLevel(logging.INFO) |
| 21 |
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") |
| 22 |
handler = logging.FileHandler(LOG_FILE) |
| 23 |
handler.setFormatter(formatter) |
| 24 |
logger.addHandler(handler) |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
client_ip_as_string=sys.argv[1] |
| 29 |
data_direction=sys.argv[2] |
| 30 |
pps_as_string=int(sys.argv[3]) |
| 31 |
action=sys.argv[4] |
| 32 |
|
| 33 |
logger.info(" - " . join(sys.argv)) |
| 34 |
|
| 35 |
|
| 36 |
# A10 Mitigator Information |
| 37 |
mitigator_ip = "192.168.199.150" |
| 38 |
zone_name = client_ip_as_string + "_zone" |
| 39 |
ip_addr = client_ip_as_string |
| 40 |
mitigator_base_url, signature = axapi_auth(mitigator_ip, "admin", "a10") |
| 41 |
|
| 42 |
|
| 43 |
if action == "unban": |
| 44 |
try: |
| 45 |
r = axapi_action(mitigator_base_url+ddos_dst_zone_path+zone_name, method="DELETE", signature=signature) |
| 46 |
except Exception as e: |
| 47 |
logger.info("Zone not removed in unban, may not exist. Result: " + str(e)) |
| 48 |
|
| 49 |
# Commit config |
| 50 |
axapi_action(mitigator_base_url+write_mem_path, signature=signature) |
| 51 |
# Logoff |
| 52 |
axapi_action(mitigator_base_url+logoff_path, signature=signature) |
| 53 |
|
| 54 |
sys.exit(0) |
| 55 |
|
| 56 |
elif action == "ban": |
| 57 |
|
| 58 |
r = axapi_action(mitigator_base_url+ddos_dst_zone_path, method='GET', signature=signature) |
| 59 |
try: |
| 60 |
if zone_name in [i['zone-name'] for i in json.loads(r)['zone-list']]: |
| 61 |
r = axapi_action(mitigator_base_url+ddos_dst_zone_path+zone_name, method="DELETE", signature=signature) |
| 62 |
logger.info(str(r)) |
| 63 |
except Exception as e: |
| 64 |
logger.info("No Zone detected or something went wrong. Erorr: " + str(e)) |
| 65 |
|
| 66 |
# A10 Mitigation On Ramp |
| 67 |
zone_name = client_ip_as_string + "_zone" |
| 68 |
ip_addr = client_ip_as_string |
| 69 |
returned_body = ddos_dst_zone(zone_name, ip_addr) |
| 70 |
try: |
| 71 |
r = axapi_action(mitigator_base_url+ddos_dst_zone_path, signature=signature, payload=returned_body) |
| 72 |
except Exception as e: |
| 73 |
logger.info("zone not created: " + str(e)) |
| 74 |
|
| 75 |
# Commit changes |
| 76 |
axapi_action(mitigator_base_url+write_mem_path, signature=signature) |
| 77 |
# Log off |
| 78 |
axapi_action(mitigator_base_url+logoff_path, signature=signature) |
| 79 |
|
| 80 |
sys.exit(0) |
| 81 |
|
| 82 |
else: |
| 83 |
sys.exit(0) |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|