Saturday, December 6, 2014

TLS/SSL Study Note

Preface

In the third assignment of "Network Security Practice", we are asked to trace TLS/SSL traffic package. I am writing down my understanding of TLS/SSL after studying on Wikipedia and other sites.

Procedure

Objects

  1. CLIENT A
  2. SERVER A: serving the desired application services for CLIENT A
  3. SERVER B: the server which issues digital certification for SERVER A

Steps

  1. [CLIENT] → [SERVER A]
    • request secure connection
    • offer a list of supported cipher suites
  2. [SERVER A] → [CLIENT]: send back followings:
    • picked cipher/hash function
    • its identification (digital certificate), mostly contains:
      • servername
      • trusted certificate authority
      • public encryption key
  3. [CLIENT] → [SERVER B]: check validity of SERVER A
  4. [CLIENT] ←→ [SERVER A]: generate the session key
    • [CLIENT] encrypts a random number using received public key, then send out the result
    • [SERVER B] decrypts with its private key, then get the random number
  5. [CLIENT] ←→ [SERVER A]: start various application-layer communications by encrypting/decrypting with the picked hash function & the random number

Reference

Sunday, November 30, 2014

Git Commit with GPG Key

Preface

Creating a commit in Git is easy, it can be done by "git commit", and the author (name and email) is set by a parameter of this commit or by reading the setting from "~/.gitconfig"

Method 1: set the author in command parameter

$ git commit -am "bug fixed" --author="Author Name <email@address.com>"

Method 2: reading the user setting

$ git config --global user.name "Heron Yang"
$ git config --global user.email heron@heron.me
However, there's no way to guarantee the author is the person with right permission. So GPG is introduced here.

GPG Design Explain

It's also applying public-private key pair design like ssh-keygen. Normally, the author generates a pair of keys, which are public key and private key. It's okay for him to share out the public key, and that allows the other people to create encrypted content for this author by using the public key. However, no one can decrypt the content unless he or she has the private key.
So, by sharing the public and holding the private key on your machine allows the user to be recognised and certificated.

Git Command with GPG Key

Therefore, if we want to create a Git commit with certification, we should make Git work with GPG. Here's the steps (one should install GPG before starts):

A. Generate key pair

$ gpg --gen-key #few questions will pop up, lease your name/email/passphrase, and pick default for others

B. List generated keys

$ gpg --list-keys # list your keys
pub   2048R/xxxxxxxx 2014-11-30
uid       [ultimate] Heron Yang (genrate gpg) <heron.yang.tw@gmail.com>
sub   2048R/yyyyyyyy 2014-11-30
$ gpg --list-secret-keys # list private keys
...

C. Add your GPG into Git Config

Put your xxxxxxxx into Git configuration by doing:
$ git config --global user.signingkey xxxxxxxx

D. Commit and See if it works

Commit like this way:
$ git commit -S
Check log:
$ git log --show-signature
commit 252aa0dd0643d86df16b93b509a6a15b95xxxxxx
gpg: Signature made Sun Nov 30 13:52:57 2014 CST using RSA key ID xxxxxxxxgpg: Good signature from "Heron Yang (genrate gpg) <heron.yang.tw@gmail.com>" [ultimate]
Author: Heron Yang <heron.yang.tw@gmail.com>
Date:   Sun Nov 30 13:52:51 2014 +0800
    test gpg

Reference

Monday, November 17, 2014

Alphanumeric Shellcode of EXEC("/bin/sh") without binsh/BINSH Characters

Abstract

This is one of the CTF tasks from "Secure Programming" course in NCTU.

Problem

Input: a string with following restrictions
  • Alphabets and Numbers only, which means: [0-9], [a-z], and [A-Z]
  • No "binsh/BINSH" characters
This string will be executed as machine code on the target server, which we call shellcode. So, we can guess that the program looks like this:


Workable Shellcode

First, let's ignore the restrictions first. We may need a test code to execute "bin/sh" successfully for further steps. And here's is it:

Also, by referring to the system call document, what we learn that the registers should be set before calling "INT 0x80":

  • EAX: 0x0b
  • EBX: Address of "bin/sh" string
  • ECX, EDX: 0x00 (optional)

Try: msfencode

cat shellcode.txt | msfencode BufferRegister=ECX -i sc.bin -e x86/alpha_mixed -t python -b 'binshBINSH'
This piece of code should give out our solution. However, the encoder returns error saying that it can't find the solution.

Write It on My Own

Since the existing encoder can't solve this problem, we now have to solve it on our own. The things we know so far:

Issue 1: Storing Arbitrary Value on Stack

  • PUSH a small random value into the stack
  • POP to ECX
  • DEC ECX to the wanted value
  • PUSH ECX
For larger wanted value:
  • PUSH a small random value into the stack
  • POP to EAX
  • XOR EAX, <value> (this may need to be done twice to gain the wanted value)
  • PUSH EAX
Notice:
  • the value for XOR should be calculated manually
  • pick ECX for DEC/POP/PUSH instructions under our initial restrictions
  • pick EAX for XOR (0x35 opcode)

Issue 2: Storing Instructions on Stack

There are some instructions not accepted in our input shellcode, so we somehow generate the instructions as data and push them onto the stack. By having "-fno-stack-protector -z execstack" option while compiling in GCC, we can execute the pushed instructions later.

Issue 3: Move EIP to Stack

  • EIP is in the stack and pointing to the current shellcode
  • ESP is where our instructions ("POP EBX", "INT 0x80") is pushed on
It will be easy if we call a jump instruction to move the EIP to ESP, then our instructions will be executed. However, we only have o8 (8-bit immediately) jump instructions, which is not long enough to jump to ESP.
Therefore, I popped the stack in order to increase ESP in the begin of the shellcode.
  • POP ESP for many times: increase ESP value
  • make ESP locates after (and close to) the end of our shellcode
  • call a small 8-bit jump to our stack (where "POP EBX", "INT 0x80" locate)

Solution

Here's my solution:
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXj0ZJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJRYQj0X5JCCX5Ul00Pj0X5JRYY5U007PTj0ZJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJRX5CG005q800Pj0X5kOOOPPj0ZJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJJRXJJJJJJJJJJJDDDDDDDDDDDDu0

And, the details are in my Github repo.

Wednesday, October 29, 2014

Radare2 Memo

Commands

Radare2 is a handy tool for analysing binary code, which offers a clean and fast way to browser binary file in assembly code. Here, I am writing down common commands I use so far:
  • fs: view flag sections
  • fs symbols: switch to "symbols" flag sections
  • f: show flag list of current symbol section
  • s main: jump to main (flag)
  • af: analysis function
  • pdf: view current function in assembly code
  • V: visual mode
  • p (in visual mode): switch between different view methods
  • q (in visual mode): quit visual mode
Configurations
  • e asm.syntax=(intel|att): change asm code syntax

Reference

Sunday, October 26, 2014

msfconsole Memo

In Secure Programming assignment, we are asked to get the flag on the server using shellcode solution. So, I've studied msfconsole and written some notes here:

Pick Target Platform / Action

First step, pick pick your target platform and the action using 'use' command. For example: use payload/linux/x86/exec

Show

'show encoders' to view the encoder list.

Generate Code

Generate your result by 'generate' command. And the options are as below
  • -h: see the help text
  • -b <opt>: the list of characters to avoid, ex. '\x00\xff'
  • -e <opt>: the name of the encoder module to use
  • -f <opt>: the output file name (otherwise stdout)
  • -i <opt>: the number of encoding iterations
  • -o <opt>: a comma separated list of options in VAR=VAL format
  • -s <opt>: add NOOP characters
  • -t <opt>: the output format: raw, ruby, rb, perl, pl, c, js_be, je_le, java, dll ...

More

  • You can execute shell commands in msfconsole directly.

Reference


Saturday, October 25, 2014

Detecting SSL in PHP on Heroku

In order to have a certified connection, websites would like to the user to use HTTPS instead of HTTP. However, sometimes we can't control what's the URL that the user request, which means that the user may ask for http://facebook.com instead of https://facebook.com.

So, to solve this problem, we detect that if the user is requesting HTTP URLs. And, if yes, response 301 to the user to ask him to redirect to our HTTPS site.

Normal, if we implement it in PHP, add the following code in the front of the file:


However, on Heroku, they don't have $_SERVER['HTTPS'] but $_SERVER['HTTP_X_FORWARDED_PROTO']) instead. So, we do:

Thursday, October 23, 2014

GDB Memo

Intro

GDB was a scaring tool for me years ago; however, I found it's actually pretty handful. And, instead of Googling the commands every time, I am writing down the common ones I use in this post:

Common Commands

General

  • kill: stop exec
  • run: start execution
  • quit
  • help

Monitoring

  • <ctl-c>: break exec
  • continue
  • list: see where's the exec stops
  • next: will go 'over' the function calls
  • step: will go 'into' the function calls
  • print variable: see variable
  • finish: return from a function

Call Stack

  • backtrace
  • info frame
  • info locals
  • info args

Breakpoint

  • break line number
  • break function name
  • tbreak: same as break but only stops once (temporary breakpoint)
  • info breakpoints
  • disable breakpoint number
  • ignore breakpoint number times: ignore the break point for number of times

Watchpoint

  • watch variable
  • rwatch variable: read watchpoint
  • awatch variable: read/write watchpoint
  • * info breakpoints
  • * disable breakpoint number

Memory

char *s = "hello!\n"
  • x/s s: print string
  • x/c s: print s[0]
  • x/4c s: print s[0]~s[3]
  • x/t s: print first 32 bit
  • x/x s: print 8 bytes in hex
  • info registers
  • core core: see core dump crash
  • nexti: 'next' for instruction level
  • stepi: 'step' for instruction level
  • disassemble function name

Other Helpful Commands

  • info proc
  • frame: show where am I

Print Variables (Organised)

  • * info variables: list "All global and static variable names"
  • * info locals: list "Local variables of current stack frame" (names and values), including static variables in that function
  • * info args: list "Arguments of the current stack frame" (names and values)

More: Fork

  • set follow-fork-mode mode: follow which process after fork
    • mode -> parent, child
    • show follow-fork-mode
  • set detach-on-fork mode (reference)
    • mode -> on, off
    • on(default): the child process (or parent process, depending on the value of follow-fork-mode) will be detached and allowed to run independently.
    • off: both processes will be held under the control of GDB. One process (child or parent, depending on the value of follow-fork-mode) is debugged as usual, while the other is held suspended.
    • show detach-on-fork
  • set follow-exec-mode mode
    • mode -> new, same

Reference