[PYTHON] Get FortiGate Application Control ID

Introduction

The FortiGate application control ID is published on fortiguard.com, but it is not displayed in the list. .. .. Not a few people want a list in csv, such as when loading information into a flow collector.

Get from the body

You can get the list from the CLI using the command below.

config application list
edit default
config entries
edit 1

# set application ?

If you enter?, It will be output, but since the supported application is different depending on the device, I would like to get all the data from fortiguard.com if possible.

I made my own tool.

I made my own tool with python.

pip install beautifulsoup4 lxml requests
import requests
import csv
import re
import time
from bs4 import BeautifulSoup

s = requests.Session()

url = 'https://fortiguard.com/appcontrol'
get_url_info = s.get(url)
bs4Obj = BeautifulSoup(get_url_info.text, 'lxml')
result = bs4Obj.find('div', class_='sidebar-content')
appids = int(re.findall('[0-9]+', result.find('a').get_text().replace(',', ''))[0])
perPage = int(len(bs4Obj.find_all('div', class_='title')))
if appids % perPage == 0:
    pages = int(appids // perPage)
else:
    pages = int(appids // perPage) + 1

i = 1
datas = []
url_base = 'https://fortiguard.com/appcontrol?deepapp=&page='

while pages + 1 > i:
    print('page:', i, '/', pages)
    url = url_base + str(i)
    try:
        get_url_info = s.get(url)
        bs4Obj = BeautifulSoup(get_url_info.text, 'lxml')
        app_list = bs4Obj.find_all('div', class_='title')
        if len(app_list) == 0:
            continue
    except Exception as e:
        print(e)
        time.sleep(1)
        continue
    j = 0
    while True:
        try:
            app_name = str(app_list[j].find('a').get_text())
            app_num = int(re.findall('[0-9]+', app_list[j].find('a').get('href'))[0])
            data = [app_num, app_name]
            datas.append(data)
            j += 1
        except:
            break
    i += 1

print('result:', len(datas), '/', appids)

with open('appid.csv', 'w') as file:
    writer = csv.writer(file, lineterminator='\n')
    writer.writerows(sorted(datas))

I have uploaded the same code on Github. https://github.com/Connie-Wild/FortiOS_AppControlID Well then!

Recommended Posts

Get FortiGate Application Control ID
Get thread ID with python
[Remote development] Control application (Practice 2)
Get AWS account ID with boto3