python简单脚本调用fortigate API / FortiGate 60C API - Python helper

前言

有时候我们需要在SOAR/SOC中集成对防火墙的控制。
本文将介绍如何用python来调用Fortigate的API。

环境

  • IBM Resilient Platform ( 好吧…这个不重要 )
  • RedHat Linux Server
  • Python 2.7.x
  • Fortigate 60C
  • Postman(API测试工具)

手上的测试机器只有60C,虽然版本有点老但也基本支持Fortigate统一的Rest API。
本来想偷懒用Ansible+yaml来实现,可是调试了半天还是不成功( 如果有哪位大神做过,欢迎指导
Resilient的integration function都是用Python写的,而在github找的pyfortiapi.py 运行也会报错,只好参考着它以及Fortigate的API文档来尝试自己写个简单的POC程序。

参考链接

  1. Fortigate Postman Youtube 教程
  2. Github - PyFortiAPI
  3. Github - fortiosapi

正文

废话不多说直接上代码…

基础部分

  1. 导入基础模块
import requests
import json
import logging
# Disable requests' warnings for insecure connections
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
#因为没有SSL verify 会报 warning,很难看,就把它disable了

log = logging.getLogger(__name__)
  1. 初始类

class Forti60C:

    def __init__(self, domain, timeout=10, vdom="root"):
        self.domain = domain  #Fortigate的IP
        self.timeout = timeout
        self.vdom = vdom

  1. AUTH
    (返回一个session)
    def login(self, url='/logincheck', payload=None):
        s = requests.session()
        url = 'https://' + self.domain + url
        
        #username and password in fortigate
        payload = "username=[?]&secretkey=[password]"

        s.post(url, data=payload, verify=False, timeout=self.timeout)
        # Get CSRF token from cookies, add to headers
        for cookie in s.cookies:
            if cookie.name == 'ccsrftoken':
                csrftoken = cookie.value[1:-1]  # strip quotes
                s.headers
  • 24
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值