quarta-feira, 22 de junho de 2016

I migrated to http://gkos.github.io, see you there :)

sexta-feira, 18 de janeiro de 2013

The end?

So many things happening..

I'm contributing to collectd
I'm contributing to riemann writing a client library in C
I'm contributing to emacs writing an interactive pastebin interface 

And I never made any effort to get more views on this blog. This was me and my self, documentating my learning. From the first post, when I was pĺaying with C pointers.. to now, that I can contribute to real opensource projects.. was a beatiful walk. 3 long years.. Maybe I create something like this with kernel stuff, starting from proc files playing to accepted kernel patches, who knows...

So I think is time to change and get more views, spread the knowledge I get everyday, and do some nicer posts, about linux, programming and offtopics too.. I'll try to migrate the archive from this blog to other platform. I really love markup languages, so I will try something with it .. and this is it!

Time to spread! Time to do real bloging! Time to change!

Cheers!

quinta-feira, 12 de julho de 2012

hex-to-ascii elisp function to fast decode protected donwload urls

Is really common to face download urls being "protected" but some annoying ringtone site. Some of then are so stupid that just encode the url as an hexadecimal string. Here is an example http://www.baixedetudo.net/id/?url=687474703a2f2f756c2e746f2f6f7977366b667732. Is easy to see the url here. I used perl to decode this, simple as in
print pack("H*", "687474703a2f2f756c2e746f2f6f7977366b667732"), "\n";
But as much I become an "emacs guy" more I do to easy my life.
Here is what I use from now to translate urls from hex to ascii The function usage is simple, just select the hex text and run it, you should get the translated text on clipboard.. You should be running emacs in its graphical form.
(defun hex-to-ascii (b e)
  "Translate the region from hex to ascii and copy it to clipboard.
I use that to translate urls in hex and paste it to url bar on my
browser."
  (interactive "r")
  (save-excursion
    (let ((i e)
           (x-select-enable-clipboard t)
           s)
      (while (> i b)
        (setq s (concat (format "%c" (read (concat "#x" (buffer-substring-no-properties (- i 2) i)))) s))
        (setq i (- i 2)))
      (kill-new s t)
      (message (format "%s copied to clip board" s)))))

I just keep this on my init.el.

Also I have done the opposite, a function that takes ascii string and returns its hex representation 
(defun ascii-to-hex (b e)
  "Translate an ascii string to a hex string and copy it to clipboard"
  (interactive "r")
  (save-excursion
    (let ((i b)
          (x-select-enable-clipboard t)
          s)
      (while (< i e)
        (setq s (concat s (format "%x" (get-byte i))))
        (setq i (+ i 1)))
      (kill-new s t)
      (message s))))

Nice and Easy :-)

quarta-feira, 11 de julho de 2012

elisp - get lines of text to a list

Here is an example of how get lines of text on a list.. I think I will use this on future for process text... Also I'm exercising my elisp skills since I want to be able to process text programmatically.
;; This text will be obtained 
;; by the function get-lines
;; It takes two parameters
;; The first being the start line (inclusive)
;; from with the text will be gathered 
;; The second being the end line (exclusive)
;; Nice and easy!! :-)

(defun get-beginning-of-line ()
  "Get the point at the beginning of line"
  (save-excursion
    (beginning-of-line)
    (point)))

(defun get-end-of-line ()
  "Get the point at the end of line"
  (save-excursion
    (end-of-line)
    (point)))

(defun programmatic-goto-line (line)
  "As goto-line but better for programming stuff"
  (goto-char (point-min))
  (forward-line (- line 1)))


(defun get-lines (start-line end-line)
  "Return a list with the lines between START-LINE (inclusive) and END-LINE (exclusive)"
  (save-excursion
    (programmatic-goto-line end-line)
    (let (lines)
      (while (< start-line (line-number-at-pos))
        (forward-line -1)
        (setq lines (cons (buffer-substring-no-properties (get-beginning-of-line) (get-end-of-line)) lines)))
      lines)))
      
               
;; Example
(let (v)
  (dolist (v (get-lines 1 7))
    (princ (format "%s\n" v))))

;; This text will be obtained 
;; by the function get-lines
;; It takes two parameters
;; The first being the start line (inclusive)
;; from with the text will be gathered 
;; The second being the end line (exclusive)
nil


sexta-feira, 6 de julho de 2012

My two "just arrived" new kernel books

Understading the Linux Kernel 

amd Linux Device Drivers

terça-feira, 3 de julho de 2012

Resolving names and IPs

Here is two examples of name resolving in linux.. I use getaddrinfo() and getnameinfo() respectively..


getaddrinfo: given a name retuns all translated IPs, one per line
/*
 * File: getaddrinfo.c
 * Compile: gcc getaddrinfo.c -o getaddrinfo
 * Usage: ./getaddrinfo FQN
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main(int argc, char **argv)
{
        int error;

        struct addrinfo saddr, *psaddr, *ptr;

        memset(&saddr, '\0', sizeof(saddr));
        saddr.ai_family = AF_INET;


        saddr.ai_socktype = SOCK_STREAM;

        error = getaddrinfo(argv[1], NULL, &saddr, &psaddr);
        if (error) {
                fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error));
                exit(EXIT_FAILURE);
        }

        for (ptr = psaddr; ptr; ptr = ptr->ai_next) {
                puts(inet_ntoa(((struct sockaddr_in *)    
                               ptr->ai_addr)->sin_addr));
        }

        return 0;
}

getnameinfo: Given an IP returns the name that first resolves to it. 
/*
 * File: getnameinfo.c
 * Compile: gcc getnameinfo.c -o getnameinfo
 * Usage: ./getnameinfo IP
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

static char hostname[255];

int main(int argc, char **argv)
{
        int error;
        struct sockaddr_in saddr_in;



        memset(&saddr_in, '\0', sizeof(saddr_in));
        saddr_in.sin_family = AF_INET;
        error = inet_aton(argv[1], &saddr_in.sin_addr);
        if (error == 0) {
                perror("inet_aton");
                exit(EXIT_FAILURE);
        }

        error = getnameinfo((struct sockaddr *)&saddr_in, sizeof(saddr_in),
                            hostname, sizeof(hostname),
                            NULL, 0, NI_NAMEREQD);
        if (error) {
                fprintf(stderr, "getnameinfo: %s\n", gai_strerror(error));
                exit(EXIT_FAILURE);
        }

        puts(hostname);
        return 0;
}


Cheers

terça-feira, 17 de abril de 2012

My first shellcode :)

/*
 * File: shello.c
 *
 * Generated from this assembly code:
 *      pushl   %ebp
 *      movl    %esp, %ebp
 *      
 *      subl    $12, %esp
 *      movl    $0x6c6c6548, -12(%ebp)
 *      movl    $0x6f57206f, -8(%ebp)
 *      movl    $0x0a646c72, -4(%ebp)
 *      
 *      movl    $4, %eax        
 *      movl    $1, %ebx
 *      leal    -12(%ebp), %ecx 
 *      movl    $12, %edx 
 *      
 *      int     $0x80
 *      addl     $12, %esp
 *      
 *      leave
 *      ret
 * 
 */

/*
 * Tested on Linux hilstdsk 3.2.7-1-ARCH #1 SMP PREEMPT Tue Feb 21
 * 16:59:04 UTC 2012 i686 AMD Athlon(tm) 64 X2 Dual Core Processor
 * 4400+ AuthenticAMD GNU/Linux
 * Archlinux
 */

/*
 * Compile: gcc -o shello shello.c
 * Run: ./shello
 * Output: Hello World
 */
 
/*
 * Thats pretty cool!
 */
#include 

static char shellcode[] = "\x55"
        "\x89\xe5"
        "\x83\xec\x0c"
        "\xc7\x45\xf4\x48\x65\x6c\x6c"
        "\xc7\x45\xf8\x6f\x20\x57\x6f"
        "\xc7\x45\xfc\x72\x6c\x64\x0a"
        "\xb8\x04\x00\x00\x00"
        "\xbb\x01\x00\x00\x00"
        "\x8d\x4d\xf4"
        "\xba\x0c\x00\x00\x00"
        "\xcd\x80"
        "\x83\xc4\x0c"
        "\xc9"
        "\xc3";

int main(void)
{
        void (*p)(void);
        p = shellcode;
        p();
        return 0;
}