-
Notifications
You must be signed in to change notification settings - Fork 0
/
python-vulnhub-author-scraper.html
197 lines (156 loc) · 17 KB
/
python-vulnhub-author-scraper.html
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
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="utf-8">
<title> Python ile basit vulnhub yazarlarını çeken bot
</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="HandheldFriendly" content="True" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="author" content="0x656e" />
<meta name="description" content="Daha önceden yazmış olduğum bu scripti bugün güncellemek zorunda kaldım.Nedeni vulnhub önceden her türlü bağlantıyı kabul etsede bugün etmiyordu.Bende scripte Chrome Dev Tools'dan kopyaladığım header bilgilerini çaktım script şimdi bağlantı kuruyordu fakat regex patterni boş olarak dönüyordu.İlk scriptte urllib.request.urlopen(url).read() şeklinde bir yapı vardı …" />
<meta name="keywords" content="python, requests, parser, lxml, vulnhub">
<meta property="article:tag" content="python" />
<meta property="article:tag" content="requests" />
<meta property="article:tag" content="parser" />
<meta property="article:tag" content="lxml" />
<meta property="article:tag" content="vulnhub" />
<link rel="canonical" href="https://enesergun.net/python-vulnhub-author-scraper.html"/>
<meta name="description" content=""/>
<!-- og definitions -->
<meta property="og:type" content="article" />
<meta property="og:url" content="https://enesergun.net/python-vulnhub-author-scraper.html" />
<meta property="og:title" content="Python ile basit vulnhub yazarlarını çeken bot" />
<meta property="og:description" content="Daha önceden yazmış olduğum bu scripti bugün güncellemek zorunda kaldım.Nedeni vulnhub önceden her türlü bağlantıyı kabul etsede bugün etmiyordu.Bende scripte Chrome Dev Tools'dan kopyaladığım..." />
<meta property="og:image" content="https://w0.peakpx.com/wallpaper/212/918/HD-wallpaper-virtual-world-sunset-80s-landscape-retro-theme-tron.jpg" />
<link rel="stylesheet" href="theme/style.css">
<link href="https://enesergun.net/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Enes Ergün Atom Feed" />
</head>
<body>
<div class="container">
<header class="blog-header">
<h1><a href="https://enesergun.net" class="glitch glow" data-text="Enes Ergün">Enes Ergün</a></h1>
<p> Chaotic Neutral / ISTP-T - 30 lvl rogue - Cyber Security </p>
<nav>
<a href="https://enesergun.net/">INDEX</a>
<a href="https://enesergun.net/archives">ARCHIVES</a>
<a href="https://enesergun.net/categories">CATEGORIES</a>
</nav>
</header>
<div class="post">
<header>
<h1>Python ile basit vulnhub yazarlarını çeken bot</h1>
<p class="date">Written on <time datetime="2016-06-17T10:20:00+03:00">Jun 17, 2016</time></p>
</header>
<article>
<p>Daha önceden yazmış olduğum bu scripti bugün güncellemek zorunda kaldım.Nedeni vulnhub önceden her türlü bağlantıyı kabul etsede bugün etmiyordu.Bende scripte Chrome Dev Tools'dan kopyaladığım header bilgilerini çaktım script şimdi bağlantı kuruyordu fakat regex patterni boş olarak dönüyordu.İlk scriptte</p>
<div class="highlight"><pre><span></span><code>urllib.request.urlopen(url).read()
</code></pre></div>
<p>şeklinde bir yapı vardı ve bu yapı response'u byte code olarak döndürüyordu.Byte code döndüren response içinde regex ile arama yapmak için basit bir harf argüman var hemen onu da göstereyim.</p>
<div class="highlight"><pre><span></span><code>rp = re.findall(b'pattern',text)
</code></pre></div>
<p>şeklinde bir argüman ile bytecode olarak arama yapıyor fakat bu debug yaparken sorun nerde ? veya pattern neden bulmuyor şeklinde ki sorunlarınızı çözerken zorluk çıkartıyordu.Bende kodu bu yüzden güncelledim.</p>
<p>urllib yerine artık requests kullanıyorum.(Respect david beazley)</p>
<p>Neyse lafı fazla uzatmayalım. Sonuç görüntüsü aşağıdadır.</p>
<p><img alt="Vulnhub" src="images/vulnhub.png" title="Vulnhub"></p>
<h3>Kodlar</h3>
<div class="highlight"><pre><span></span><code><span class="ch">#!/usr/bin/python3</span>
<span class="kn">import</span> <span class="nn">requests</span><span class="o">,</span><span class="nn">re</span>
<span class="n">headers</span> <span class="o">=</span> <span class="p">{</span>
<span class="s1">':authority'</span><span class="p">:</span><span class="s1">'www.vulnhub.com'</span><span class="p">,</span>
<span class="s1">':method'</span><span class="p">:</span><span class="s1">'GET'</span><span class="p">,</span>
<span class="c1">#':path':'/?page=1'</span>
<span class="s1">':scheme'</span><span class="p">:</span><span class="s1">'https'</span><span class="p">,</span>
<span class="s1">'accept'</span><span class="p">:</span><span class="s1">'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'</span><span class="p">,</span>
<span class="s1">'accept-encoding'</span><span class="p">:</span><span class="s1">'gzip, deflate, sdch, br'</span><span class="p">,</span>
<span class="s1">'accept-language'</span><span class="p">:</span><span class="s1">'tr,en;q=0.8'</span><span class="p">,</span>
<span class="s1">'cache-control'</span><span class="p">:</span><span class="s1">'max-age=0'</span>
<span class="p">,</span><span class="s1">'cookie'</span><span class="p">:</span><span class="s1">'__cfduid=db8361564d5291910653bb1219526d1051468106057; cf_clearance=bba81bf5ad85eef7974fd7ffe76cc5c022a83ca1-1468752269-2592000; _ga=GA1.2.1086152234.1468106055'</span>
<span class="p">,</span><span class="s1">'upgrade-insecure-requests'</span><span class="p">:</span><span class="s1">'1'</span><span class="p">,</span>
<span class="s1">'user-agent'</span> <span class="p">:</span><span class="s1">'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36'</span> <span class="p">}</span>
<span class="n">liste</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">linkler</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">def</span> <span class="nf">getir</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="n">url</span> <span class="o">=</span> <span class="s1">'https://www.vulnhub.com/?page=</span><span class="si">%s</span><span class="s1">'</span> <span class="o">%</span><span class="n">x</span>
<span class="n">linkler</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">url</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">yazartopla</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"Acılan link : "</span><span class="o">+</span><span class="n">x</span><span class="p">)</span> <span class="c1">#debug için eklemiştim</span>
<span class="n">ac</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span><span class="o">.</span><span class="n">text</span> <span class="c1">## Siteyi get metodu ile çekiyoruz ve içindeki textleri alıyoruz</span>
<span class="n">ac</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'\/author\/(.*)\/'</span><span class="p">,</span><span class="n">ac</span><span class="p">)</span> <span class="c1"># Text içinden /author/Arasındaki herşeyi alıyoruz/</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">ac</span><span class="p">:</span> <span class="c1"># Tüm sayfadan arayıp liste şeklinde bir sonuç döndüğü için for döngüsüne sokup tek tek listeye ekliyoruz.</span>
<span class="k">if</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">liste</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">liste</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">git</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="k">try</span><span class="p">:</span> <span class="c1">### Hata yakalama blokları</span>
<span class="n">url</span> <span class="o">=</span> <span class="s2">"https://www.vulnhub.com/author/"</span><span class="o">+</span><span class="n">i</span><span class="o">+</span><span class="s2">"/"</span>
<span class="n">ac</span> <span class="o">=</span> <span class="n">requests</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">url</span><span class="p">,</span><span class="n">headers</span><span class="o">=</span><span class="n">headers</span><span class="p">)</span><span class="o">.</span><span class="n">text</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'<li><b>Name<\/b>:(.*)<\/li>'</span><span class="p">,</span><span class="n">ac</span><span class="p">)</span>
<span class="n">website</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'<li><b>Website.*<a href="(.*)">'</span><span class="p">,</span><span class="n">ac</span><span class="p">)</span>
<span class="n">contact</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'<li><b>.*<\/b>: <a href=</span><span class="se">\"</span><span class="s1">(.*)</span><span class="se">\"</span><span class="s1">>.*<\/a><\/li>'</span><span class="p">,</span><span class="n">ac</span><span class="p">)</span>
<span class="n">series</span> <span class="o">=</span> <span class="n">re</span><span class="o">.</span><span class="n">findall</span><span class="p">(</span><span class="s1">'<a href="\/series\/.*\/">(.*)<\/a>'</span><span class="p">,</span><span class="n">ac</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">contact</span><span class="p">)</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">author</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"İsim"</span> <span class="p">:</span> <span class="n">name</span><span class="p">,</span><span class="s2">"Websitesi"</span> <span class="p">:</span> <span class="n">website</span><span class="p">,</span><span class="s2">"İletişim"</span> <span class="p">:</span> <span class="n">contact</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span><span class="s2">"Seriler"</span> <span class="p">:</span> <span class="n">series</span><span class="p">}</span>
<span class="n">dosyayaz</span><span class="p">(</span><span class="n">author</span><span class="p">)</span>
<span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
<span class="k">pass</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">author</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"İsim"</span> <span class="p">:</span> <span class="n">name</span><span class="p">,</span><span class="s2">"Websitesi"</span> <span class="p">:</span> <span class="n">website</span><span class="p">,</span><span class="s2">"İletişim"</span> <span class="p">:</span> <span class="n">contact</span><span class="p">,</span><span class="s2">"Seriler"</span> <span class="p">:</span> <span class="n">series</span><span class="p">}</span>
<span class="n">dosyayaz</span><span class="p">(</span><span class="n">author</span><span class="p">)</span>
<span class="k">except</span> <span class="n">urllib</span><span class="o">.</span><span class="n">error</span><span class="o">.</span><span class="n">HTTPError</span><span class="p">:</span> <span class="c1">## Eğer herhangi bir şekilde ulaşamazsa sayfaya hata var diye uyarı veriyor.Nedense break etmemişim</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">'Hata var'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">dosyayaz</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="n">x</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
<span class="n">ac</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'yazarlar.txt'</span><span class="p">,</span><span class="n">mode</span><span class="o">=</span><span class="s2">"a"</span><span class="p">)</span>
<span class="n">ac</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">x</span><span class="o">+</span><span class="s2">"</span><span class="se">\n</span><span class="s2">"</span><span class="p">)</span>
<span class="n">ac</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">16</span><span class="p">):</span>
<span class="n">getir</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">linkler</span><span class="p">:</span>
<span class="n">yazartopla</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">liste</span><span class="p">:</span>
<span class="n">i</span> <span class="o">=</span> <span class="n">i</span>
<span class="n">git</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
</code></pre></div>
</article>
<footer>
<p>This entry is posted in <a href="https://enesergun.net/category/python.html">python</a>.</p>
</footer>
<div class="comments">
<div id="disqus_thread"></div>
<script type="text/javascript">
var disqus_shortname = '0x656e';
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="http://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
<a href="http://disqus.com" class="dsq-brlink">comments powered by <span class="logo-disqus">Disqus</span></a>
</div>
</div>
<footer class="blog-footer">
<ul class="nav">
<li><a href="https://devilinside.me/"> Eşelyon</a></li>
<li><a href="https://medium.com/@brkalbyrk"> Berk Albayrak</a></li>
<li><a href="https://ahmtglr.wordpress.com/"> Ahmet Güler</a></li>
<li><a href="https://kaganisildak.com/"> Kağan Işıldak</a></li>
<li><a href="https://eybisi.run/"> Eybisi</a></li>
<li><a href="https://onur.im/"> Onur Aslan</a></li>
<li><a href="https://canyoupwn.me/"> Canyoupwn.me</a></li>
<li><a href="https://www.oguzozkeroglu.com"> Oğuz Özkeroğlu</a></li>
<li><a href="https://aligoren.com"> Ali Gören</a></li>
<li><a href="https://0xf61.gitlab.io/"> Emir Kurt</a></li>
<br>
<li><a href="www.twitter.com/eness_ergun">Twitter</a></li>
<li><a href="info@enesergun.net">Mail</a></li>
</ul>
<p class="disclaimer">
Built with <a href="http://getpelican.com">Pelican</a>, and <a href="https://github.com/ka1nsha/MBPelican">MB Pelican</a> theme.
</p>
</footer>
</div>
</body>
</html>