[Erlang20]一起攻克Binary
Cowboy aims to provide a complete HTTP stack in a small code base. It is optimized for low latency and low memory usage, in part because it uses binary strings.
- ErlangVM 是怎么实现Binary数据类型的,实现原理从宏观到细节,值得反复细读:http://www.cnblogs.com/zhengsyao/p/erlang_eterm_implementation_5_binary.html
- 这个是从应用层上去具体使用上去解释为什么Binary会非常高效且内存占用比List少:http://cryolite.iteye.com/blog/1547252
- 1 中提到的官方效率指南:http://www.erlang.org/doc/efficiency_guide/binaryhandling.html
- 如果要处理Binary最好自己写模式匹配或使用binary.erl里面的函数:http://stackoverflow.com/questions/21779394/erlang-high-memory-usage-for-processing-list-of-binary-parts
- 一个讲故事介绍Binary原理的文章:http://dieswaytoofast.blogspot.com/2012/12/erlang-binaries-and-garbage-collection.html
keep_0XX([{0,B2,B3}|Rest]) ->
[{0,B2,B3}|keep_0XX(Rest)];
keep_0XX([{1,_,_}|Rest]) ->
keep_0XX(Rest);
keep_0XX([]) ->
[].
或者使用下面的列表解析方式:
keep_0XX(List) ->
[{0,B2,B3} || {0,B2,B3} <- List].
keep_0XX(Bin) ->
[ <<0:1,B:2>> || <<0:1,B:2>> <= Bin].
<<Segment1,Segment2,...,Senmentn>>
Value:Size/TypeSpecifierList
| Segment | Default expansion |
| X | X:8/integer-unit:1 |
| X/float | X:64/float-unit:1 |
| X/binary | X:all/binary |
| X:size/binary | X:Size/binary-unit:8 |
Binary = <<10, 11, 12>>,
<<A:8, B/binary>> = Binary.
A=10,B=<<11,12>>.
<<Sz:8/integer,
Vsn:Sz/integer,
Msg/binary>> = <<16,2,154,42>>.
Sz = 16,Vsn=666,Msg=<<42>>.
case Binary of
<<42:8/integer, X/binary>> ->
handle_bin(X);
<<Sz:8, V:Sz/integer, X/binary>> when Sz > 16 ->
handle_int_bin(V, X);
<< :8, X:16/integer, Y:8/integer>> ->
handle int_int(X, Y)
end.
| Binary | Matching of X |
| <<42,14,15>> | <<14,15>> |
| <<24,1,2,3,10,20>> | <<10,20>> |
| <<12,1,2,20>> | 258 |
| <<0,255>> | failure |
1> [ X || <<X>> <= <<1,2,3,4,5>>, X rem 2 == 0].
[2,4]
5.2 如果你只是想把不是binary处理后变成一个binary就不用使用 <=2> << <<R:8, G:8, B:8>> || {R,G,B} <- [{213,45,132},{64,76,32},{76,0,0},{234,32,15}] >>.
<<213,45,132,64,76,32,76,0,0,234,32,15>>
113 | Module | Function | Arity
113代表的是fun类型, Module 和 Function 都是 atoms , Arity 是一个整数. 这些atoms可以使用 ATOM_EXT来解码 ,那Arity可以使用 SMALL_INTEGER_EXT解码 .
atoms的解码格式是这样子:
100 | Len | AtomName
Len 是AtomName的长度,有2bytes.
整数的解码格式是这样子:
97 | Int
Eshell V5.8.1 (abort with ^G) > term_to_binary(erlang). <<,,,,,,,,,>> > term_to_binary(halt). <<,,,,,,,>>
<<,,,,,,,,,,,,,,,>>
然后再把131(所有的term_to_binary/1都会加的), 113 (外部funs的类型标识)最后不要在结尾忘了arity:0:
<<,,,,,,,,,,,,,,,,,,,>>
这样,我们就把外部fun erlang:halt/0用binary的形式表现出来了!
> binary_to_term(<<,,,,,,,,,,,,,,,,,,,>>).
8>#Fun<erlang.halt.0>
那么,现在把我们的成果搞到tryerlang.org的shell里面:
>B = <<,,,,,,,,,,,,,,,,,,,>>.
然后我们再把B从binary转成Erlang term. 最开始时, tryerlang.org 可以使用 the binary_to_term function in safe mode. 这个函数从那次攻击之后也被加入黑名单,所以你只能在你自己的shell里面试试:)
>F = binary_to_term(B, [safe]).
现在我们来启动一个这个Fun看看:
>F().
很好,现在还是不行 tryerlang.org 会察觉到 erlang:halt/0 会被调用,然后把他阻塞住. 我们需要再小小改变一下:
erlang:halt/0 exists, taking exactly one argument. 来做. 我们只需要把最后一个0改成1,记得先使用BIF f/1把变量B.> f(B).
> B = <<,,,,,,,,,,,,,,,,,,,>>.
然后我们应该就可以啦:
> f(F).
>F = binary_to_term(B, [safe]).
>lists:map(F, []).
Please note that the hacker had the advantage to look at the source code for tryerlang.org while performing the attack.
I wanted to share this experience with all of you. I consider it highly constructive, since it leads to reflect on several aspects of Erlang

祝马上就要开学的各位高中生们逛街时偶遇班主任~~~哈哈~~~
[Erlang20]一起攻克Binary的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id
出现场景:当点击"分类"再返回"首页"时,发生error退出 BUG描述:Caused by: java.lang.IllegalArgumentExcep ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- Leetcode, construct binary tree from inorder and post order traversal
Sept. 13, 2015 Spent more than a few hours to work on the leetcode problem, and my favorite blogs ab ...
- [LeetCode] Binary Watch 二进制表
A binary watch has 4 LEDs on the top which represent the hours (0-11), and the 6 LEDs on the bottom ...
- [LeetCode] Find Leaves of Binary Tree 找二叉树的叶节点
Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...
随机推荐
- Bootstrap-CL:Well
ylbtech-Bootstrap-CL:Well 1.返回顶部 1. Bootstrap Well Well 是一种会引起内容凹陷显示或插图效果的容器 <div>.为了创建 Well,只 ...
- java之Jsch实现Linux的文件上传与下载
一.JSch是Java Secure Channel的缩写.JSch是一个SSH2的纯Java实现.它允许你连接到一个SSH服务器,并且可以使用端口转发,X11转发,文件传输等,当然你也可以集成它的功 ...
- javascript中不存在块级作用域,所以要小心使用在块级作用域中的函数声明所带来的作用域混乱.
在javascript中函数的作用域是一个非常重要的概念. javascript中是没有块级作用域,但是有函数作用域的概念. 我们在开发的过程中,经常会遇到这样的问题, 某个函数我暂时不需要,不想声明 ...
- 5月24日上课笔记-js操作DOM
解析properpties配置文件 类加载器 ResourceBundle 一.jquery操作DOM 1.jquery操作css css("",""); cs ...
- Linux gdb调试器用法全面解析
GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工具,GDB主要可帮助工程师完成下面4个方面的功能: 启动程序,可以按照工程师自定义的要求随心所欲的运行程序. 让被调试的程序在工程师指定的断 ...
- Oracle 性能调优之:使用 V$SQL_PLAN 视图查询内存中的执行计划
V$SQL_PLAN视图提供了一种方法,可用于检查仍位于库高速缓存的游标的执行计划.此视图中的信息与 PLAN_TABLE 视图中的信息非常类似.但是,EXPLAIN PLAN 显示的是执行相应语句时 ...
- python socket 详细介绍
Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供了标准的 BSD Sockets API. 第二个是 SocketServer, 它提供了服务器中心类,可以简化网络 ...
- Linux知识温习
进程间通信(IPC)介绍 mmap - 用户空间与内核空间 linux 进程地址空间的一步步探究 mmap 还是 shmget ? linux C/C++服务器后台开发面试题总结 Trie树详解及其应 ...
- C# user32.dll
#region User32.dll 函数 /// <summary> /// 该函数检索一指定窗口的客户区域或整个屏幕的显示设备上下文环境的句柄,以后可以在GDI函数中使用该句柄来在设备 ...
- 为什么nginx error_page遇到后端错误时不跳转?
nginx不得不说的参数之 proxy_intercept_errors与fastcgi_intercept_errors 为什么我的error_page 配置没有生效,没有正常跳转?我也遇到过这个问 ...