SDB

SDB stands for String DataBase. It’s a simple key-value database that only operates with strings created by pancake. It is used in many parts of r2 to have a disk and in-memory database which is small and fast to manage using it as a hashtable on steroids.

SDB is a simple string key/value database based on djb’s cdb disk storage and supports JSON and arrays introspection.

There’s also the sdbtypes: a vala library that implements several data structures on top of an sdb or a memcache instance.

SDB supports:

  • namespaces (multiple sdb paths)
  • atomic database sync (never corrupted)
  • bindings for vala, luvit, newlisp and nodejs
  • commandline frontend for sdb databases
  • memcache client and server with sdb backend
  • arrays support (syntax sugar)
  • json parser/getter

Usage example

Let’s create a database!

  1. $ sdb d hello=world
  2. $ sdb d hello
  3. world

Using arrays:

  1. $ sdb - '[]list=1,2' '[0]list' '[0]list=foo' '[]list' '[+1]list=bar'
  2. 1
  3. foo
  4. 2
  5. foo
  6. bar
  7. 2

Let’s play with json:

  1. $ sdb d g='{"foo":1,"bar":{"cow":3}}'
  2. $ sdb d g?bar.cow
  3. 3
  4. $ sdb - user='{"id":123}' user?id=99 user?id
  5. 99

Using the command line without any disk database:

  1. $ sdb - foo=bar foo a=3 +a -a
  2. bar
  3. 4
  4. 3
  5. $ sdb -
  6. foo=bar
  7. foo
  8. bar
  9. a=3
  10. +a
  11. 4
  12. -a
  13. 3

Remove the database

  1. $ rm -f d

So what ?

So, you can now do this inside your radare2 sessions!

Let’s take a simple binary, and check what is already sdbized.

  1. $ cat test.c
  2. int main(){
  3. puts("Hello world\n");
  4. }
  5. $ gcc test.c -o test
  1. $ r2 -A ./test
  2. [0x08048320]> k **
  3. bin
  4. anal
  5. syscall
  6. debug
  1. [0x08048320]> k bin/**
  2. fd.6
  3. [0x08048320]> k bin/fd.6/*
  4. archs=0:0:x86:32

The file corresponding to the sixth file descriptor is a x86_32 binary.

  1. [0x08048320]> k anal/meta/*
  2. meta.s.0x80484d0=12,SGVsbG8gd29ybGQ=
  3. [...]
  4. [0x08048320]> ?b64- SGVsbG8gd29ybGQ=
  5. Hello world

Strings are stored encoded in base64.


More Examples

List namespaces

  1. k **

List sub-namespaces

  1. k anal/**

List keys

  1. k *
  2. k anal/*

Set a key

  1. k foo=bar

Get the value of a key

  1. k foo

List all syscalls

  1. k syscall/*~^0x

List all comments

  1. k anal/meta/*~.C.

Show a comment at given offset:

  1. k %anal/meta/[1]meta.C.0x100005000