Flask 上的小後門或如何控製本機網路上的計算機

嘿哈布爾!

我最近觀看了編程流的下載版本「如何在 Flask 中建立您自己的 Web 應用程式」。 我決定在一些專案中鞏固我的知識。 很長一段時間我不知道該寫什麼,然後我突然想到:“為什麼不在 Flask 中製作一個迷你後門?”

後門的第一個實現和功能選項立即出現在我的腦海中。 但我決定立即列出後門功能清單:

  1. 知道如何打開網站
  2. 具有命令列存取權限
  3. 能夠打開程式、照片、視頻

因此,使用 webbrowser 模組非常容易實現第一點。 我決定使用 os 模組來實現第二點。 第三個也是透過 os 模組,但我將使用「連結」(稍後會詳細介紹)。

編寫伺服器

所以,*鼓樂*所有伺服器程式碼:

from flask import Flask, request
import webbrowser
import os
import re

app = Flask(__name__)
@app.route('/mycomp', methods=['POST'])
def hell():
    json_string = request.json
    if json_string['command'] == 'test':
        return 'The server is running and waiting for commands...'
    if json_string['command'] == 'openweb':
        webbrowser.open(url='https://www.'+json_string['data'], new=0)
        return 'Site opening ' + json_string['data'] + '...'
    if json_string['command'] == 'shell':
        os.system(json_string['data'])
        return 'Command execution ' + json_string['data'] + '...'
    if json_string['command'] == 'link':
        links = open('links.txt', 'r')
        for i in range(int(json_string['data'])):
            link = links.readline()
        os.system(link.split('>')[0])
        return 'Launch ' + link.split('>')[1]
if __name__ == '__main__':
    app.run(host='0.0.0.0')

我已經把所有的程式碼都轉儲了,是時候解釋一下本質了。

所有程式碼都在本機電腦的連接埠 5000 上執行。 要與伺服器交互,我們必須傳送 JSON POST 請求。

JSON請求結構:

{‘command’:  ‘comecommand’, ‘data’: ‘somedata’}

嗯,「命令」是我們要執行的命令,這是有道理的。 “數據”是命令參數。

您可以編寫並發送 JSON 請求以手動與伺服器互動(請求將幫助您)。 或者您可以編寫一個控制台客戶端。

寫一個客戶端

代碼:

import requests

logo = ['nn',
        '******      ********',
        '*******     *********',
        '**    **    **     **',
        '**    **    **     **      Written on Python',
        '*******     **     **',
        '********    **     **',
        '**     **   **     **      Author: ROBOTD4',
        '**     **   **     **',
        '**     **   **     **',
        '********    *********',
        '*******     ********',
        'nn']

p = ''
iport = '192.168.1.2:5000'
host = 'http://' + iport + '/mycomp'

def test():
    dict = {'command': 'test', 'data': 0}
    r = requests.post(host, json=dict)
    if r.status_code == 200:
        print (r.content.decode('utf-8'))

def start():
    for i in logo:
        print(i)

start()
test()

while True:
    command = input('>')
    if command == '':
        continue
    a = command.split()
    if command == 'test':
        dict = {'command': 'test', 'data': 0}
        r = requests.post(host, json=dict)
        if r.status_code == 200:
            print (r.content.decode('utf-8'))
    if a[0] == 'shell':
        for i in range(1, len(a)):
            p = p + a[i] + ' '
        dict = {'command': 'shell', 'data': p}
        r = requests.post(host, json=dict)
        if r.status_code == 200:
            print (r.content.decode('utf-8'))
        p = ''
    if a[0] == 'link':
        if len(a) > 1:
            dict = {'command': 'link', 'data': int(a[1])}
            r = requests.post(host, json=dict)
            if r.status_code == 200:
                print (r.content.decode('utf-8'))
        else:
            print('Комманда не содержит аргументов!')
    if a[0] == 'openweb':
            if len(a) > 1:
                dict = {'command': 'openweb', 'data': a[1]}
                r = requests.post(host, json=dict)
                if r.status_code == 200:
                    print (r.content.decode('utf-8'))
            else:
                print('Комманда не содержит аргументов!')
    if a[0] == 'set':
        if a[1] == 'host':
            ip = a[2] + ':5000'
    if command == 'quit':
        break

說明:

首先,導入 requests 模組(用於與伺服器互動)。 下面是啟動和測試功能的描述。 然後就是神奇發生的循環。 你讀過代碼嗎? 這樣你就明白了循環中發生的魔法的意義。 輸入命令 - 它被執行。 Shell – 命令列命令(邏輯超出範圍)。

測試 – 檢查伺服器是否正在運作(後門)
連結—使用“快捷方式”
Openweb – 開啟網站
Quit-退出客戶端
設定-設定你的電腦在本地網路的IP

現在更多關於連結的資訊。

伺服器旁邊有一個link.txt檔案。 它包含文件(影片、照片、程式)的連結(完整路徑)。

結構是這樣的:

полный_путь>описание
полный_путь>описание

我們有一個後門伺服器,用於控製本地網路(在 Wi-Fi 網路內)上的電腦。 從技術上講,我們可以從任何具有 python 解釋器的裝置運行客戶端。

PS我加入了set指令,這樣如果本機網路上的電腦分配了不同的IP,可以直接在客戶端中更改。

來源: www.habr.com

添加評論