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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include <windows.h>
#include <net/lan.h>
#include <iostream>
#include <string>
#include <ddk/ntddk.h>
#include <rosrtl/string.h>

using std::string;
using std::cin;
using std::cout;
using std::cerr;
using std::endl;

void display_row( char *data, int off, int len ) {
    int i;

    printf( "%08x:", off );
    for( i = off; i < len && i < off + 16; i++ ) {
	printf( " %02x", data[i] & 0xff );
    }

    for( ; i < off + 16; i++ )
	printf("   ");

    printf( " -- " );

    for( i = off; i < len && i < off + 16; i++ ) {
	printf( "%c", (data[i] >= ' ' && data[i] <= '~') ? data[i] : '.' );
    }

    printf( "\n" );
}

void display_buffer( char *Packet, int ReadLen ) {
    UINT PktLen;
    for( PktLen = 0; PktLen < ReadLen; PktLen += 16 )
	display_row( Packet, PktLen, ReadLen );
}

int byte_till_end( char *Packet, int PktLen ) {
    int byte;
    std::string word;

    cin >> word;
    while( word != "end" ) {
	byte = strtoul( (string("0x") + word).c_str(), 0, 0 );
	fprintf( stderr, "Byte[%d]: %x\n", PktLen, byte & 0xff );
	Packet[PktLen++] = byte;
	cin >> word;
    }

    return PktLen;
}

/* Ethernet types. We swap constants so we can compare values at runtime
   without swapping them there */
#define ETYPE_IPv4 WH2N(0x0800)
#define ETYPE_IPv6 WH2N(0x86DD)
#define ETYPE_ARP  WH2N(0x0806)

extern "C"
NTSTATUS NTAPI NtCreateFile(
    PHANDLE FileHandle,
    ACCESS_MASK DesiredAccess,
    POBJECT_ATTRIBUTES ObjectAttributes,
    PIO_STATUS_BLOCK IoStatusBlock,
    PLARGE_INTEGER AllocationSize,
    ULONG FileAttributes,
    ULONG ShareAccess,
    ULONG CreateDisposition,
    ULONG CreateOptions,
    PVOID EaBuffer,
    ULONG EaLength);

int main( int argc, char **argv ) {
    string word;
    HANDLE LanFile;
    OBJECT_ATTRIBUTES Attributes;
    UNICODE_STRING LanDevice;
    IO_STATUS_BLOCK Iosb;
    HANDLE Event;
    PFILE_FULL_EA_INFORMATION EaBuffer;
    NTSTATUS Status;
    DWORD On = 1, PktLen;
    CHAR Packet[1600];
    PLAN_PACKET_HEADER Hdr = (PLAN_PACKET_HEADER)Packet;
    PLAN_ADDRESS Addr = (PLAN_ADDRESS)Packet;
    USHORT TypesToListen[] = { ETYPE_IPv4, ETYPE_IPv6, ETYPE_ARP };
    UINT EaLength = LAN_EA_INFO_SIZE(sizeof(TypesToListen)/sizeof(USHORT));

    Status = NtCreateEvent(&Event,<--- Status is assigned
			   EVENT_ALL_ACCESS,
			   NULL,
			   0,
			   0 );

    RtlInitUnicodeString( &LanDevice, L"\\Device\\Lan" );

    InitializeObjectAttributes( &Attributes,
				&LanDevice,
				OBJ_CASE_INSENSITIVE,
				NULL,
				NULL );

    EaBuffer = (PFILE_FULL_EA_INFORMATION)calloc( EaLength, 1 );
    LAN_FILL_EA_INFO(EaBuffer,sizeof(TypesToListen)/sizeof(USHORT),
		     TypesToListen);

    Status = ZwCreateFile( &LanFile,<--- Status is overwritten
			   SYNCHRONIZE | GENERIC_READ | GENERIC_WRITE |
			   GENERIC_EXECUTE,
			   &Attributes,
			   &Iosb,
			   NULL,
			   FILE_ATTRIBUTE_NORMAL,
			   FILE_SHARE_READ | FILE_SHARE_WRITE,
			   FILE_OPEN_IF,
			   FILE_SYNCHRONOUS_IO_NONALERT,
			   EaBuffer,
			   EaLength );

    if( !NT_SUCCESS(Status) ) {
	cerr << "Could not open lan device " << Status << "\n";
	return 1;
    }

    Status = DeviceIoControl( LanFile,
			      IOCTL_IF_BUFFERED_MODE,
			      &On,
			      sizeof(On),
			      0,
			      0,
			      &PktLen,
			      NULL );

    if( !Status ) {
	cerr << "Could not turn on buffered mode " << Status << "\n";
	return 1;
    }

    while( cin >> word ) {
	if( word == "end" ) {
	    NtClose( LanFile );
	    return 0;
	} else if( word == "enum" ) {
	    Status = DeviceIoControl( LanFile,
				      IOCTL_IF_ENUM_ADAPTERS,
				      NULL,
				      0,
				      Packet,
				      sizeof(Packet),
				      &PktLen,
				      NULL );

	    cout << "EnumAdapters: " << Status << "\n";
	    if( Status )
		display_buffer( Packet, PktLen );
	} else if( word == "query" ) {
	    cin >> PktLen;

	    Status = DeviceIoControl( LanFile,
				      IOCTL_IF_ADAPTER_INFO,
				      &PktLen,
				      sizeof(PktLen),
				      Packet,
				      sizeof(Packet),
				      &PktLen,
				      NULL );

	    cout << "QueryAdapterInfo: " << Status << "\n";
	    if( Status )
		display_buffer( Packet, PktLen );
	} else if( word == "send" ) {
	    cin >> Hdr->Fixed.Adapter
		>> Hdr->Fixed.AddressType
		>> Hdr->Fixed.AddressLen
		>> Hdr->Fixed.PacketType;
	    Hdr->Fixed.Mdl = NULL;
	    PktLen = byte_till_end( Packet, Hdr->Address - (PCHAR)Hdr );
	    Status = NtWriteFile( LanFile,
				  NULL,
				  NULL,
				  NULL,
				  &Iosb,
				  Packet,
				  PktLen,
				  NULL,
				  NULL );

	    cout << "NtWriteFile: " << Status << "\n";
	} else if( word == "recv" ) {
	    ULONG ReadLen;
	    Status = NtReadFile( LanFile,
				 Event,
				 NULL,
				 NULL,
				 &Iosb,
				 Packet,
				 sizeof(Packet),
				 NULL,
				 NULL );
	    cout << "NtReadFile: " << Status << "\n";
	    if( Status == STATUS_PENDING ) {
		LARGE_INTEGER Timeout = { 0 };
		Status = NtWaitForSingleObject( Event, 1, &Timeout );
	    }

	    ReadLen = Iosb.Information;

	    if( Status == STATUS_SUCCESS ) {
		cout << "Read " << ReadLen << " bytes\n";
		display_buffer( Packet, ReadLen );
	    }
	}
    }

    return 0;
}