<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>
	<channel>
		<title>MAIN.LV</title>
		<link>http://www.main.lv/</link>
		<description>by main.lv</description>
		<language>lv-lv</language>
		<pubDate>Sun, 20 May 2012 15:19:35 GMT</pubDate>
		<atom:link href='http://www.main.lv/posts/rss/' rel='self' type='application/rss+xml' />
		<generator>CakePHP</generator>
		<managingEditor>dos21h@gmail.com</managingEditor>
		<webMaster>dos21h@gmail.com</webMaster>
				<item>
			<title>Create small ELF file byte by byte</title>
			<link>http://www.main.lv/posts/view/create-small-elf-file-byte-by-byte</link>
			<description>Creating smallest possible elf file.

Structure of ELF file:
&lt;table&gt;
&lt;tr&gt;&lt;td&gt;Elf header&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Program header&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Code Part&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Data Part&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

C structure of ELF header /usr/include/elf.h:
&lt;pre class="brush:c"&gt;
	typedef struct
	{
	  unsigned char	e_ident[EI_NIDENT];	/* Magic number and other info */
	  Elf64_Half	e_type;				/* Object file type */
	  Elf64_Half	e_machine;			/* Architecture */
	  Elf64_Word	e_version;			/* Object file version */
	  Elf64_Addr	e_entry;			/* Entry point virtual address */
	  Elf64_Off		e_phoff;			/* Program header table file offset */
	  Elf64_Off		e_shoff;			/* Section header table file offset */
	  Elf64_Word	e_flags;			/* Processor-specific flags */
	  Elf64_Half	e_ehsize;			/* ELF header size in bytes */
	  Elf64_Half	e_phentsize;		/* Program header table entry size */
	  Elf64_Half	e_phnum;			/* Program header table entry count */
	  Elf64_Half	e_shentsize;		/* Section header table entry size */
	  Elf64_Half	e_shnum;			/* Section header table entry count */
	  Elf64_Half	e_shstrndx;			/* Section header string table index */
	} Elf64_Ehdr;
&lt;/pre&gt;



Structure of Program header file /usr/include/elf.h:
&lt;pre class="brush:c"&gt;
	typedef struct
	{
	  Elf64_Word	p_type;			/* Segment type */
	  Elf64_Word	p_flags;		/* Segment flags */
	  Elf64_Off		p_offset;		/* Segment file offset */
	  Elf64_Addr	p_vaddr;		/* Segment virtual address */
	  Elf64_Addr	p_paddr;		/* Segment physical address */
	  Elf64_Xword	p_filesz;		/* Segment size in file */
	  Elf64_Xword	p_memsz;		/* Segment size in memory */
	  Elf64_Xword	p_align;		/* Segment alignment */
	} Elf64_Phdr;
&lt;/pre&gt;


This structures is all what we need to make our ELF file.
Now we will look inside kernel source and see that 
we need only one program header for our program. All big programs
using usually two program headers one for code and one for data.

/linux-3.3.1/fs/binfmt_elf.c:605
&lt;pre class="brush:c"&gt;	
	if (loc-&gt;elf_ex.e_phnum &lt; 1 ||
		loc-&gt;elf_ex.e_phnum &gt; 65536U / sizeof(struct elf_phdr))
		goto out;
&lt;/pre&gt;

Step by step there should be filled all
fields of the ELF header structure.

&lt;pre class="brush:c"&gt;
	typedef struct
	{
	  unsigned char	e_ident[EI_NIDENT];	/* default values of ELFMAG,ELFCLASS64,ELFDATA2LSB */
	  Elf64_Half	e_type;				/* we making executable then it would be ET_EXEC  */
	  Elf64_Half	e_machine;			/* Architecture is 0x3e(EM_X86_64) 
										 (not from elf header 
										 from /binutils/include/elf/common.h) */
	  Elf64_Word	e_version;			/* Object file version EV_CURRENT */
	  Elf64_Addr	e_entry;			/* Entry point virtual address points to
										 main function it is with label entrypoint */
	  Elf64_Off		e_phoff;			/* Program header table file offset */
										  offset of program header sizeof(Elf64_Ehdr)
	  Elf64_Off		e_shoff;			/* Section header table file offset 
											there is no section header */
	  Elf64_Word	e_flags;			/* No processor-specific flags 
											*/
	  Elf64_Half	e_ehsize;			/* ELF header size in bytes 
											0x40 sizeof(Elf64_Ehdr)
	  Elf64_Half	e_phentsize;		/* Program header table entry size 
											0x38 sizeof(Elf64_Phdr) */
	  Elf64_Half	e_phnum;			/* Program header table entry count 
											0x01 */
	  Elf64_Half	e_shentsize;		/* Section header table entry size 
											I put 0x40 */
	  Elf64_Half	e_shnum;			/* Section header table entry count 
											0x00 */
	  Elf64_Half	e_shstrndx;			/* There is no section header and 
										 string table index is 0x0 then */
	} Elf64_Ehdr;
&lt;/pre&gt;


With program header we will tell kernel how to load our file in memory
and with part of file will be mmaped to needed address. As our data
and code is placed in one address space and kernel ELF source says
that there is enough with 1 program header then we will use only 1.

&lt;pre class="brush:c"&gt;
	typedef struct
	{
	  Elf64_Word	p_type;			/* Segment type PT_LOAD */
	  Elf64_Word	p_flags;		/* Segment flags PF_X,PF_R,PF_W
									as our memory should be readable, writable and
									executable as it contains code and data */
	  Elf64_Off		p_offset;		/* Segment file offset 
										point to offset of entry point label offset
										in file */
	  Elf64_Addr	p_vaddr;		/* Segment virtual address 
										64bits programs is usually at 0x400000+code_file_offset*/
	  Elf64_Addr	p_paddr;		/* Segment physical address 
										same as above*/
	  Elf64_Xword	p_filesz;		/* Segment size in file 
										size of code and data if file */
	  Elf64_Xword	p_memsz;		/* Segment size in memory 
										same as above */
	  Elf64_Xword	p_align;		/* Segment alignment 
										same as all programs have on my PC*/
	} Elf64_Phdr;
&lt;/pre&gt;

Now everything is ready. Only thing that is left is code some small code
that uses data. And it would be hello world

&lt;pre class="brush:asm"&gt;
mov eax, 1
mov edx, 12
mov rsi, qword 0x040009c ;address of string 
mov edi, 1
syscall

xor edi, edi
mov eax, 60
syscall

msg db &quot;Hello World&quot;,0xA
&lt;/pre&gt;

To calculate offsets of code and data labels is used macro:

&lt;pre class="brush:asm"&gt;
macro doffset
{	
	bits = 16
    display &quot; 0x&quot;
    repeat bits/4
        d = &quot;0&quot; + $ shr (bits-%*4) and 0Fh
        if d &gt; &quot;9&quot;
            d = d + &quot;A&quot;-&quot;9&quot;-1
        end if
        display d
    end repeat
    display 13,10
}
&lt;/pre&gt;

Total size of executable on 64bit system:
&lt;table border="1"&gt;
&lt;tr&gt;
  &lt;th&gt;Program part&lt;/th&gt;&lt;th&gt;Size&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;ELF header size&lt;/td&gt;&lt;td&gt;0x40&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Program header&lt;/td&gt;&lt;td&gt; 0x38&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Code size&lt;/td&gt;&lt;td&gt; 0x24&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Data size&lt;/td&gt;&lt;td&gt; 0xc&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Total:&lt;/td&gt;&lt;td&gt; 168 bytes&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;

If 32 bit system is used then need to find defintions of data structures
and retype some bytes. Also architecture variable need to be changed.

Future plans:
	Add some shared libs and compile smallest possible program using
SDL graphics lib.

&lt;b&gt;Code:&lt;/b&gt;
&lt;b&gt;&lt;a href="../files/small_elf_file.zip"&gt;Source&lt;/a&gt;&lt;/b&gt;
Code is written and tested on x86_64.

&lt;b&gt;Links:&lt;/b&gt;
&lt;b&gt;[1]&lt;/b&gt; http://refspecs.freestandards.org/elf/elf.pdf
</description>
			<pubDate>Thu, 10 May 2012 19:22:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/128</guid>
		</item>
				<item>
			<title>List ELF section names</title>
			<link>http://www.main.lv/posts/view/list-elf-seection-names</link>
			<description>Every ELF (Executable Linux Format) file has standard structure.
There is section names that used to identify purpose of section.

Here is example how to write all names of all ELF sections.

Here is steps that we have taken:
1. Find String Table Section
2. Get all section names from string table section
3. Run trough all section an get names of sections

First of all we need get ELF header (Elf32_Ehdr) from position 0.
ELF header have offset of section headers (Elf32_Ehdr.e_shoff).

Sting table section have attributes with help us to recognize it:
1. string table section header address in memory (Elf32_Shdr.sh_addr) is 0
2. its type (Elf32_Shdr.sh_type) is SHT_STRTAB = 3
3. and it is first section with such attributes

To get trough all sections we make for cycle. We can get number
of sections from (Elf32_Ehdr.e_shnum) .
we run all trough all sections and checking for 3 string table section 
rules.

&lt;pre class="brush:c"&gt;
for ( iter_s=0; iter_s &lt; ELFheader.e_shnum; iter_s++  )
	{
		fseek( f, ELFheader.e_shoff+(ELFheader.e_shentsize*iter_s), SEEK_SET);
		fread( &amp;STRheader, ELFheader.e_shentsize, 1, f );
		if ((STRheader.sh_type == SHT_STRTAB) &amp;&amp; 
			(STRheader.sh_addr == 0x00000000))
		{
			//some code
			iter_s=ELFheader.e_shnum+1; //this is to exit from for cycle
		}
	}
&lt;/pre&gt;

String table section has all section names as strings. Section name
is in (Elf32_Shdr.sh_name) as position number of strings first symbol.

All string table values we read inside buffer

&lt;pre class="brush:c"&gt;
fseek( f, STRheader.sh_offset, SEEK_SET);
fread( STR_buffer, STRheader.sh_size, 1, f);
&lt;/pre&gt;

Now we can get section name with 

&lt;pre class="brush:c"&gt;
printf("%s\n", STR_buffer+ITERheader.sh_name);
&lt;/pre&gt;

This is example code to get some info from ELF file. There is allot other
info that can be gained from ELF file.

</description>
			<pubDate>Thu, 15 Dec 2011 20:32:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/127</guid>
		</item>
				<item>
			<title>C inline assembler</title>
			<link>http://www.main.lv/posts/view/c-inline-assembler</link>
			<description>There is long time since wanted to learn "creepy" gcc inline assembly.
Looking at manuals its not so hard and "creepy". Using it is more
interesting and dissambly of compiled code is very nice looking.

volatile puts our asm code where it is and don&quot;t optimize it without
volatile it can optimize.

What to write in __asm__ directive looks like this

&lt;pre class="brush: c"&gt;
__asm__ __volatile__("our_code":output:input:used)
&lt;/pre&gt;

as code to convert to inline asm we will use last post &lt;a href="#link1"&gt;[2]&lt;/a&gt;. 

There is only one instruction that we using and it usage was

&lt;pre class="brush: asm"&gt;
get_timer:
	rdtsc
	ret
&lt;/pre&gt;

its not very optimal and for 1 instruction writing whole function
its not beautiful. We remember that returning result of this function is 
saved in eax register.

&lt;pre class="brush: asm"&gt;
__asm__ __volatile__("rdtsc":"=a"(x)::)
&lt;/pre&gt;

code looks like this. But we can make it as define function

&lt;pre class="brush: c"&gt;
#define get_timer(X) __asm__ __volatile__("rdtsc":"=a"(X)::)
&lt;/pre&gt;

This code works fine and give 70058 ticks on cycle
When adding option -O2 then result becomes wherry strange.

As we remember that rdtsc return result in edx:eax then we add to 
used registers(clobber) %edx. 

&lt;pre class="brush: c"&gt;
#define get_timer(X) __asm__ __volatile__("rdtsc":"=a"(X)::"%edx")
&lt;/pre&gt;

And also we can rewrite everything as 
inline function.

&lt;pre class="brush: c"&gt;
static inline unsigned int get_timeri()
{
	unsigned int i;
	__asm__ __volatile__("rdtsc":"=a"(i)::);
	return i;
}
&lt;/pre&gt;

Now this two functions works fine with -O options.
When empty cycle is optimized then it becomes empty and resulting 
tick number is 32 for both inline function and define macro. 
It not working for his main purpose. When no optimization switched
then get_timer works for some ticks faster then get_timeri.

We can add attribute always inline and we will win some ticks
and function will always inline regards optimization level

&lt;pre class="brush: c"&gt;
__attribute__((always_inline)) unsigned int get_timeri() 
&lt;/pre&gt;

Too fix test cycle for our measurement we make it as object file
and it will compiled without options.

&lt;pre class="brush: c"&gt;
void fixed_cycle()
{
	int i;
	for (i=0;i&lt;10000;i++)
	{
	}
}
&lt;/pre&gt;

Now everything looks quite good and also inline assembly works as expected.

For reference about inline asm you can go to &lt;a href="#link1"&gt;[1]&lt;/a&gt;

&lt;b&gt;&lt;a href="../files/asm_inline.zip"&gt;Source&lt;/a&gt;&lt;/b&gt;

&lt;b&gt;Links&lt;/b&gt;
&lt;b id="link1"&gt;[1]&lt;/b&gt;&lt;a href="http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html"&gt;http://www.ibiblio.org/gferg/ldp/GCC-Inline-Assembly-HOWTO.html&lt;/a&gt;
&lt;b id="link2"&gt;[2]&lt;/b&gt;&lt;a href="http://main.lv/post/linux-antidebug-5"&gt;http://main.lv/post/linux-antidebug-5&lt;/a&gt;
</description>
			<pubDate>Sun, 30 Oct 2011 16:30:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/126</guid>
		</item>
				<item>
			<title>Linux antidebug 5</title>
			<link>http://www.main.lv/posts/view/linux-antidebug-5</link>
			<description>When debuging programm line by line or when running it
in some debugger then ther can be some time delays when you
pressing buttons. We can measure them with asm command

&lt;pre class="brush: asm"&gt;
rdtsc 
&lt;/pre&gt;

this instruction read time-stamp counter into &lt;b&gt;edx:eax&lt;/b&gt; in our programm will be enought values from
eax

function for c that uses rdtsc is

&lt;pre class="brush: cpp"&gt;
extern int get_timer()
&lt;/pre&gt;

in fasm it looks like

&lt;pre class="brush: cpp"&gt;
get_timer:
	rdtsc
	ret
&lt;/pre&gt;

ther is writen code

&lt;pre class="brush: cpp"&gt;
s = get_timer();
for (i=0;i&lt;10000;i++)
{
}
e = get_timer();
d = e - s;
&lt;/pre&gt;

average time to execute 10000 is 70069 ticks for value
on with we detecting how fast working code i have choose
twice of average 120000 if execution time is larger then
probably it is debuged.

&lt;b&gt;Compile&lt;/b&gt;
&lt;code&gt;
make
&lt;/code&gt;


</description>
			<pubDate>Wed, 28 Sep 2011 20:50:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/125</guid>
		</item>
				<item>
			<title>Linux antidebug 4</title>
			<link>http://www.main.lv/posts/view/linux-antidebug-4</link>
			<description>Here is one more method how to check if your application is debugged.
Need to set signal handler with handles interrupt number 3 with is used
for step by step debugging

&lt;b&gt;Compile:&lt;/b&gt;
&lt;code&gt;gcc main.c -o main&lt;/code&gt;

&lt;pre class="brush: cpp"&gt;
#include &lt;signal.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

#define FALSE 0
#define TRUE  1

void sig_handler( int );

int debuging;

int main()
{
	debuging = FALSE;
	signal(SIGTRAP, sig_handler);
	__asm__("int3");
	if (debuging == FALSE)
	{
		printf("Nothing special\n");
	} else
	{
		printf("Playing seek and hide\n");
	}
	exit(1);
}

void sig_handler( int sig)
{
	debuging = TRUE;
}
&lt;/pre&gt;

&lt;b&gt;Run:&lt;/b&gt;
&lt;code&gt;./main&lt;/code&gt;

Example with asm

&lt;b&gt;Compile:&lt;/b&gt;
&lt;code&gt;fasm ad4.asm ad4.o
&lt;br/&gt;gcc ad4.o -o ad4
&lt;/code&gt;

&lt;pre class="brush: asm"&gt;
format ELF

include &quot;ccall.inc&quot;

SYS_EXIT	equ		1
SIGTRAP		equ		5
TRUE		equ		1
FALSE		equ		0
section &quot;.text&quot; executable

public main

extrn printf
extrn exit
extrn signal

main:
	ccall	signal, SIGTRAP, sig_handler
	int		3h
	
	cmp		[debug],FALSE
	jne		no_dbg
	ccall	printf,str1
	jmp exit
	
no_dbg:
	ccall	printf,str2

to_exit:
	mov		eax, SYS_EXIT
	mov		ebx, 0
	int		80h

sig_handler:
	param1 equ dword [ebp+8]	
	mov		[debug], TRUE
	ret

section &quot;.data&quot; writable

debug	db	FALSE
str1	db "Under debug",0xA,0
str2	db "No debug",0xA,0
&lt;/pre&gt;

Tested and works for gdb and ald.

&lt;b&gt;Links:&lt;/b&gt;
&lt;br&gt;[1] &lt;a href="http://blog.binarycell.org/2011/04/simple-antidebugging-methods-part-2.html"&gt;http://blog.binarycell.org/2011/04/simple-antidebugging-methods-part-2.html&lt;/a&gt;
</description>
			<pubDate>Thu, 15 Sep 2011 19:48:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/124</guid>
		</item>
				<item>
			<title>Embeding Lua in C</title>
			<link>http://www.main.lv/posts/view/embeding-lua-in-c</link>
			<description>Bedimming lua in you C programs is can be done in few minutes.
Many examples is in &lt;a href="http://lua-users.org/wiki/SampleCode"&gt;lua-users.org&lt;/a&gt;.

First thing to write is module and then compile everything with lua precompiled lib.

&lt;pre class="brush: cpp"&gt;
int module_register(lua_State*);
void module_print(lua_State*);
int module_getone(lua_State*);

int module_gc(lua_State*);
int module_tostring(lua_State*);

static const luaL_reg module_methods[] =
{
	//{,(void *)},
	{"print",	(void *)module_print},
	{"getone",	(void *)module_getone},
	{0,			0}
};

static const luaL_reg module_meta[] = 
{
	{"__gc",		(void *)module_gc},
	{"__tostring",	(void *)module_tostring},
	{0, 0}
};
&lt;/pre&gt;
to make printf("%s\n") available in lua
&lt;pre class="brush: cpp"&gt;
void module_print( lua_State *L)
{
	int argc = lua_gettop(L);
	int n;
	for (n=1; n &amp;lt;= argc; n++) printf("%s\n", lua_tostring(L, n));
}
&lt;/pre&gt;
next one function that have return value 1
&lt;pre class="brush: cpp"&gt;
int module_getone(lua_State *L)
{
	int x=1;
	lua_pushnumber(L, x);
	return 1;
}
&lt;/pre&gt;
and easy to compile if needed.
&lt;code&gt;gcc -c module.c&lt;/code&gt;
&lt;code&gt;gcc module.o main.c -o main -llua&lt;/code&gt;

Links:
[1] &lt;a href="http://lua-users.org/wiki/SampleCode"&gt;http://lua-users.org/wiki/SampleCode&lt;/a&gt;</description>
			<pubDate>Wed, 10 Aug 2011 19:37:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/113</guid>
		</item>
				<item>
			<title>AVR disassembler</title>
			<link>http://www.main.lv/posts/view/avr-disassembler</link>
			<description>Disassembler for Atmel AVR microcontrollers made for be fast and simple. No extra features only
basics.&amp;nbsp; Converts binary file to AVR asm output.

If you have ihex then you can convert it to binary with
&lt;a href="/post/reprbin-represent-binary-files-in-different-formats"&gt;ReprBin&lt;/a&gt;

Here is example output&lt;pre class="brush: asm"&gt;2411      CLR   0x11  
be1f      OUT   0x3f   , 0x1    
e5cf      LDI   0xc    , 0x5f   
e0d4      LDI   0xd    , 0x4    
bfde      OUT   0x3e   , 0x1d   
bfcd      OUT   0x3d   , 0x1c   
e010      LDI   0x1    , 0x0    
e6a0      LDI   0xa    , 0x60   
e0b0      LDI   0xb    , 0x0    
ebee      LDI   0xe    , 0xbe   
e0f0      LDI   0xf    , 0x0    
c002      RJMP  +4    
9005      LPM   0x0   
920d      ST    0x0    , 0x0    
36a0      CPI   0xa    , 0x60   
07b1      CPC   0x1b   , 0x11   
f7d9      BRBC  0x1    , -10 
e010      LDI   0x1    , 0x0    
e6a0      LDI   0xa    , 0x60   
e0b0      LDI   0xb    , 0x0    
c001      RJMP  +2    &lt;/pre&gt;</description>
			<pubDate>Fri, 1 Jul 2011 22:33:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/112</guid>
		</item>
				<item>
			<title>Phase space for chaos</title>
			<link>http://www.main.lv/posts/view/phase-space-for-chaos</link>
			<description>Phase space for some chaotic function. And see the picture of it.&lt;pre class="brush: py"&gt;from MO0_w0 import t
from pylab import *

dt = []
for tt in xrange(0,len(t)-1):
	dt.append(t[tt+1]-t[tt])

figure(1)
plot(dt,t[:len(t)-1])
show()&lt;/pre&gt;</description>
			<pubDate>Mon, 30 May 2011 22:55:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/110</guid>
		</item>
				<item>
			<title>Hooking interrupt descriptor table  </title>
			<link>http://www.main.lv/posts/view/hooking-interrupt-descriptor-table</link>
			<description>Hook interrupt descriptor table  

Hooking interrupt table is very interesting thing
with it you can dissallow some operations to be made or watch what
happening in system. This article is more like review and more tehnical
description is in link 1

First thing that we should know that it will done trought kernel module
there is 2 commands for loading and unloading modules

&lt;code&gt;
insmod
&lt;/code&gt;

and

&lt;code&gt;
rmmod
&lt;/code&gt;

there is way how we can check system call addresses and position of syscall
table

&lt;code&gt;
grep sys_call_table /proc/kallsyms 
&lt;br/&gt;grep system_call /proc/kallsyms
&lt;/code&gt;
&lt;br/&gt;also we can use it for detecting our module functions and syscall addreses

&lt;br/&gt;&lt;code&gt;
grep sys_write /proc/kallsyms
&lt;/code&gt;

&lt;br/&gt;or if we whant check out module functions

&lt;br/&gt;&lt;code&gt;
grep hook_idt /proc/kallsyms
&lt;/code&gt;

&lt;br/&gt;We will now try to hook &lt;i&gt;sys_mkdir&lt;/i&gt;. I usualy using some minimalistic
windowmanagers but some browsers or other GUIsh programs like such directories
"Download" or "Desktop" all my directories in ~/ is lowercase and I realy hate
anoying "Download" and "Desktop" directories that are made without my permission
and for my lowercase /home directory style is agly. With this hook they will 
be denied to make such thing.

Out kernel module consist of such functions:

&lt;pre class="brush: cpp"&gt;
static int __init hook_init(void) //stufff on module init,idt hooking
static void __exit hook_exit(void) //stuff on module exit, restore idt table

asmlinkage long hooked_mkdir(const char *filename, mode_t mode) //our hook function

//how works this functions you can find in link number 1 
void *get_writable_sct(void *sct_addr)
void *get_syscall_table(void) 
&lt;/pre&gt;

Basic hooked function is:

&lt;pre class="brush:cpp"&gt;
asmlinkage long hooked_mkdir(const char *filename, mode_t mode)
{
	return mkdir(filename, mode);
}
&lt;/pre&gt;

but now we need to add check for ("Desktop","Download"). First we need some error
that will returned when some one whant to make bad directory
we will use &lt;b&gt;EACCES&lt;/b&gt; error.

here is modified functions for out task:

&lt;pre class="brush: cpp"&gt;
//hook mkfile command
asmlinkage long hooked_mkdir(const char *filename, mode_t mode)
{
	//it will disallow all files that starts with Desktop&amp;&amp;Download
	if (((strncmp(filename,"Desktop",7) == 0) &amp;&amp; (strlen(filename) == 7)) ||
		((strncmp(filename,"Download",8) == 0) &amp;&amp; (strlen(filename) == 8)))
	{
		printk(KERN_INFO "Mkdir hook\n");
		return EACCES;
	}
	return real_mkdir(filename, mode);
}
&lt;/pre&gt;

For module compiling:

&lt;code&gt;
make
&lt;/code&gt;

This is tested with kernel version 2.6.38

&lt;b&gt;Links:&lt;/b&gt;
&lt;br/&gt;[1] &lt;a href="http://codenull.net/articles/kmh_en.html"&gt;http://codenull.net/articles/kmh_en.html&lt;/a&gt;
&lt;br/&gt;[2] &lt;a href="http://www.gadgetweb.de/linux/40-how-to-hijacking-the-syscall-table-on-latest-26x-kernel-systems.html"&gt;http://www.gadgetweb.de/linux/40-how-to-hijacking-the-syscall-table-on-latest-26x-kernel-systems.html&lt;/a&gt;

</description>
			<pubDate>Thu, 21 Apr 2011 13:03:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/109</guid>
		</item>
				<item>
			<title>Sauerbraten patching and cheating</title>
			<link>http://www.main.lv/posts/view/sauerbrate-patching-and-cheating</link>
			<description>sauerbraten is open source first person shooter. Also there is multi player 
mode. I like time to time play sauerbraten. But I am not very good player.

As game source is comes with game you can view it and add some patches that 
can help get better scores in games. Usually it called cheating. 
As this features/cheats is made by my self I don&quot;t think so. But in game admins 
don&quot;t care =] about it. 

First of all this patches don&quot;t make game enjoyable for other players 
that way sooner or later you will be banned. Every one have freedom to 
be banned. 

First&lt;em&gt; "allowed"&lt;/em&gt; cheat is recoil to 0 from any weapon 

in file &lt;strong&gt;src/fpsgame/game.h&lt;/strong&gt; on line 333:   

&lt;pre class="brush: cpp"&gt;static const struct guninfo { short sound, attackdelay, damage, projspeed, part, kickamount, range; const char *name, *file; } guns[NUMGUNS] = 
 { 
 { S_PUNCH1, 250, 50, 0, 0, 0, 14, "fist", "fist" }, 
 { S_SG, 1400, 10, 0, 0, 20, 1024, "shotgun", "shotg" }, // *SGRAYS 
 { S_CG, 100, 30, 0, 0, 7, 1024, "chaingun", "chaing"}, 
 { S_RLFIRE, 800, 120, 80, 0, 10, 1024, "rocketlauncher", "rocket"}, 
 { S_RIFLE, 1500, 100, 0, 0, 30, 2048, "rifle", "rifle" }, 
 { S_FLAUNCH, 500, 75, 80, 0, 10, 1024, "grenadelauncher", "gl" }, 
 { S_PISTOL, 500, 25, 0, 0, 7, 1024, "pistol", "pistol" }, 
 { S_FLAUNCH, 200, 20, 50, PART_FIREBALL1, 1, 1024, "fireball", NULL }, 
 { S_ICEBALL, 200, 40, 30, PART_FIREBALL2, 1, 1024, "iceball", NULL }, 
 { S_SLIMEBALL, 200, 30, 160, PART_FIREBALL3, 1, 1024, "slimeball", NULL }, 
 { S_PIGR1, 250, 50, 0, 0, 1, 12, "bite", NULL }, 
 { -1, 0, 120, 0, 0, 0, 0, "barrel", NULL } 
 };&lt;/pre&gt;
changing sixths values all to 0 makes no recoil. 
but if you change recoil to 1024 you can easily jump on the sky after shut. 
Think what will see your on-line opponents? Someone if shutting from the skies. 

Not-flying rocket? Yes you can make it. 
fourth field in structure is projspeed change it for rocket launcher to 
0 and you can place your rockets on air. Bet I don&quot;t know what see others. 
Only thing with that you will get ban for team-killing because team mates 
are usually around you and they blow-up when colliding with rockets in air. 

Precision also is very nice but every one will notice that you shutting with shotgun 
and chain-gun with precision like rifle. 
In src/fpsgame/weapon.cpp on 130 line:  &lt;pre class="brush: cpp"&gt;void offsetray(const vec &amp;amp;from, const vec &amp;amp;to, int spread, float range, vec &amp;amp;dest) 
   { 
       float f = to.dist(from)*spread/1000; 
       for(;;) 
       { 
           #define RNDD rnd(101)-50 
           vec v(RNDD, RNDD, RNDD); 
           if(v.magnitude()&amp;gt;50) continue; 
           v.mul(f); 
           v.z /= 2; 
           dest = to; 
           dest.add(v); 
           vec dir = dest; 
           dir.sub(from); 
           dir.normalize(); 
           raycubepos(from, dir, dest, range, RAY_CLIPMAT|RAY_ALPHAPOLY); 
           return; 
       } 
   } &lt;/pre&gt;make&lt;pre class="brush: cpp"&gt;#define RNDD rnd(2)-1 &lt;/pre&gt;
and it will work fine.

Remember this patches is cheat/like and it is not good to play with others 
when this patches is added because they loose their enjoyment of game. Remember of FREEDOM to be banned.
</description>
			<pubDate>Sun, 13 Mar 2011 21:43:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/108</guid>
		</item>
				<item>
			<title>Python web login tips</title>
			<link>http://www.main.lv/posts/view/python-web-login-tips</link>
			<description>Some times there is need to automitize all tasks.
Like login on page download some info and go out.
There is html parsers they can do such tasks

For example it can be login script for some browser game or mail account that doesnt allow
SMTP or SMTP is not for free.

For example there is web-browser game travian an it after some time playing
it becomes very boring to play because only thing that you do it waiting
while some game events take too many time. Like when you click upgdade
something than you need to wait some hours until finish.

Now here we will make login example.
We need external libraries:
&lt;strong&gt;httplib2&lt;/strong&gt;		&lt;a href="http://code.google.com/p/httplib2/"&gt;http://code.google.com/p/httplib2/&lt;/a&gt;
&lt;strong&gt;lxml&lt;/strong&gt;			&lt;a href="http://lxml.de/"&gt;http://lxml.de/&lt;/a&gt;

First thing that we need its to get page source.

&lt;pre class="brush: py"&gt;conn = httplib2.Http("cache")
resp,cont = conn.request("http://travian.com")&lt;/pre&gt;

After we have source we look on login form
&lt;pre class="brush: html"&gt;
&lt;form method="post" name="snd" action="dorf1.php"&gt;
	&lt;input class="text" type="text" name="name" value=""&gt;
	&lt;input class="text" type="password" name="password" value="" maxlength="20"&gt;
	&lt;input type="image" value="login" name="s1" onclick="xy();" id="btn_login" class="dynamic_img"&gt;
	&lt;input type="hidden" name="w" value=""&gt;
	&lt;input type="hidden" name="login" value="1299937743"&gt;
&lt;/form&gt;
&lt;/pre&gt;
 As we see here is many inputs

As ther is only 1 form we dont check and simply take first form from array

&lt;pre class="brush: py"&gt;from lxml.html import parse,tostring,fromstring,submit_form

page = fromstring( cont )
form = page.forms[0] 
for inp in form.inputs:
	if inp.type == "text":
		inp.value = name
	if inp.type == "password":
		inp.value = password
&lt;/pre&gt;

		
Dont forget about method="post"

&lt;pre class="brush: py"&gt;headers = {&quot;Content-type&quot;: &quot;application/x-www-form-urlencoded&quot;}&lt;/pre&gt;

Now we are ready to send data and get cookie that will allow us
get inside the page

&lt;pre class="brush: py"&gt;resp , cont = self.conn.request( self.server+"/"+form.action , "POST" , body=urllib.urlencode(body) , headers=headers )&lt;/pre&gt;

Response has cookie that we need to save if would like to work with page in future

&lt;pre class="brush: py"&gt;cookie = resp[&quot;set-cookie&quot;]&lt;/pre&gt;

Also cookie is needed if whant to logout:

&lt;pre class="brush: py"&gt;headers = { &quot;Content-type&quot;: &quot;application/x-www-form-urlencoded&quot; }
headers = { &quot;Cookie&quot;: self.cookie }
body = {}
resp,cont = self.conn.request(self.server+"/logout.php", body=urllib.urlencode(body) , headers=headers)&lt;/pre&gt;

As you see now cookie is inside headers. You should allways place cookie
inside headers if whant to be loged in. Because only cookie that you get at login
says for server that you are loged in and can see what is behind the wall.

Thers is also easy way how to access DOM components
With your favorite browser you can easly get DOM path to prefered tag in HTML source.

&lt;pre class="brush: py"&gt;tmp = page.xpath("/html//div//div//div//div//p//span")&lt;/pre&gt;

You can find some tag by class name using find_class()
Or get text content from tag with text_content()

&lt;pre class="brush: py"&gt;tmp = page.xpath("/html//div//div//div//div//p//span")[2].find_class("none")[0].text_content()&lt;/pre&gt;

To make your own script that can parse and get info you need only

&lt;pre class="brush: py"&gt;reguest()
find_class()
text_content()
xpath()
fromstring()&lt;/pre&gt;

It is very easy. Now you know everything to make your first script that can login on 
you favorite page.

</description>
			<pubDate>Sat, 12 Mar 2011 16:16:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/107</guid>
		</item>
				<item>
			<title>Linux Assembler SSE add</title>
			<link>http://www.main.lv/posts/view/linux-assembler-sse-add</link>
			<description>SSe programming is whery interesting fromthat point that there are parallely 4 numbers that are porcessed.SSE has registers of size 128 bits. They can handle 4 floats.GCC C there is no default type for 128 bits and we define our ownstructure for that.&lt;pre class="brush: cpp"&gt;typedef struct xmm
{
    float a;
    float b;
    float c;
    float d;
} xmm __attribute__ ((aligned (16)));&lt;/pre&gt;structure is aligned for perfomance.to make 4byted value + 4byte valuewe need to load values:&lt;pre class="brush: asm"&gt;movaps xmm0, [eax]
movaps xmm1, [ebx]&lt;/pre&gt;and add them&lt;pre class="brush: cpp"&gt;addps xmm0,xmm1&lt;/pre&gt;after that store somewhere &lt;pre class="brush: asm"&gt;movaps [eax], xmm0&lt;/pre&gt;Final test program in C looks like:&lt;pre class="brush: cpp"&gt;typedef struct xmm
{
    float a;
    float b;
    float c;
    float d;
} xmm __attribute__ ((aligned (16)));

extern void sse_add( xmm *, xmm * );

int main( int argc, char **argv)
{
    xmm x0,x1;
    x0.a = 1.0;
    x0.b = 2.0;
    x0.c = 3.0;
    x0.d = 4.0;
    x1.a = x1.b = x1.c = x1.d = 5.0;
    
    printf("%10f %10f %10f %10f\n",x0.a,x0.b,x0.c,x0.d);
    printf("%10f %10f %10f %10f\n",x1.a,x1.b,x1.c,x1.d);
    
    sse_add( &amp;amp;x0 , &amp;amp;x1 );
    
    printf("%10f %10f %10f %10f\n",x0.a,x0.b,x0.c,x0.d);
    printf("%10f %10f %10f %10f\n",x1.a,x1.b,x1.c,x1.d);
    
    return 0;
}&lt;/pre&gt;&lt;code&gt;gcc main.c add.o -o main&lt;/code&gt; And asm example&lt;pre class="brush: asm"&gt;format ELF

section &quot;.text&quot;

public sse_add

align 4
sse_add:
    ;arguments that are pointers for 2 xmm data blocks
    x0 equ [ebp+8]
    x1 equ [ebp+12]
    
    push ebp
    mov ebp, esp
    
    mov eax, x0
    mov ebx, x1
    
    ;load in xmm0 and xmm1 values
    ;if values where not aligned than we would used other instruction
    movaps xmm0, [eax]
    movaps xmm1, [ebx]
    
    ;sum up and save inside xmm0
    addps xmm0,xmm1
    
    ;save value in first argument
    movaps [eax], xmm0
    
    pop ebp
    ret&lt;/pre&gt;&lt;b&gt;fasm add.asm add.o&lt;/b&gt;</description>
			<pubDate>Fri, 25 Feb 2011 15:30:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/106</guid>
		</item>
				<item>
			<title>Intel/Linux/BSD system</title>
			<link>http://www.main.lv/posts/view/linux-bsd-intel</link>
			<description>&lt;b&gt;FreeBSD&lt;/b&gt; assembler sample:
&lt;a href="./view/freebsd-assembler-what-you-need"&gt;Tools&lt;/a&gt;
&lt;a href="./view/freebsd-assembler-simple-programm"&gt;Simple programm&lt;/a&gt;
&lt;a href="./view/freebsd-assembler-hello-world"&gt;Hello world&lt;/a&gt;
&lt;a href="./view/freebsd-assembler-hello-world-libc"&gt;Hello world + libc&lt;/a&gt;
&lt;a href="./view/freebsd-assembler-asm-from-c"&gt;C + asm&lt;/a&gt;
&lt;a href="./view/freebsd-assembler-links"&gt;Links where is somthing useful&lt;/a&gt;
Files
&lt;a href="./view/freebsd-assembler-open-file"&gt;Open File&lt;/a&gt;

&lt;b&gt;Linux&lt;/b&gt; assembler samples:
&lt;a href="./view/linux-assembler-hello-world"&gt;Hello World&lt;/a&gt;
&lt;a href="./view/linux-assembler-gcc"&gt;gcc + asm&lt;/a&gt;
&lt;a href="./view/linux-assembler-and-g"&gt;g++ + asm&lt;/a&gt;
&lt;a href="./view/linux-assembler-open-file"&gt;Open file&lt;/a&gt;&amp;nbsp;
&lt;a href="./view/linux-assembler-make-directory"&gt;Make directory&lt;/a&gt;

&lt;b&gt;SDL assembler example&lt;/b&gt;
&lt;a href="/posts/view/linux-assembler-sdl"&gt;SDL programming&lt;/a&gt;

&lt;b&gt;FPU Topics&lt;/b&gt;
&lt;a href="./view/calculate-polynom"&gt;Calculating polinom&lt;/a&gt;

&lt;b&gt;SSE&lt;/b&gt;
&lt;a href="./linux-assembler-sse-add"&gt;SSE add&lt;/a&gt;

Programming sample from various themes.
&lt;a href="./view/basic-http-server"&gt;Basic HTTP server&lt;/a&gt;
&lt;a href="./view/fpu-catch-division-by-zero"&gt;FPU catch division by zero&lt;/a&gt;
&lt;a href="./view/bin2hex"&gt;BIn2Hex converter&lt;/a&gt;
&lt;a href="./view/reprbin-represent-binary-files-in-different-formats"&gt;ReprBin&lt;/a&gt;
&lt;a href="./view/arp-analyzer"&gt;Arp Packet Analyzer&lt;/a&gt;
&lt;a href="./view/linux-keyboard-led"&gt;Keyboard LED flush&lt;/a&gt;
&lt;a href="./view/linux-pc-speaker"&gt;PC speaker&lt;/a&gt;
&lt;a href="./view/xlib-hello-world"&gt;Xlib, hello world&lt;/a&gt;

&lt;b&gt;Interesting themes:&lt;/b&gt;
&lt;a href="./view/linux-format-string-attack-1"&gt;Linux Format String Attack&lt;/a&gt;
&lt;a href="./view/elf-rewrite-function"&gt;ELF rewrite function&lt;/a&gt;
&lt;a href="./view/linux-assembler-scripting-language"&gt;Assembler scripting language&lt;/a&gt;
&lt;a href="./view/elf-text-section"&gt;ELF text section&lt;/a&gt;
&lt;a href="./view/linux-shellcode-1"&gt;Linux ShellCode 1&lt;/a&gt;
&lt;a href="./view/linux-local-descriptor-table"&gt;Local Descriptor Table&lt;/a&gt;
&lt;a href="./view/cvs-2010-1160-nano-bug"&gt;Nano bug (CVS 2010-1160)&lt;/a&gt;
&lt;a href="./view/hooking-interrupt-descriptor-table"&gt;Hooking interrupt descriptor table &lt;/a&gt;

&lt;b&gt;Antidebug&lt;/b&gt;
&lt;a href="./view/linux-antidebug-1"&gt;Antidebug 1&lt;/a&gt;
&lt;a href="./view/linux-antidebug-2"&gt;Antidebug 2&lt;/a&gt;
&lt;a href="./view/linux-antidebug-3"&gt;Antidebug 3&lt;/a&gt;

</description>
			<pubDate>Fri, 18 Feb 2011 11:28:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/105</guid>
		</item>
				<item>
			<title>ReprBin - represent binary files in different formats</title>
			<link>http://www.main.lv/posts/view/reprbin-represent-binary-files-in-different-formats</link>
			<description>This is bin2hex style project. It converts binary to other formats. Its purpose is to use with combination with assembler or uC.Code is public and on Evil Google::Code page
&lt;a href="http://code.google.com/p/represent-binary-file/"&gt;Google storage&lt;/a&gt;

&lt;b&gt;SVN line:
&lt;/b&gt;&lt;code&gt;
svn checkout http://represent-binary-file.googlecode.com/svn/trunk/ represent-binary-file-read-only
&lt;/code&gt;
</description>
			<pubDate>Thu, 10 Feb 2011 22:12:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/104</guid>
		</item>
				<item>
			<title>Linux assembler scripting language</title>
			<link>http://www.main.lv/posts/view/linux-assembler-scripting-language</link>
			<description>This is small interpretr in asm.
It works with small language thats can make simple things
All that you need to know about language
is this symbols &lt;i&gt;"ABCDI$@"&lt;/i&gt;

&lt;b&gt;ABCD&lt;/b&gt; is used with parametr.
&lt;b&gt;I&lt;/b&gt; without param
&lt;b&gt;$@&lt;/b&gt; is params

&lt;br&gt;&lt;u&gt;ABCD - is like assembler command &lt;i&gt;mov&lt;/i&gt; where symbol is register name&lt;/u&gt;
&lt;br&gt;&lt;b&gt;A0&lt;/b&gt; is mov eax, 0
&lt;br&gt;&lt;b&gt;B9&lt;/b&gt; is mov ebx, 9
&lt;br&gt;only one number is supported. Number range after &lt;b&gt;ABCD&lt;/b&gt; suposed to be &lt;b&gt;0...9&lt;/b&gt;.
But you can add any other symbol only not &lt;b&gt;@&lt;/b&gt; or &lt;b&gt;$&lt;/b&gt;. Look inside ascii table
char &quot;0&quot; is 0 and other goes relativly from it. number &lt;b&gt;&quot;~&quot;&lt;/b&gt; is &lt;b&gt;&quot;~&quot;-&quot;0&quot;=127-48=79&lt;/b&gt;
&lt;br&gt;

&lt;br/&gt;&lt;u&gt;I - is interupt number 80h&lt;/u&gt;
&lt;br&gt;

&lt;br/&gt;&lt;u&gt;$@ - is variables from stack&lt;/u&gt;
&lt;br/&gt;&lt;b&gt;@&lt;/b&gt; - uses current varaible from stack and stack pointer goes to next stack value
&lt;br/&gt;&lt;b&gt;$&lt;/b&gt; - uses current stack value and dont change stack pointer position
&lt;br&gt;

&lt;br/&gt;Thats all.

&lt;br&gt;
&lt;br/&gt;Now we can make our first script and run it.

&lt;br&gt;
&lt;br/&gt;There is 2 thing that you should know.
Script is converted to assembler commands and copyed in memory position.

&lt;br&gt;
&lt;br/&gt;Every file has hiw own purpose and all they seperated for easy to use

&lt;br&gt;
&lt;br/&gt;&lt;i&gt;&quot;script.inc&quot;&lt;/i&gt; you scipt inside it
&lt;br/&gt;&lt;i&gt;&quot;stack_table.inc&quot;&lt;/i&gt; configure stack for use
&lt;br/&gt;&lt;i&gt;&quot;variables.inc&quot;&lt;/i&gt; define variables
&lt;br/&gt;&lt;i&gt;&quot;exec.inc&quot;&lt;/i&gt; memory region wher script interpreted commands will copyed

&lt;br&gt;
&lt;br&gt;
&lt;b&gt;Example 1:&lt;/b&gt;
&lt;br&gt;Now first example script:
&lt;pre class="brush:asm"&gt;script db &quot;A1B0I&quot;&lt;/pre&gt;

mov eax, 1 ;you can look  this variable inside 
&lt;pre class="brush:cpp"&gt;
#include &amp;lt; asm/unistd.h&amp;gt; 
&lt;/pre&gt; 
or in &lt;a href="http://bluemaster.iu.hio.no/edu/dark/lin-asm/syscalls.html"&gt;http://bluemaster.iu.hio.no/edu/dark/lin-asm/syscalls.html&lt;/a&gt;
&lt;pre class="brush:asm"&gt;
mov ebx, 0
int 80h
&lt;/pre&gt;

&lt;br&gt;
it is command exit. stack can be empty.

&lt;br/&gt;&lt;b&gt;Example2:&lt;/b&gt;

&lt;br&gt;Now we can make hello_world. 

&lt;pre class="brush:asm"&gt;
script db &quot;A4B1C@D@IA1B0I&quot;
&lt;/pre&gt;

It is 

&lt;pre class="brush:asm"&gt;
mov eax, 4
mov ebx, 1
mov ecx, buffer_msg; stack value 0
mov edx, buffer_len; stack value 1
int 80h

mov eax, 1
mov ebx, 0
int 80
&lt;/pre&gt;

in C it would be

&lt;pre class="brush:cpp"&gt;
write(1,buffer_msg,buffer_len)
exit(0);
&lt;/pre&gt;

Here is example how corresponds asm to C code http://www.main.lv/posts/view/linux-assembler-open-file.
Ther is used stack in &quot;stack_table.inc&quot;:

&lt;pre class="brush:asm"&gt;
stack_table:
	dd buffer_msg ;variable 0
	dd buffer_len ;variable 1
&lt;/pre&gt;
	
and in &quot;variables.inc&quot; we define this variables:

&lt;pre class="brush:asm"&gt;
buffer_msg db "Hello world",10	;with newline
buffer_len = $-buffer_msg	;using fasm mega feature to detect size
&lt;/pre&gt;

we can count equvialent asm commands and there is 8 of them
it means add 8 lines in &quot;exec.inc&quot;

&lt;pre class="brush:asm"&gt;
	db 0x90,0x90,0x90,0x90,0x90
	db 0x90,0x90,0x90,0x90,0x90
	db 0x90,0x90,0x90,0x90,0x90
	db 0x90,0x90,0x90,0x90,0x90
	db 0x90,0x90,0x90,0x90,0x90
	db 0x90,0x90,0x90,0x90,0x90
	db 0x90,0x90,0x90,0x90,0x90
	db 0x90,0x90,0x90,0x90,0x90
&lt;/pre&gt;
type make and everything works =]. WooHoo small interpretd language is made and it fits in 417 bytes.</description>
			<pubDate>Sat, 22 Jan 2011 17:06:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/103</guid>
		</item>
				<item>
			<title>FPU catch division by zero</title>
			<link>http://www.main.lv/posts/view/fpu-catch-division-by-zero</link>
			<description>There can occure some problems in C onw of them is divison on zero. For this we setup system exception handler or signal handler.&amp;nbsp; When is division on zero it works.Also for return in main function there is used &lt;i&gt;setjmp&lt;/i&gt; and &lt;i&gt;longjmp&lt;/i&gt;&lt;pre class="brush: cpp"&gt;void set_exception_handler()
{
	int err;
	fenv.__control_word &amp;amp;= ~FE_ALL_EXCEPT;
	fenv.__cs_selector &amp;amp;= ~FE_ALL_EXCEPT &amp;lt;&amp;lt; 7;
	fesetenv( &amp;amp;fenv );	
	
	sa.sa_sigaction = &amp;amp;exception_handler;
	sa.sa_flags = SA_SIGINFO;
	err = sigaction( SIGFPE, &amp;amp;sa, NULL );
	if (err != 0)
		printf("Cannot set FloatingPoint exception handler\n");
	else
		printf("[OK] SIGFPE is set\n");
}

void exception_handler(int i, siginfo_t *s, void *v )
{
	if (s-&amp;gt;si_signo == SIGFPE)
	{
		printf("[SIGFPE] SIGFPE Occure\n");
		printf("[SIGFPE] Error number: %d\n", s-&amp;gt;si_errno);
		printf("[SIGFPE] Signal code: %d\n", s-&amp;gt;si_code);
		switch (s-&amp;gt;si_code)
		{
			case FPE_INTDIV:
				printf("[SIGFPE] Divison by 0\n");
				longjmp( jmp , 1 );
				break;
		}
	}
	abort();
}&lt;/pre&gt;
Compilation is easy: 
&lt;code&gt;gcc sigfpe.c -o sigfpe -lm&lt;/code&gt; 
Now it will no so big problem when some error occur to properly exit or make some checks.</description>
			<pubDate>Sun, 16 Jan 2011 22:52:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/102</guid>
		</item>
				<item>
			<title>Skype Fallow Mood Text </title>
			<link>http://www.main.lv/posts/view/skype-fallow-mood-text</link>
			<description>There are dozzen mood texts that have your skype contacts. But if someone from contact changte it. I have writen this script for following changes in mood textes. It creates sqlite data base, records user name, hash of mood text and mood text. If some of contacts have changed mood text then it show it in output.
&lt;b&gt;Run:&lt;/b&gt;
&lt;code&gt;./checkMood.py&lt;/code&gt;

and everything works.</description>
			<pubDate>Fri, 3 Dec 2010 00:52:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/101</guid>
		</item>
				<item>
			<title>Skype Rich Mood Text Animations</title>
			<link>http://www.main.lv/posts/view/skype-rich-mood-text-animations</link>
			<description>Mood text in skype is simple and not very interactive. Trought skype api there can be done some animations.First step was to test set mood text&amp;nbsp; trought api.Here is script that directly sends to skype Skype commnd for setting rich mood text. Linux dont support latest skype Protocol 7 (API version 3.0) but on Win there everything words ok. Here you type in commandline 
&lt;code&gt;./setrichmood.py "New mood"&lt;/code&gt;
and rich mood text changed
&lt;pre class="brush: py"&gt;import sys
import os
import Skype4Py

skype = Skype4Py.Skype()
skype.Attach()

if len(sys.argv) == 2:
	if os.path.exists( sys.argv[1] ):
		f = open( sys.argv[1] , "r" )
		s = unicode(f.read())
		f.close()
		c = skype.Command( "SET PROFILE RICH_MOOD_TEXT "+s )
		skype.SendCommand( c )
	else:
		s = unicode(sys.argv[1])
		c = skype.Command( "SET PROFILE RICH_MOOD_TEXT "+s )
		skype.SendCommand( c )&lt;/pre&gt;
Why I it call rich mood text? because it support some xml like commands.from skype api there is such commands
&lt;pre class="brush: py"&gt;Example:

//------------------------------------------------------------------
// For purpose of bit conservation we omit feedback notifications
SET PROFILE RICH_MOOD_TEXT Smiley: &lt;ss type="smile"&gt;:-)&lt;/ss&gt;
SET PROFILE RICH_MOOD_TEXT &lt;font color="#ff0010"&gt;Red text&lt;/font&gt;
SET PROFILE RICH_MOOD_TEXT &lt;blink&gt;Blinking text&lt;/blink&gt;
SET PROFILE RICH_MOOD_TEXT &lt;b&gt;Bold text&lt;/b&gt;
SET PROFILE RICH_MOOD_TEXT &lt;i&gt;Italics&lt;/i&gt;
SET PROFILE RICH_MOOD_TEXT &lt;u&gt;Underlined&lt;/u&gt;
SET PROFILE RICH_MOOD_TEXT First lineSecond lineThird line

&lt;ss type="smile"&gt;&lt;/ss&gt; also accepts following smileys:

    * smile, sad, laugh, cool, surprised, wink, cry, sweat, speechless, kiss, tongueout, blush, wonder, sleepy, snooze, dull, inlove, talk, yawn, puke, doh, angry, wasntme, party, worry, mmm, nerdy, lipssealed, hi, call, devil, angel, envy, wait, hug, makeup, giggle, clap, think, bow, rofl, whew, happy, smirk, nod, shake, punch, emo, no, yes, handshake, skype, heart, brokenheart, mail, flower, rain, sun, time, music, movie, phone, coffee, pizza, cash, muscle, beer, drink, dance, ninja, star, mooning, finger, bandit, smoke, toivo, rock, headbang, poolparty, swear, bug, fubar, tmi. &lt;/pre&gt;I have tryed use them one inside other but it doesnt worked.How there can be made animations? Here is very simple example that reads from file linesand after time delay shows lines.

&lt;code&gt;./moodanime.py anime.xml&lt;/code&gt;
Here is new peace of script:
&lt;pre class="brush: py"&gt;import sys
import os
import Skype4Py
import time

skype = Skype4Py.Skype()
skype.Attach()

s = []
if os.path.exists( sys.argv[1] ):
	f = open( sys.argv[1] , "r" )
	for line in f:
		s.append(line)
	f.close()
	
while True:
	for frame in s:
		c = skype.Command( "SET PROFILE RICH_MOOD_TEXT "+frame )
		skype.SendCommand( c )
		time.sleep( 1 )
&lt;/pre&gt;
as example file can be:
&lt;pre class="brush: asm"&gt;____Bonanza____
___#Bonanza#___
__##Bonanza##__
_###Bonanza###_
####Bonanza####
_###Bonanza###_
__##Bonanza##__
___#Bonanza#___
&lt;/pre&gt;
And now everything works fine. I have tested this scipts with python2.7 and on ArchLinux. If there is some problems try static or dynamic skype from skype download page</description>
			<pubDate>Fri, 3 Dec 2010 00:12:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/100</guid>
		</item>
				<item>
			<title>Scan memory for variable</title>
			<link>http://www.main.lv/posts/view/scan-memory-for-variable</link>
			<description>&lt;br/&gt;Somedays ago I was playing one game. And as I not so often playing
games. I would like to change some variables in memory like ammo quantity
or health. May be it is not very interesting to play game with "cheating"
but there is much more interest to play with program.

&lt;br/&gt;In such play can help scanmem

&lt;br/&gt;Here is example of programm that will help us to lern how to use scanmem:

&lt;pre class="brush: cpp"&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

unsigned int secret_dw = 1000; //variable to search
unsigned int tmp;//for input variable


int main()
{
	int i;
	while ( secret_dw != -1 )
	{
		scanf("%u",&amp;tmp);
		printf("secret_dw was %u \n",secret_dw);
		secret_dw = tmp;
		tmp = 0; // This is to prevent from detecting tmp variable position
	}
	printf("\bExit\n");
	return 0;
}
&lt;/pre&gt;

here only two variables one secret_dw for value that we will search
and second one tmp to save input. Also tmp will zeroed if not then we will find tmp and secret_dw.

compile example with

&lt;br/&gt;&lt;code&gt;make&lt;/code&gt;

&lt;br/&gt;and run

&lt;br/&gt;&lt;code&gt;./example&lt;/code&gt;

&lt;br/&gt;And in paralel run

&lt;pre class="brush: text"&gt;
$ scanmem `pidof example`
scanmem version 0.11
Copyright (C) 2009,2010 Tavis Ormandy, Eli Dupree, WANG Lu
Copyright (C) 2006-2009 Tavis Ormandy
scanmem comes with ABSOLUTELY NO WARRANTY; for details type `show warranty&quot;.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show copying&quot; for details.

info: maps file located at /proc/1801/maps opened.
info: 5 suitable regions found.
Please enter current value, or "help" for other commands.
&lt;/pre&gt;

As we searching 4 byte value of uint we defining it by setting up option

&lt;pre class="brush: text"&gt;0&gt; option scan_data_type int32&lt;/pre&gt;

Now we ready to start our game. At begining we know our secret_dw value it is 1000 but we will not use it.
Type 1 in example

&lt;pre class="brush: text"&gt;
1
&lt;/pre&gt;
secret_dw was 1000 

in scanmem

&lt;pre class="brush: text"&gt;
0&gt; 1
info: 01/05 searching  0x8049000 -  0x804a000...........ok
info: 02/05 searching 0xb763d000 - 0xb763e000...........ok
info: 03/05 searching 0xb7787000 - 0xb778a000...........ok
info: 04/05 searching 0xb77a7000 - 0xb77a9000...........ok
info: 05/05 searching 0xbf9d4000 - 0xbf9f5000...........ok
info: we currently have 58 matches.
&lt;/pre&gt;

As we can see 58 matches. WooHoo. Now type &quot;1000&quot;in example

&lt;pre class="brush: text"&gt;
1000
&lt;/pre&gt;

secret_dw was 1


in scanmem

&lt;pre class="brush: text"&gt;
58&gt; 1000
..........info: we currently have 2 matches.
&lt;/pre&gt;

only 2 now

scanmem has also many built in commands you can see them when type help.
One of them is &quot;list&quot;. Use it.

&lt;pre class="brush: text"&gt;
2&gt; list
[ 0]            0x8049680, 1000, [I32 ]
[ 1]           0xbf9f2dd8, 1000, [I32 ]
&lt;/pre&gt;

Here is list of matched variables. Number,address,value,size. By adress we see that
our variable is with number 0. 

&lt;pre class="brush: text"&gt;
2&gt; set 0=999
info: setting *0x8049680 to 0x3e7...
2&gt; list
[ 0]            0x8049680, 1000, [I32 ]
[ 1]           0xbf9f2dd8, 1000, [I32 ]
&lt;/pre&gt;

Now our variable is with value 999. When you type list it may be little
bit confusing that values is the same. Go in example

&lt;pre class="brush: text"&gt;
12
&lt;/pre&gt;

secret_dw was 999


Yes. We have changed our variable. Our goal is completed.

Scanmem webpage &lt;a href="http://taviso.decsystem.org/scanmem.html"&gt;http://taviso.decsystem.org/scanmem.html&lt;/a&gt;

Source contains programm outputs and example code.

</description>
			<pubDate>Thu, 18 Nov 2010 15:35:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/99</guid>
		</item>
				<item>
			<title>Numerical integration. Simpson method</title>
			<link>http://www.main.lv/posts/view/numerical-integration-simpson-method</link>
			<description>Simpsons method not to compilcated to calculate numerical value of integral.Main point of this Simpson rules is insert values in to parabole 
\[ y(x) = Ax^2+Bx+C \]
this parabole going trought points x_0, x_0+h, x_0+2*h we doubling number of points where we will calculate by 2 and this gives us enought points for this formula. 
\[ \int_{x_0}^{x_0+2h} y(x)= \frac{h}{3}(y_0+4y_1+y_2) \]

&lt;pre class="brush: cpp"&gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;math.h&amp;gt;

#define TYPE float
#define NUMBER 10

TYPE integ_simpson( TYPE f(TYPE) , TYPE, TYPE, int);
TYPE fun( TYPE );

int main()
{
	printf("Result: %f\n",integ_simpson( &amp;amp;fun , 0.0 , 1.0 , NUMBER ));
	return 0;
}

TYPE integ_simpson( TYPE f(TYPE) , TYPE a, TYPE b, int n)
{
	int i;
	n=2*n;
	TYPE sum=f(a),h=(b-a)/n;
	for (i=1;i&amp;lt;=n-1;i+=2) sum += 4*f(a+h*i);
	for (i=2;i&amp;lt;=n-2;i+=2) sum += 2*f(a+h*i);
	sum += f(b);
	return h * sum / 3;
}

TYPE fun( TYPE x )
{
	return 1/(1+pow(x,2.0));
}

&lt;/pre&gt;</description>
			<pubDate>Tue, 9 Nov 2010 21:16:00 +0200</pubDate>
			<guid>http://www.main.lv/posts/view/98</guid>
		</item>
			</channel>
</rss>

