#include <stdio.h>
#include <windows.h>
#include <stdarg.h>
struct _S7NetInfo
{
char szMac[18];
char szIP[16];
char szSubNetMask[16];
char szGateway[16];
char szName[32];
char szType[32];
};
typedef _S7NetInfo S7NetInfo;
int search(LPSTR lpszPath, LPSTR lpszFilename);
int CheckFile(LPSTR lpszFilename);
int GetS7NetInfos(LPSTR lpszFilename, S7NetInfo *aInfos, int iInfoCount);
void RestoreS7Ip(char* pwd);
int Win_Debug(const char *szFormat, ...);
int GetBufferOffsetFromSearchArray(unsigned char *buff, int iBuffLen, int iStart, int iStop, unsigned char* search, int iSearchLen);
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
int i, iFound;
char szPath[1024];
GetModuleFileName(NULL, szPath, sizeof(szPath));
char* ptr = strrchr(szPath, '\\');
ptr[1] = '\0';
strcat_s(szPath, "S7NONFGX.tab");
iFound = GetS7NetInfos(szPath, NULL, 0);
switch (iFound)
{
case -1:
Win_Debug("Error file not found!");
break;
case 0:
Win_Debug("No IP address found!");
break;
default:
S7NetInfo *Infos = (S7NetInfo*)calloc(iFound, sizeof(S7NetInfo));
iFound = GetS7NetInfos(szPath, Infos, iFound);
for (i = 0; i < iFound; i++)
{
Win_Debug("IP : %s\nName : %s\nType : %s", Infos[i].szIP, Infos[i].szName, Infos[i].szType);
}
free(Infos);
}
return 0;
}
int GetS7NetInfos(LPSTR lpszFilename, S7NetInfo *aInfos, int iInfoCount)
{
HANDLE hFile;
DWORD dwBytesRead;
int iOffset = 0, iOffsetName = 0, iOffsetValid = 0, iOffsetIp = 0, iFound = 0;
hFile = CreateFile(lpszFilename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
int iFileSize = GetFileSize(hFile, NULL);
if (iFileSize <= 0)
{
CloseHandle(hFile);
return -1;
}
unsigned char *buff = (unsigned char*)malloc(iFileSize + 1);
ReadFile(hFile, buff, iFileSize, &dwBytesRead, NULL);
CloseHandle(hFile);
unsigned char searchValid[] = { 'A', 'd', 'd', 'r', 'e', 's', 's', 'I', 's', 'V', 'a', 'l', 'i', 'd' };
unsigned char searchName[] = { 'B', 'a', 'u', 'g', 'r', 'u', 'p', 'p', 'e', 'n', 'n', 'a', 'm', 'e' };
unsigned char searchIp[] = { 0xe0, 0x0f, 0x00, 0x00, 0xe0, 0x0f, 0x00, 0x00, 0x00 };
while ((iOffsetIp = GetBufferOffsetFromSearchArray(buff, iFileSize, iOffset, iFileSize, searchIp, sizeof(searchIp))) >= 0)
{
iOffset = iOffsetIp + 1;
iFound += 1;
if (aInfos != NULL)
{
if (iInfoCount < iFound) break;
iOffsetValid = GetBufferOffsetFromSearchArray(buff, iFileSize, iOffsetIp, iOffsetIp - 2000, searchValid, sizeof(searchValid));
iOffsetName = GetBufferOffsetFromSearchArray(buff, iFileSize, iOffsetValid, iOffsetIp + 2000, searchName, sizeof(searchName));
iOffsetName += 24;
memcpy_s(aInfos[iFound - 1].szName, sizeof(aInfos[iFound - 1].szName), &(buff[iOffsetName + 1]), buff[iOffsetName]);
iOffsetName += buff[iOffsetName] + 1;
iOffsetName += buff[iOffsetName] + 1;
memcpy_s(aInfos[iFound - 1].szType, sizeof(aInfos[iFound - 1].szType), &(buff[iOffsetName + 1]), buff[iOffsetName]);
//memcpy_s(aInfos[iFound - 1].szMac, sizeof(aInfos[iFound - 1].szMac), &(buff[iOffsetIp - 254]), 12);
iOffsetIp += 20;
memcpy_s(aInfos[iFound - 1].szIP, sizeof(aInfos[iFound - 1].szIP), &(buff[iOffsetIp]), 8);
RestoreS7Ip(aInfos[iFound - 1].szIP);
}
}
return iFound;
}
int Win_Debug(const char *szFormat, ...)
{
char szBuffer[1024];
va_list pArgList;
va_start(pArgList, szFormat);
_vsnprintf_s(szBuffer, sizeof(szBuffer), szFormat, pArgList);
va_end(pArgList);
return MessageBox(NULL, szBuffer, "S7 IP address", MB_ICONINFORMATION | MB_TOPMOST);
}
void RestoreS7Ip(char* sIp)
{
int i, j;
char part[3] = { 0 };
int num[4];
for (i = 0, j = 0; i < 7; i+=2, j++)
{
memcpy_s(part, sizeof(part), &(sIp[i]), 2);
num[j] = (int)strtol((const char*)part, NULL, 16);
}
sprintf_s(sIp, 16, "%i.%i.%i.%i", num[0], num[1], num[2], num[3]);
}
int GetBufferOffsetFromSearchArray(unsigned char *buff, int iBuffLen, int iStart, int iStop, unsigned char* search, int iSearchLen)
{
int i, j, ok;
if (iStart > iStop)
{
for (i = iStart; i > iStop && i > 0; i--)
{
ok = 1;
for (j = 0; j < iSearchLen; j++)
{
if (buff[i + j] != search[j])
{
ok = 0;
break;
}
}
if (ok == 1) return i;
}
}
else
{
for (i = iStart; i < iStop && i < iBuffLen; i++)
{
ok = 1;
for (j = 0; j < iSearchLen; j++)
{
if (buff[i + j] != search[j])
{
ok = 0;
break;
}
}
if (ok == 1) return i;
}
}
return -1;
}