1
C / C++ /C# / Re: ASCII Matrix - C code
« on: October 18, 2006 »
Really nice work there!
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

/**
* PE header section permision modifier.
* Will set the section to READ/WRITE/EXECUTE
* usable if you wrote a selfmodifying code segment in asm, and wishes
* to use that outputed asm obj in you c program.
* - Add this in a .bat file after you compiled your c project into a exe.
* (only PE format supported)
*
* code by saida^titan^rebels
* 2006-10-06
* saida@lava.nu
* http://saida.lava.nu
*/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
void showReadError()
{
printf("*** Could not read from file - is it a valid PE?\n");
}
void showWriteError()
{
printf("*** Could not write to file - in use - or disk full?\n");
}
void showUsage()
{
printf("Usage: section_perm.exe <filename> <section name to alter>\n");
}
void showOpenError()
{
printf("Could not open input file\n");
}
int main(int argc, char *argv[])
{
//section to modify
IMAGE_SECTION_HEADER modSection;
IMAGE_DOS_HEADER headerDos;
IMAGE_FILE_HEADER headerPE;
int i;
HFILE hFile;
unsigned char cBuf [5];
long sectionStartAddr;
if (argc<3) {
showUsage();
return 0;
}
// open file
hFile = _lopen(argv[1], OF_READWRITE);
if (hFile==0) {
showOpenError();
return 0;
}
//write some text on start.
printf("--------------------------------------------------------------------------------");
printf("*** Starting section_perm.exe\n");
printf("*** Opening file: \"%s\" and searching for \"%s\"\n", argv[1], argv[2]);
//read dos header and check we read the correct amount of bytes
if (_lread(hFile, &headerDos, sizeof(IMAGE_DOS_HEADER))!=sizeof(IMAGE_DOS_HEADER)) {
printf("DOS_HEADER: ");
showReadError();
return 0;
}
//move filepointer to PE header start...
_llseek(hFile, headerDos.e_lfanew, FILE_BEGIN);
// Read PE signature and check we read the correct amount of bytes
memset(&cBuf, 0, 5);
if (_lread(hFile, &cBuf, 4)!=4) {
printf("PE_HEADER_SIGNATURE: ");
showReadError();
return 0;
}
// check read data to make sure this is a PE file
if (strcmp("PE", cBuf)!=0) {
printf("PE signature not found. This is not av valid PE file!");
return 0;
}
//read PE header data
if (_lread(hFile, &headerPE, sizeof(IMAGE_FILE_HEADER))!=sizeof(IMAGE_FILE_HEADER)) {
printf("PE_HEADER: ");
showReadError();
return 0;
}
//move pass the optional header
_llseek(hFile, headerPE.SizeOfOptionalHeader, FILE_CURRENT );
//now, we are on the first section address.
sectionStartAddr = headerDos.e_lfanew + sizeof(IMAGE_FILE_HEADER) + headerPE.SizeOfOptionalHeader + 4;
// step through all sections and search for the section the user entered.
for (i=0; i < headerPE.NumberOfSections; i++) {
if (_lread(hFile, &modSection, sizeof(IMAGE_SECTION_HEADER))!=sizeof(IMAGE_SECTION_HEADER)) {
printf("SECTION: ");
showReadError();
break;
}
// print current section
printf("%d: %s\n",i, modSection.Name);
if (strcmp(argv[2], modSection.Name)==0) {
modSection.Characteristics = modSection.Characteristics | IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_MEM_READ | IMAGE_SCN_MEM_WRITE;
_llseek(hFile, sectionStartAddr + (i * sizeof(IMAGE_SECTION_HEADER)), FILE_BEGIN);
if (_lwrite(hFile, &modSection, sizeof(IMAGE_SECTION_HEADER))==HFILE_ERROR) {
printf("SECTION: ");
showWriteError();
} else {
printf("Section was found and written to.\n" );
}
_lclose(hFile);
return 0;
}
}
// close the exe file
_lclose(hFile);
printf("Section was not found in file. Did you forget the leading dot?\n");
return 0;
}
.data
szMyString db "param1",0 ;7 bytes
.code
push offset szMyString ; 5 bytes
call myFunctionWithOneParameterPushed ; 5 bytes
.code
call _ff ; 5 bytes
szMyString db "param1",0 ;7 bytes
_ff:
call myFunctionWithOneParameterPushed ; 5 bytes



a db "C", 0, "L", 0, "Y", 0, "D", 0, "E", 0 ,0,0

.586p
.MODEL flat, stdcall
LoadLibraryA PROTO :DWORD
GetProcAddress PROTO :DWORD, :DWORD
CoInitialize PROTO :DWORD
CoCreateInstance PROTO :DWORD, :DWORD, :DWORD, :DWORD, :DWORD
option casemap:none
.code
align 1
speakIt proc
pushad
xor edi, edi
mov esi, offset e
cmp dword ptr [esi], 0
jne @f
; invoke CoInitialize, null
push edi
call CoInitialize
; invoke CoCreateInstance, addr b, 0, 7, addr d, addr e
push esi
push offset d
push 00000007h
push edi
push offset b
call CoCreateInstance
@@:
; invoke sapiFunction, asdasd, offsetToText, 1, 0
push edi
inc edi
push edi
push offset a
mov edx, [esi]
push edx
mov edx, [edx]
call dword ptr [edx+50h] ; speak!!!
popad
ret
speakIt endp
;blah1 CLSID and such...
b db 077h, 093h, 074h, 096h, 091h, 033h, 0d2h, 011h, 09eh, 0e3h, 000h, 0c0h, 04fh, 079h, 073h, 096h
;blah2 CLSID and such...
d db 074h, 0dfh, 044h, 06ch, 0b9h, 072h, 092h, 049h, 0a1h, 0ech, 0efh, 099h, 06eh, 004h, 022h, 0d4h
; stuff to say: uit says "music is chip-on.com"
a db "m",0,"u",0,"s",0,"i",0,"c",0," ",0,"i",0,"s",0," ",0;,;0,"s",0
db "c",0,"h",0,"i",0,"p",0," ",0,"o",0,"n",0,".",0,"c",0,"o",0,"m",0,0,0,0;
;szOle32
e dd 0
end

/
/