-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrapdown.py
More file actions
190 lines (155 loc) · 7.35 KB
/
Copy pathscrapdown.py
File metadata and controls
190 lines (155 loc) · 7.35 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/python3
## This program is made by Clint Canada ##
import sys, requests, urllib, argparse, os, shutil
from bs4 import BeautifulSoup
from urllib.parse import urlsplit
def print_banner():
print('███████╗ ██████╗██████╗ █████╗ ██████╗ ██████╗ ██████╗ ██╗ ██╗███╗ ██╗ ██████╗ ██╗ ██╗')
print('██╔════╝██╔════╝██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔═══██╗██║ ██║████╗ ██║ ██╔══██╗╚██╗ ██╔╝')
print('███████╗██║ ██████╔╝███████║██████╔╝██║ ██║██║ ██║██║ █╗ ██║██╔██╗ ██║ ██████╔╝ ╚████╔╝ ')
print('╚════██║██║ ██╔══██╗██╔══██║██╔═══╝ ██║ ██║██║ ██║██║███╗██║██║╚██╗██║ ██╔═══╝ ╚██╔╝ ')
print('███████║╚██████╗██║ ██║██║ ██║██║ ██████╔╝╚██████╔╝╚███╔███╔╝██║ ╚████║██╗██║ ██║ ')
print('╚══════╝ ╚═════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═════╝ ╚══╝╚══╝ ╚═╝ ╚═══╝╚═╝╚═╝ ╚═╝ ')
print()
def extract_input_fields(soup):
fields = {}
elements = soup.findAll('input')
textareaelements = soup.findAll('textarea')
selectelements = soup.findAll('select')
for input in elements:
type = input.get('type')
# let us just skip inputs with no name:
if input.get("name") == "" or input.get("name") == None:
continue
# for normal inputs
if type in ('text', 'hidden', 'password', 'submit', 'image', 'tel'):
fields[input.get('name')] = input.get('value') or ''
continue
# for radiobuttons and checkboxes
if type in ('checkbox', 'radio'):
value = ''
if input.has_attr("checked"):
if input.has_attr('value'):
value = input.get('value')
else:
value = 'on'
if 'name' in input and value:
fields[input['name']] = value
if not 'name' in input:
fields[input['name']] = value
continue
# for textareas
for textarea in textareaelements:
# let us just skip textareas with no name:
if textarea.get("name") == "" or textarea.get("name") == None:
continue
fields[textarea.get('name')] = textarea.string or ''
# for select elements
for select in selectelements:
value = ''
name = select.get('name')
options = select.findAll('option')
multiple = select.has_attr('multiple')
selected_options = []
first_value = ''
last_option_value = ''
counter = 0
for option in options:
if counter == 0:
first_value = option['value']
if option.has_attr('selected'):
selected_options.append(option.get('value'))
counter = counter+1
# let us get the first option if there is no selected one
if not selected_options and options:
selected_options = [options[0].get('value')]
elif not multiple and len(selected_options) == 0:
selected_options = [first_value]
elif not multiple and len(selected_options) > 1:
selected_options = [selected_options[0]]
fields[name] = selected_options
return fields
def get_author():
print('Program made by the author out of sheer laziness')
print('to help him downloading files behind logins without browsing the site')
def main():
print()
print_banner()
parser = argparse.ArgumentParser()
parser.add_argument("url", help="URL to look into")
parser.add_argument("url2", help="URL to download")
parser.add_argument("--author",action='store_true', help="Who am I? Why I made this?")
parser.add_argument("--mfa",action='store_true', help="mfa login")
parser.add_argument("-o","--output", help="Output path of file downloaded")
args = parser.parse_args()
url = args.url
url2 = args.url2
mfa = args.mfa
if args.author:
get_author()
quit()
try:
my_session = requests.session()
page = my_session.get(url)
except:
print ("Error in getting input")
else:
if mfa:
c = 3
else:
c = 2
soup = BeautifulSoup(page.content, 'html.parser')
for x in range(1,c):
# We will get forms
forms = soup.findAll('form')
formcount = len(forms)
if formcount == 0 and runsqlmap == False:
print("URL has no detected forms")
quit()
if formcount == 1:
action = str(forms[0].get('action'))
inputs = extract_input_fields(soup)
print("form action: "+str(forms[0].get('action')))
formname = forms[0].get('name') or ''
method = forms[0].get('method') or ''
else:
temp = input("Enter form [1 - " + str(formcount) + "]: ")
temp = int(temp)
if (temp < 1) or (temp > (formcount)):
print("Form is out of range.")
quit()
inputs = extract_input_fields(forms[int(temp)-1])
action = str(forms[int(temp)-1].get('action'))
print("form action: "+action)
formname = forms[int(temp)-1].get('name') or ''
method = forms[int(temp)-1].get('method') or ''
print("form name: "+formname+" ("+method+")")
print("Getting inputs from ", url, "\r\n")
for key,value in inputs.items():
if not value: #for empty values we place in values
inputs[key] = input("Enter input for "+key+": ")
base_url = "{0.scheme}://{0.netloc}".format(urlsplit(url))
base_url = base_url+action
response = my_session.post(base_url, data=inputs)
if x==1:
print("Going to mfa verification")
soup = BeautifulSoup(response.content, 'html.parser')
del inputs
else:
print("Posting then downloading from "+url2)
# Deleting response
del response
response = my_session.get(url2, stream=True)
page = response
# Get output filename
if args.output:
file = args.output
else:
file = 'download'
print("Saving as "+file)
local_file = open(file, 'wb')
response.raw.decode_content = True
shutil.copyfileobj(response.raw, local_file)
del response
## start of main program ##
main()