Information Technology Grimoire

Version .0.0.1

IT Notes from various projects because I forget, and hopefully they help you too.

API Scripting

Guide

https://sc1.checkpoint.com/documents/R80.20_GA/WebAdminGuides/EN/CP_R80.20_CLI_ReferenceGuide/html_frameset.htm?topic=documents/R80.20_GA/WebAdminGuides/EN/CP_R80.20_CLI_ReferenceGuide/205489_1

General

https://community.checkpoint.com/t5/API-CLI-Discussion/New-to-Checkpoint-Scripting-Where-to-start-from/m-p/21087 https://yurisk.info/2021/05/09/checkpoint-api-tutorial-part1-getting-started/

Examples

1 https://community.checkpoint.com/t5/API-CLI-Discussion/create-host-step1-sh/m-p/38722

2 https://community.checkpoint.com/t5/API-CLI-Discussion/create-host-step2-sh/m-p/38720?attachment-id=134

3 https://community.checkpoint.com/t5/API-CLI-Discussion/create-host-step3-sh/m-p/38718?attachment-id=963

Block lists

https://community.checkpoint.com/t5/API-CLI-Discussion/Dynamic-Block-Lists-for-Check-Point-firewalls/m-p/38871#M2359

import requests,json, sys, getpass
requests.packages.urlib3.disable_warnings()

proxies = {
	'http': 'http://some:80',
	'https': 'http://some:80'
}

def api_call(ip,port,command,json_payload, sid):
	url = 'https://' + ip + ':' + port + '/web_api/' + command
	if sid == '':
		request_headers = {'Content-Type':'application/json'}
	else:
		request_headers = {'Content-Type':'application/json', 'Xchkp-sid': sid}
	r = requests.post(url,data=json.dumps(json_payload), headers=request_headers,proxies=proxies, verify=False)
	return r.json()
	
def login(user,password, mgmt_ip, port);
	payload = {'user':user, 'password':password}
	r = api_call(mgmt_ip, port, 'login', payload, '')
	return r['sid']

def get_data():
	d = {
		'192.168.1.7' : {
			'name': 'some_host_7',
			'ip-address': '192.168.1.7'
		},		
		'192.168.1.8' : {
			'name': 'some_host_8',
			'ip-address': '192.168.1.8'
		}
	}
	return d

if __name__ == "__main__":
	mgmt_ip = '1.2.3.4'
	port = 443

	user = 'admin'
	password = getpass()
	sid = login(user,password,mgmt_ip,port)
	print("session id: " + sid)

	d = get_data()

	for ip in d:
		r = api_call(mgmt_ip, port, 'add-host', d[ip], sid)
		print(json.dumps(r))
	
	print(json.dumps(api_call(mgmt_ip, port, 'publish', (), sid))
	print(json.dumps(api_call(mgmt_ip, port, 'logout', (), sid))
import requests, json, sys
requests.packages.urllib3.disable_warnings()

proxies = {
	'http':'http://proxy:80',
	'https':'http://proxy:8443'
}

def ap_call(ip_addr,port command,json_payload,sid):
	url = 'https://' + ip_addr + ':' + 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, proxies=proxies, verify=False)
	return r.json()
	
def login(user,password):
	payload = {'user':user,'password':password}
	response = api_call('1.2.3.4','443','login',payload,"")
	return response['sid']

sys.exit()

net_host_data ={'name':'new host name', 'ip-address':'192.168.1.1'}
new_host_result = api_call('192.2.2.2',443,'add-host',new_host_data, sid)
print(json.dumps(new_host_result))

publish_result = api_call('192.2.2.2',443,"publish",{},sid)
print(json.dumps(publish_result))

logout_result = api_call('192.2.2.2',443,"logout",{},sid)
print(json.dumps(json.dumps(logout_result))