Category Archives: Programming

Using C/C++ for Python Extension

In general, C/C++ can be used to extend the functionality of Python with almost the highest performance you demand. To write a Python extension in C/C++ is relatively easy.

I'll show a simplified extension which is used in real life. This extension is made to extract records in a special file format, .pcap, and .pcap file is used to store the captured network packets so that the network activities can be analysed later.

Although there are many alternatives, they cannot achieve the goal in reasonable time. One of these alternatives is scapy, please don't get me wrong, scapy is a fabulous networking package. It can automatically parse all the records in .pcap file, which is an amazing feature. However, the parsing work will also take significant amount of time, especially for a large .pcap file with hundreds of thousands records inside.

At that time, my goal was quite straightforward. The time when captured the packet, from which source IP the packet was sent, and the destination IP of the packet. Given these demanding, there is no need to parse any record as deep as scapy would do. I can just check whether it contains IP layer or not, and if yes, extract the source IP and destination IP. Otherwise I'll skip to next record. And that's all.

I decided to name the extension as streampcap. And the class name would be StreamPcap so that I can write my Python code as below.

from streampcap import StreamPcap

pcap = StreamPcap("sample.pcap")
packet = pcap.next()
while packet is not None:
    print("{} {} {}".format(packet["time"], packet["ip_src"], packet["ip_dst"]))
    packet = pcap.next()

In order to implement this functionality, python-dev should be installed if the OS is Ubuntu/Debian/CentOS and etc Linux based operating systems. As for macOS, personally I use miniconda to manage the Python environment, and I think that miniconda will automatically get the same thing done. And miniconda is also available for Linux based OS. Life is easier!

Continue reading Using C/C++ for Python Extension

从零开始的 Rust 学习笔记(9)

Well, at this point, The Rust Programming Language demonstrates how to write a command line program, which named minigrep. Followed the textbook, I decided to rewrite the small utility that I mentioned in https://ryza.moe/2019/08/rewrite-the-styled-code-in-html-generated-by-apple-to-wordpress-compatible-html/.

The things learnt so far is enough to support me to write a, at least, workable utility. And if you're an expert in Rust, you'll find the following code is ugly and perhaps even not Rust-ish.

However, based on the previous 9 posts of this series, for these who just begins to learn Rust lang like me, the code which will be shown below won't be a giant jump. Nevertheless, there definitely has plenty of room to improve the following code. Any suggestions or questions are welcomed(⁎⁍̴̛ᴗ⁍̴̛⁎)

Furthermore, I googled a lot during writing the code. So I also attached corresponding link in comments.

Continue reading 从零开始的 Rust 学习笔记(9)

从零开始的 Rust 学习笔记(7)——Lifetime

这次的笔记只讲一个东西——Lifetime☆〜(ゝ。∂)

倒也不是说特别复杂,不过算是很与众不同的一个 feature~Rust的内存安全、无需 GC(垃圾回收) 则是因为有这个 feature(当然,真要在运行时搞事情的话,,编译器静态分析也未必能保证 100% 的安全)

然后我们说的内存安全的话,则是指需要禁止以下两种情况发生

  1. Use After Free
  2. Dangling Pointer

第一种情况的话,可能会导致 Segment Fault,也有可能会被 Hacker 利用,例如有些 iOS 版本上的越狱的一部分,则是基于 kernel 中包含了 UAF 的代码,UAF 的地址上的内容又可以被用户控制,随后通过一系列操作,在 kenrel 某些可以提权的代码里,再次 allocated 并用到这块被用户控制的内存时,就可以实现原本 需要 privileged 的操作了~

对于第二种情况的话,也就是「野指针」,比如某个函数返回了其栈上的内存的指针,而我们知道,当函数返回时,其栈上的内容是会被销毁的( ;´Д`)

举例如下~看看最后的输出是什么

#include <stdio.h>

int * stack_ref() {
    // local variable `ret` is located on stack
    int ret = 233;
    // return address of stack memory associated with local variable `ret`
    return &ret;
}   // at this point, the stack memory is no longer valid

int main(int argc, char *argv[]) {
    // get a ref, but on stack
    int * ref = stack_ref();
    // inc 4
    *ref += 4;
    
    // call that again
    stack_ref();
    
    // and please guess the value
    printf("Guess: %d\n", *ref);
}
Continue reading 从零开始的 Rust 学习笔记(7)——Lifetime

从零开始的 Rust 学习笔记(3)——Yet Another Way to Kill Your Brain

于是结合前面看到的语法,再加上 Google 的帮助,用 Rust 来写一个 Brainfuck 解释器吧~

这一篇与另一篇 post 联动,Brainfuck Interpreter in C++17——A Modern Approach to Kill Your Brain

其实在有了 C++ 写的经历之后,用 Rust 来重写一次,几乎就是熟悉一下 Rust 的基本语法,然后一些小的地方(比如 std::vector, std::stack, std::map 在 Rust 中的等价的类是什么)就靠着 Google 和 Rust 官方文档基本就可以写出来了

另外 Brainfuck 解释器的话,与别的很多东西比起来,似乎没有那么困难,写起来还是蛮快的

当然,有些 C++ 里的就没法依葫芦画瓢放到 Rust 里了,但终归只是一些小的修改~

Continue reading 从零开始的 Rust 学习笔记(3)——Yet Another Way to Kill Your Brain