-
Notifications
You must be signed in to change notification settings - Fork 0
/
report.py
executable file
·150 lines (103 loc) · 4.22 KB
/
report.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
145
146
147
148
149
150
#!/bin/python
import argparse
import os
import requests
from bs4 import BeautifulSoup
#This is a script to create writeups of overthewire wargames
#https://overthewire.org/wargames/bandit
#It works on both windows and linux systems
#Author : Nikolasfil
def file_existing(file):
ls = os.listdir()
if '.' in file:
f = file.split('.')
if file in ls:
i=1
while True:
file = ".".join([f"{f[0]}_{i}",f[1]])
if file not in ls:
return file
i+=1
else:
if file in ls:
i=1
while True:
file = f"{file}_{i}"
if file not in ls:
return file
i+=1
return file
def clear():
#yes there is a more efficient way
#to discern os
try:
os.system('clc')
except:
pass
try :
os.system('clear')
except :
pass
def url(num,game):
return f"https://overthewire.org/wargames/{game}/{game}{num}.html"
def scraping(num,game):
header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0'}
request = requests.get(url(num,game),headers=header)
soup = BeautifulSoup(request.text, 'html.parser')
text = soup.find('div',id="content")
if text is not None:
return text.get_text()
def scraping_general(game):
header = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0'}
request = requests.get(f"https://overthewire.org/wargames/{game}",headers=header)
soup = BeautifulSoup(request.text, 'html.parser')
text = soup.find('div',id="content")
return text.get_text()
def writing(f,i,game):
f.write(f"\n{'-'*2*len('Level 1 → Level 1')}\n\nLevel {i} → Level {i+1}\n\n{'-'*2*len('Level 15 → Level 16')}\n")
# f.write(f"{scraping(i+1,game)}\n")
f.write(f"{scraping(i,game)}\n")
f.write(f'\n{"-"*len("steps: ")}\nsteps: \n\n\n{"-"*len("steps: ")}\n\n')
f.write(f'\n{"-"*len("password: ")}\npassword: \n{"-"*len("password: ")}\n')
def main():
#general command to know
h1='\nSSH wargames writeup with challenge\'s description'
h2='\nWith -f/--file specify the name of the report (optional, there is a default value of {game}-steps.txt\nWith -n/--number you can specify a level to scrape \n'
h3='\nWith -g/--game you can specify the game to construct the report of [leviathan,krypton,bandit,natas,narnia,behemoth,utumno,maze,vortex,manpage]\n'
#Parser Arguments
parser = argparse.ArgumentParser(description=f"{h1}\n{h2}\n{h3}")
parser.add_argument('--file', '-f', type=str, help='File Name')
parser.add_argument('--number', '-n', type=int, help='Scrapes Only one Level')
parser.add_argument('--game', '-g', type=str, help='Specifies the wargame')
args = parser.parse_args()
if args.game is not None:
levels={'leviathan': 8,'krypton': 6,'bandit': 34,'natas': 34,'narnia': 9,'behemoth': 8,'utumno': 8,'maze': 9,'vortex': 27,'manpage': 7}
if args.file is None:
# args.file = f'{args.game}-steps.txt'
args.file = file_existing(f'{args.game}-steps.txt')
#Main excecution
if args.number is None :
with open(args.file,'w') as f:
clear()
progress='Progress:[='
f.write(f"\n{'-'*10}\n{args.game.upper()}\n{'-'*10}\n")
progress+='='
print(progress)
f.write(f"{scraping_general(args.game)}\n")
for i in range(levels[args.game]):
progress+='='
writing(f,i,args.game)
clear()
print(progress)
progress+=']'
clear()
print(progress)
else:
with open(args.file,'w') as f:
f.write(f"\n{'-'*10}\n{args.game.upper()}\n{'-'*10}\n")
f.write(f"{scraping_general(args.game)}\n")
writing(f,args.number,args.game)
else:
print(h3)
if __name__=='__main__':
main()