#!/bin/bash main() { clear #LOGIN (don't ask for username and password, user is already logged in to Management server as 'root' user) mgmt_cli login --root true > id_add_host.txt on_error_print_and_exit "Error: Failed to login, check that the server is up and running (run 'api status')" #READ HOST NAME printf "Enter host name:\n" read -e host_name on_empty_input_print_and_exit "$host_name" "Error: The host's name cannot be empty." #READ IP ADDRESS printf "\nEnter host IP address:\n" read -e ip on_empty_input_print_and_exit "$ip" "Error: The host's IP address cannot be empty." #CREATE HOST printf "Creating new host: $host_name with IP address: $ip\n" new_host_response=$(mgmt_cli add host name $host_name ip-address $ip -s id_add_host.txt 2> /dev/null) on_error_print_and_exit "Error: Failed to create host object. \n$new_host_response" #PUBLISH THE CHANGES printf "\nPublishing the changes\n" mgmt_cli publish --root true -s id_add_host.txt &> /dev/null on_error_print_and_exit "Error: Failed to publish the changes." #LOGOUT logout printf "Done.\n" } logout(){ mgmt_cli logout --root true -s id_add_host.txt &> /dev/null } on_error_print_and_exit(){ if [ $? -ne 0 ]; then handle_error "$1" fi } handle_error(){ printf "\n$1\n" #print error message mgmt_cli discard --root true -s id_add_host.txt &> /dev/null logout exit 1 } on_empty_input_print_and_exit(){ if [ -z "$1" ]; then printf "$2\n" #print error message logout exit 0 fi } # Script starts here. Call function "main". main
(https://<managemenet server>:<port>/web_api/<command>)
content-Type: application/json x-chkp-sid: <session ID token as returned by the login command>
def api_call(ip_addr, port, command, json_payload, sid): url = 'https://' + ip_addr + ':' + str(port) + '/web_api/' + command if sid == “”: request_headers = {'Content-Type' : 'application/json'} else: request_headers = {'Content-Type' : 'application/json', 'X-chkp-sid' : sid} r = requests.post(url,data=json.dumps(json_payload), headers=request_headers,verify=False) return r.json() 'xxx.xxx.xxx.xxx' -> Ip address GAIA
payload = {'user': 'your_user', 'password' : 'your_password'} response = api_call('xxx.xxx.xxx.xxx', 443, 'login',payload, '') return response["sid"] response = api_call('xxx.xxx.xxx.xxx', 443,'logout', {} ,sid) return response["message"]
new_gateway_data = {'name':'CPGleb','anti-bot':True,'anti-virus' : True,'application-control':True,'ips':True,'url-filtering':True,'interfaces': [{'name':"eth0",'topology':'external','ipv4-address': 'xxx.xxx.xxx.xxx',"ipv4-network-mask": "255.255.255.0"}, {'name':"eth1",'topology':'internal','ipv4-address': 'xxx.xxx.xxx.xxx',"ipv4-network-mask": "255.255.255.0"}]} new_gateway_result = api_call('xxx.xxx.xxx.xxx', 443,'set-simple-gateway', new_gateway_data ,sid) print(json.dumps(new_gateway_result))
new_access_data={'name':'Cleanup rule','layer':'Network','action':'Accept'} new_access_result = api_call('xxx.xxx.xxx.xxx', 443,'set-access-rule', new_access_data ,sid) print(json.dumps(new_access_result))
add_access_layer_application={ 'name' : 'application123',"applications-and-url-filtering" : True,"firewall" : False} add_access_layer_application_result = api_call('xxx.xxx.xxx.xxx', 443,'add-access-layer', add_access_layer_application ,sid) print(json.dumps(add_access_layer_application_result)) set_package_layer={"name" : "Standard","access":True,"access-layers" : {"add" : [ { "name" : "application123","position" :2}]} ,"installation-targets" : "CPGleb"} set_package_layer_result = api_call('xxx.xxx.xxx.xxx', 443,'set-package', set_package_layer ,sid) print(json.dumps(set_package_layer_result))
publish_result = api_call('xxx.xxx.xxx.xxx', 443,"publish", {},sid) print("publish result: " + json.dumps(publish_result)) new_policy = {'policy-package':'Standard','access':True,'targets':['CPGleb']} new_policy_result = api_call('xxx.xxx.xxx.xxx', 443,'install-policy', new_policy ,sid) print(json.dumps(new_policy_result) task_id=(json.dumps(new_policy_result ["task-id"])) len_str=len(task_id) task_id=task_id[1:(len_str-1)] show_task_id ={'task-id':(task_id)} show_task=api_call('xxx.xxx.xxx.xxx',443,'show-task',show_task_id,sid) print(json.dumps(show_task))
new_host_data = {'name':'JohnDoePc', 'ip-address': '192.168.0.10'} new_host_result = api_call('xxx.xxx.xxx.xxx', 443,'add-host', new_host_data ,sid) print(json.dumps(new_host_result))
set_package_layer={'name':'Standard','threat-prevention' :True,'installation-targets':'CPGleb'} set_package_layer_result = api_call('xxx.xxx.xxx.xxx', 443,'set-package',set_package_layer,sid) print(json.dumps(set_package_layer_result))
new_session_data = {'limit':'50', 'offset':'0','details-level' : 'standard'} new_session_result = api_call('xxx.xxx.xxx.xxx', 443,'show-sessions', new_session_data ,sid) print(json.dumps(new_session_result))
add_threat_profile={'name':'Apeiron', "active-protections-performance-impact" : "low","active-protections-severity" : "low or above","confidence-level-medium" : "prevent", "confidence-level-high" : "prevent", "threat-emulation" : True,"anti-virus" : True,"anti-bot" : True,"ips" : True, "ips-settings" : { "newly-updated-protections" : "staging","exclude-protection-with-performance-impact" : True,"exclude-protection-with-performance-impact-mode" : "High or lower"}, "overrides" : [ {"protection" : "3Com Network Supervisor Directory Traversal","capture-packets" : True,"action" : "Prevent","track" : "Log"}, {"protection" : "7-Zip ARJ Archive Handling Buffer Overflow", "capture-packets" : True,"action" : "Prevent","track" : "Log"} ]} add_threat_profile_result=api_call('xxx.xxx.xxx.xxx',443,'add-threat-profile',add_threat_profile,sid) print(json.dumps(add_threat_profile_result))
set_threat_protection={ "name" : "3Com Network Supervisor Directory Traversal", "overrides" : [{ "profile" : "Apeiron","action" : "Detect","track" : "Log","capture-packets" : True}, { "profile" : "Apeiron", "action" : "Detect", "track" : "Log", "capture-packets" : False} ]} set_threat_protection_result=api_call('xxx.xxx.xxx.xxx',443,'set-threat-protection',set_threat_protection,sid) print(json.dumps(set_threat_protection_result))
add_service_udp={ "name" : "Dota2_udp", "port" : '27000-27030', "keep-connections-open-after-policy-installation" : False, "session-timeout" : 0, "match-for-any" : True, "sync-connections-on-cluster" : True, "aggressive-aging" : {"enable" : True, "timeout" : 360,"use-default-timeout" : False }, "accept-replies" : False} add_service_udp_results=api_call('xxx.xxx.xxx.xxx',443,"add-service-udp",add_service_udp,sid) print(json.dumps(add_service_udp_results))
add_application_site_category={ "name" : "Valve","description" : "Valve Games"} add_application_site_category_results=api_call('xxx.xxx.xxx.xxx',443,"add-application-site-category",add_application_site_category,sid) print(json.dumps(add_application_site_category_results)) add_application_site={ "name" : "Dota2", "primary-category" : "Valve", "description" : "Dotka", "url-list" : [ "www.dota2.ru" ], "urls-defined-as-regular-expression" : False} add_application_site_results=api_call('xxx.xxx.xxx.xxx',443,"add-application-site " , add_application_site , sid) print(json.dumps(add_application_site_results)) add_application_site_group={"name" : "Games","members" : [ "Dota2"]} add_application_site_group_results=api_call('xxx.xxx.xxx.xxx',443,"add-application-site-group",add_application_site_group,sid) print(json.dumps(add_application_site_group_results))
Source: https://habr.com/ru/post/339924/
All Articles