This repository has been archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBuffer.c
190 lines (152 loc) · 4.36 KB
/
Buffer.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
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*!
*
* PostEx
*
* GuidePoint Security LLC
*
* Threat and Attack Simulation Team
*
!*/
#include "Common.h"
typedef struct
{
D_API( RtlReAllocateHeap );
D_API( RtlAllocateHeap );
D_API( RtlFreeHeap );
D_API( _vsnprintf );
} API ;
/*!
*
* Purpose:
*
* Creates a "buffer" object to use. The "buffer"
* pointer of the structure points to the payload
* being appended to.
*
!*/
PBUFFER BufferCreate( VOID )
{
API Api;
HANDLE Ntl = NULL;
PBUFFER Buf = NULL;
/* Zero out stack structures */
RtlSecureZeroMemory( &Api, sizeof( Api ) );
Ntl = LoadLibraryA( "ntdll.dll" );
if ( Ntl != NULL ) {
Api.RtlReAllocateHeap = C_PTR( GetProcAddress( Ntl, "RtlReAllocateHeap" ) );
Api.RtlAllocateHeap = C_PTR( GetProcAddress( Ntl, "RtlAllocateHeap" ) );
Api.RtlFreeHeap = C_PTR( GetProcAddress( Ntl, "RtlFreeHeap" ) );
Api._vsnprintf = C_PTR( GetProcAddress( Ntl, "_vsnprintf" ) );
/* Create buffer Object */
if ( ( Buf = Api.RtlAllocateHeap( NtCurrentPeb()->ProcessHeap, HEAP_ZERO_MEMORY, sizeof( BUFFER ) ) ) != NULL ) {
};
FreeLibrary( Ntl );
};
/* Zero out stack structures */
RtlSecureZeroMemory( &Api, sizeof( Api ) );
/* Return */
return Buf;
};
/*!
*
* Purpose:
*
* Extends a buffer to the specified length
*
!*/
BOOL BufferExtend( _In_ PBUFFER Buffer, ULONG Length )
{
API Api;
BUFFER Buf;
INT Len = 0;
BOOL Ret = FALSE;
HANDLE Ntl = NULL;
/* Zero out stack structures */
RtlSecureZeroMemory( &Api, sizeof( Api ) );
RtlSecureZeroMemory( &Buf, sizeof( Buf ) );
Ntl = LoadLibraryA( "ntdll.dll" );
if ( Ntl != NULL ) {
Api.RtlReAllocateHeap = C_PTR( GetProcAddress( Ntl, "RtlReAllocateHeap" ) );
Api.RtlAllocateHeap = C_PTR( GetProcAddress( Ntl, "RtlAllocateHeap" ) );
Api.RtlFreeHeap = C_PTR( GetProcAddress( Ntl, "RtlFreeHeap" ) );
Api._vsnprintf = C_PTR( GetProcAddress( Ntl, "_vsnprintf" ) );
if ( Buffer->Buffer != NULL ) {
Buf.Buffer = Api.RtlReAllocateHeap( NtCurrentPeb()->ProcessHeap, HEAP_ZERO_MEMORY, Buffer->Buffer, Buffer->Length + Length );
} else {
Buf.Buffer = Api.RtlAllocateHeap( NtCurrentPeb()->ProcessHeap, HEAP_ZERO_MEMORY, Buffer->Length + Length );
};
if ( Buf.Buffer != NULL ) {
/* Set the new pointer */
Buffer->Buffer = C_PTR( Buf.Buffer );
/* Set the new length */
Buffer->Length = Buffer->Length + Length;
/* Status */
Ret = TRUE;
};
/* Dereference */
FreeLibrary( Ntl );
};
/* Zero out stack structures */
RtlSecureZeroMemory( &Api, sizeof( Api ) );
RtlSecureZeroMemory( &Buf, sizeof( Buf ) );
/* Status */
return Ret;
};
/*!
*
* Purpose:
*
* Appends a formated string to a buffer.
*
!*/
BOOL BufferPrintf( _In_ PBUFFER Buffer, _In_ PCHAR Format, ... )
{
API Api;
BUFFER Buf;
va_list Lst;
INT Len = 0;
BOOL Ret = FALSE;
HANDLE Ntl = NULL;
/* Zero out stack structures */
RtlSecureZeroMemory( &Api, sizeof( Api ) );
RtlSecureZeroMemory( &Buf, sizeof( Buf ) );
RtlSecureZeroMemory( &Lst, sizeof( Lst ) );
/* Reference ntdll.dll */
Ntl = LoadLibraryA( "ntdll.dll" );
if ( Ntl != NULL ) {
Api.RtlReAllocateHeap = C_PTR( GetProcAddress( Ntl, "RtlReAllocateHeap" ) );
Api.RtlAllocateHeap = C_PTR( GetProcAddress( Ntl, "RtlAllocateHeap" ) );
Api.RtlFreeHeap = C_PTR( GetProcAddress( Ntl, "RtlFreeHeap" ) );
Api._vsnprintf = C_PTR( GetProcAddress( Ntl, "_vsnprintf" ) );
/* Get length of buffer */
va_start( Lst, Format );
Len = Api._vsnprintf( NULL, 0, Format, Lst );
va_end( Lst );
/* Create a buffer */
if ( Buffer->Buffer != NULL ) {
Buf.Buffer = Api.RtlReAllocateHeap( NtCurrentPeb()->ProcessHeap, HEAP_ZERO_MEMORY, Buffer->Buffer, Buffer->Length + Len );
} else {
Buf.Buffer = Api.RtlAllocateHeap( NtCurrentPeb()->ProcessHeap, HEAP_ZERO_MEMORY, Buffer->Length + Len );
};
if ( Buf.Buffer != NULL ) {
/* Set new pointer */
Buffer->Buffer = C_PTR( Buf.Buffer );
/* Copy over our buffer */
va_start( Lst, Format );
Len = Api._vsnprintf( C_PTR( U_PTR( Buffer->Buffer ) + Buffer->Length ), Len, Format, Lst );
va_end( Lst );
/* Set new length */
Buffer->Length = Buffer->Length + Len;
/* Status */
Ret = TRUE;
};
/* Dereference */
FreeLibrary( Ntl );
};
/* Zero out stack structures */
RtlSecureZeroMemory( &Api, sizeof( Api ) );
RtlSecureZeroMemory( &Buf, sizeof( Buf ) );
RtlSecureZeroMemory( &Lst, sizeof( Lst ) );
/* Did our allocation succeed? */
return Ret;
};