Hackthissite Programming 第11關心得

題目網址:https://www.hackthissite.org/missions/prog/11/

本關重點:眼明手快

題目:Reverse Ascii Shift
This string was randomly generated. It will not be recognizable text. You have 3 seconds to take the information from the website, and apply that to your algorithm.
Generated String: 82%102%64%75%114%101%91%107%72%90%
Shift: 29
大意是給你一串任意ascii碼,以任意特殊符號分隔。目的是把這一串ascii碼減去某個數字後,再還原成字元輸出。限制3秒鐘內...難就難在3秒鐘內要回答。

安全限制

原本想要把網頁內嵌在iframe中,當題目載入後,就從外框去抓內框的ascii碼,處理之後再submit。但是會違反相同來源政策,瀏覽器不讓我這樣搞。所以只能另外想辦法。

想到的方法有兩種,一種是用windows shell script登入網站讀取題目和關鍵字,處理完轉碼之後在送出表單,可是我覺得這樣很花時間,而且題目和關鍵字沒有特定的html tag包起來,只能讀取整個頁面,怕效率不好。第二種方法就是人工複製題目,寫一支小程式來轉碼,複製貼上進行轉碼,得到答案後再貼回答案欄。

用javascript寫小程式的限制

因為我懶惰,所以直接用javascript寫了小程式,這才發現,題目中的ascii碼有時候會超過html可以顯示的字元範圍(譬如:換行字元、刪除字元、esc字元等等)。還好經過多次嘗試之後,總是會有能正常顯示的組合出現,因此我勉強用了兩三個小時解開題目。當我解開時,也決定下次要採用更有效的方法。(想嘗試你的手速可以用這支下面小程式來玩玩)


<html>
<head>
</head>
<body>
<script>
function show()
{
 //str_html
 var str_html = document.getElementById('txt_html').value;
 //Generated String:
 var pos_start = str_html.search('Generated String: ');
 var pos_stop = str_html.search('Shift');
 var str_GS = str_html.substring(pos_start + 'Generated String: '.length,pos_stop);
 //分隔符號
 var spilt_tag = str_GS.substr(2,1);//從位置 pos_start 取長度 1 的分割符號
 //輸出陣列
 str_GS = str_GS.substr(0, str_GS.length-3);//截掉最後一個分隔符
 //str_GS = str_GS.replace(/(\r\n|\r|\n)/g, '\n');
 var ary_GS = new Array();
 ary_GS= str_GS.toString().split(spilt_tag);
 //Shift:
 pos_start = str_html.search('Shift: ')+'Shift: '.length;
 var srt_len = str_html.lastIndexOf(' ',pos_start);
 var str_SH = str_html.substr(pos_start,pos_stop);
 var int_str_SH = parseInt(str_SH, 10);
 //輸出答案
 var txt_answer = document.getElementById('txt_answer');
 //減 int_str_SH
 document.getElementById('dv_answer').innerHTML='';
 for(var i=0; i < ary_GS.length ; i++)
 {
  var int1 = parseInt(ary_GS[i], 10) - int_str_SH;
  if(i==0)
  {
   txt_answer.value = String.fromCharCode(int1);
   document.getElementById('dv_answer').innerHTML = String.fromCharCode(int1);
  }
  else
  {
   txt_answer.value = txt_answer.value + String.fromCharCode(int1);
   document.getElementById('dv_answer').innerHTML =  document.getElementById('dv_answer').innerHTML +String.fromCharCode(int1);
  }
 }
 txt_answer.select();
}
function clear_result()
{
 document.getElementById('txt_html').value='';
 document.getElementById('txt_answer').value='';
 document.getElementById('txt_html').focus();
}
</script>
<input type="text" id="txt_html" style="width:400px;height:200px;" onmouseover="clear_result();"><br>
<input type="button" onclick="show();" value="alert" style="width:400px;height:200px;"><br>
<input type="text" id="txt_answer" style="width:400px;height:200px;"><br>
<div id="dv_answer"></div>
</body>
</html>



留言

這個網誌中的熱門文章

資安JAVA(八):HTTP強制傳輸安全(HSTS)

以 SharpPcap 實作可收聽封包的 C# 程式

資安JAVA(四):Session Cookie HTTPOnly Flag