返回列表 回复 发帖

远程打开telnet服务opentelnet程序代码

信息来源:refdom

opentelnet.cpp
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //     
  3. //     Resume Telnet Remote Config
  4. //     
  5. //     File     : ReumeTelnet.cpp
  6. //     Version   : 0.5
  7. //     Comment   : only for win2k and XP(pro\server\adv)
  8. //     
  9. //     Create at : 2002.3.9
  10. //     Create by : refdom
  11. //            Email       : [email]refdom@263.net[/email]
  12. //            Home Page : [url]www.opengram.com[/url]
  13. //
  14. //            If you modify the code, or add more functions, please email me a copy.
  15. //     
  16. ////////////////////////////////////////////////////////////////////////////////

  17. #include <stdio.h>
  18. #include <windows.h>

  19. #pragma comment(lib, "Advapi32.lib")
  20. #pragma comment(lib, "Mpr.lib")

  21. SC_HANDLE      g_schSCManager;
  22. SC_HANDLE   g_schRegistryService;
  23. HKEY            g_hKey;
  24. DWORD            g_DefaultTelnetStartType = 0;
  25. DWORD            g_DefaultRegistryStartType = 0;
  26. LPBYTE            g_lpDefaultTelnetNTLM;
  27. LPBYTE            g_lpDefaultTelnetPort;

  28. void Usage(char*);
  29. int CloseTelnet();
  30. int StartRemoteRegistry();
  31. int MyStartService(SC_HANDLE, char*);
  32. int ResumeRegistryService();

  33. int main(int argc, char* argv[])
  34. {
  35.      int nRetCode;
  36.      char szIpc[50] = "";
  37.      HKEY hKey;
  38.      LPSTR lpUserName, lpPassword;
  39.      NETRESOURCE NET;

  40.      Usage(argv[0]);
  41.      if (argc != 4)
  42.            return 0;

  43.      sprintf (szIpc, "%s\\ipc$", argv[1]);
  44.      lpUserName = argv[2];
  45.      lpPassword = argv[3];

  46.      NET.lpLocalName = NULL;
  47.      NET.lpRemoteName = szIpc;
  48.      NET.dwType = RESOURCETYPE_ANY;
  49.      NET.lpProvider = NULL;

  50.      printf ("Connecting %s...",argv[1]);

  51. ReConnect:

  52.      nRetCode = WNetCancelConnection2(szIpc, CONNECT_UPDATE_PROFILE, TRUE);
  53.      if (nRetCode == NO_ERROR)
  54.            printf ("Canncel Successfully!\n");

  55.      nRetCode = WNetAddConnection2(&NET, lpPassword, lpUserName, CONNECT_INTERACTIVE);
  56.      if (nRetCode == ERROR_ALREADY_ASSIGNED || nRetCode == ERROR_DEVICE_ALREADY_REMEMBERED)
  57.      {
  58.            printf ("Already conneted to the server!\n");
  59.            printf ("Now re-connecting the server...\n");
  60.            goto ReConnect;
  61.      }
  62.      else if (nRetCode == NO_ERROR)
  63.            printf ("Successfully!\n");
  64.      else
  65.      {
  66.            printf ("\n\tErr:");
  67.            switch (nRetCode)
  68.            {
  69.            case ERROR_ACCESS_DENIED:
  70.                  printf ("ERROR_ACCESS_DENIED\n");
  71.                  break;
  72.            case ERROR_BAD_NET_NAME:
  73.                  printf ("ERROR_BAD_NET_NAME\n");
  74.                  break;
  75.            default:
  76.                  printf ("CONNECT ERR:%d!\n",GetLastError());
  77.                  break;
  78.            }
  79.            return 0;
  80.      }

  81.      //open SCManager
  82.      g_schSCManager = OpenSCManager(argv[1], NULL, SC_MANAGER_ALL_ACCESS);
  83.      if (g_schSCManager == NULL)
  84.      {
  85.            printf ("Open SCManager failed!\n");
  86.            return 0;
  87.      }

  88.      //check remote registry service is running
  89.      if (!StartRemoteRegistry())
  90.      {
  91.            printf ("All Process Failed!\n");
  92.            return 0;
  93.      }

  94.      //edit the registry
  95.      if (!(RegConnectRegistry((LPCTSTR) argv[1], HKEY_LOCAL_MACHINE, &g_hKey) == ERROR_SUCCESS))
  96.      {
  97.            printf ("Connect remote registry failed!\n");
  98.            return 0;
  99.      }

  100.      if (!(RegOpenKeyEx(g_hKey, "SOFTWARE\\Microsoft\\TelnetServer\\1.0", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS))
  101.      {
  102.            printf ("Open key failed!\n");
  103.            return 0;
  104.      }

  105.      //read the registry for default config
  106.      g_lpDefaultTelnetNTLM = (LPBYTE) LocalAlloc(LPTR, 50);
  107.      g_lpDefaultTelnetPort = (LPBYTE) LocalAlloc(LPTR, 50);
  108.      DWORD dwDataSize = 50;
  109.      if (!(RegQueryValueEx(hKey, "default_NTLM", NULL, NULL, g_lpDefaultTelnetNTLM, &dwDataSize) == ERROR_SUCCESS))
  110.      {
  111.            printf ("Read NTLM failed!\n ");
  112.            printf ("%d\n", GetLastError());
  113.            return 0;
  114.      }
  115.      if (!(RegQueryValueEx(hKey, "default_Port", NULL, NULL, g_lpDefaultTelnetPort, &dwDataSize) == ERROR_SUCCESS))
  116.      {
  117.            printf ("Read port failed!\n ");
  118.            return 0;
  119.      }
  120.      if (!(RegQueryValueEx(hKey, "default_RegistryStart", NULL, NULL, (LPBYTE) &g_DefaultRegistryStartType, &dwDataSize) == ERROR_SUCCESS))
  121.      {
  122.            printf ("Read registrystart failed!\n ");
  123.            return 0;
  124.      }
  125.      if (!(RegQueryValueEx(hKey, "default_TelnetStart", NULL, NULL, (LPBYTE) &g_DefaultTelnetStartType, &dwDataSize) == ERROR_SUCCESS))
  126.      {
  127.            printf ("Read telnetstart failed!\n ");
  128.            return 0;
  129.      }
  130.      if (!(RegSetValueEx(hKey, "NTLM", 0, REG_DWORD, g_lpDefaultTelnetNTLM, sizeof(DWORD)) == ERROR_SUCCESS))
  131.      {
  132.            printf ("Set NTLM failed!");
  133.            return 0;
  134.      }

  135.      if (!(RegSetValueEx(hKey, "TelnetPort", 0, REG_DWORD, g_lpDefaultTelnetPort, sizeof(DWORD)) == ERROR_SUCCESS))
  136.      {
  137.            printf ("Set port failed!");
  138.            return 0;
  139.      }

  140.      //close telnet service
  141.      nRetCode = CloseTelnet();

  142.      if (nRetCode)
  143.      {
  144.            printf ("\nBINGLE!!!\n");
  145.            printf ("The config of remote telnet server is resumed!\n");
  146.      }

  147.      //resume the configure
  148.      if (!(RegDeleteValue(hKey, (LPCTSTR) "default_NTLM") == ERROR_SUCCESS))
  149.            printf ("Delete NTLM value failed!\n");
  150.      if (!(RegDeleteValue(hKey, (LPCTSTR) "default_Port") == ERROR_SUCCESS))
  151.            printf ("Delete port value failed!\n");
  152.      if (!(RegDeleteValue(hKey, (LPCTSTR) "default_RegistryStart") == ERROR_SUCCESS))
  153.            printf ("Delete registrystart value failed!\n");
  154.      if (!(RegDeleteValue(hKey, (LPCTSTR) "default_TelnetStart") == ERROR_SUCCESS))
  155.            printf ("Delete telnetstart value failed!\n");

  156.      RegCloseKey(hKey);
  157.      RegCloseKey(g_hKey);

  158.      ResumeRegistryService();
  159.      CloseServiceHandle(g_schRegistryService);
  160.      //close SCManager
  161.      CloseServiceHandle(g_schSCManager);

  162.      //close the session with remote server
  163.      printf ("\nDisconnecting server...");
  164.      nRetCode = WNetCancelConnection2(argv[1], CONNECT_UPDATE_PROFILE, TRUE);
  165.      if (nRetCode == NO_ERROR)
  166.            printf ("Successfully!\n");
  167.      else
  168.            printf ("Failed!\n");

  169.      LocalFree(g_lpDefaultTelnetNTLM);
  170.      LocalFree(g_lpDefaultTelnetPort);

  171.      return 0;
  172. }

  173. void Usage(char* pcAppName)
  174. {
  175.      printf ("*******************************************************\n");
  176.      printf ("Resume Remote Telnet Config, by refdom\n");
  177.      printf ("Email: [email]refdom@263.net[/email]\n");
  178.      printf ("%s\n\n", pcAppName);
  179.      printf ("Usage:ResumeTelnet.exe \\\\server username password\n");
  180.      printf ("*******************************************************\n");
  181.      return;
  182. }

  183. int CloseTelnet()
  184. {
  185.      DWORD                              dwWaitTime;
  186.      DWORD                              dwConfigSize;
  187.      SC_HANDLE                        schTelnetService;
  188.      SERVICE_STATUS                  ssTelnetStatus;
  189.      LPQUERY_SERVICE_CONFIG      lpTelnetConfig;

  190.      //stop the telnet service
  191.      schTelnetService = OpenService(g_schSCManager, "TlntSvr", SERVICE_ALL_ACCESS);
  192.      if (schTelnetService == NULL)
  193.      {
  194.            printf ("Open service failed!\n");
  195.            return 0;
  196.      }

  197.      lpTelnetConfig = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 1024);
  198.      if (lpTelnetConfig == NULL)
  199.      {
  200.            printf ("Alloc memory failed!\n");
  201.            return 0;
  202.      }
  203.      if (!QueryServiceConfig(schTelnetService, lpTelnetConfig, 1024, &dwConfigSize))
  204.      {
  205.            printf ("Query service congfig failed!\n");
  206.            return 0;
  207.      }

  208.      if (lpTelnetConfig->dwStartType != g_DefaultTelnetStartType)
  209.      {
  210.            if (!ChangeServiceConfig(schTelnetService,
  211.                                          SERVICE_NO_CHANGE,
  212.                                          g_DefaultTelnetStartType,
  213.                                          SERVICE_NO_CHANGE,
  214.                                          NULL, NULL, NULL, NULL, NULL, NULL, NULL))
  215.            {
  216.                  printf ("Set telnet start type error!\n");
  217.                  return 0;
  218.            }
  219.      }

  220.      if (!(QueryServiceStatus(schTelnetService, &ssTelnetStatus)))
  221.      {
  222.            printf ("Query service status failed!\n");
  223.            return 0;
  224.      }

  225.      if (ssTelnetStatus.dwCurrentState != SERVICE_STOPPED && ssTelnetStatus.dwCurrentState != SERVICE_STOP_PENDING)
  226.      {
  227.            printf ("Stopping telnet service ...\n");
  228.            if (!(ControlService(schTelnetService, SERVICE_CONTROL_STOP, &ssTelnetStatus)))
  229.            {
  230.                  printf ("Control telnet service status failed!\n");
  231.                  return 0;
  232.            }

  233.            dwWaitTime = ssTelnetStatus.dwWaitHint / 10;
  234.            if( dwWaitTime < 1000 )
  235.                  dwWaitTime = 1000;
  236.            else if ( dwWaitTime > 10000 )
  237.                  dwWaitTime = 10000;

  238.            Sleep(dwWaitTime);
  239.            if (!QueryServiceStatus(schTelnetService, &ssTelnetStatus))
  240.            {
  241.                  printf ("Query service status failed!\n");
  242.            }

  243.            if ( ssTelnetStatus.dwCurrentState == SERVICE_STOPPED || ssTelnetStatus.dwCurrentState == SERVICE_STOP_PENDING)
  244.            {
  245.                  printf ("Telnet service is stopped successfully!\n");
  246.            }
  247.            else
  248.            {
  249.                  printf ("Stopping telnet service failed!\n");
  250.                  return 0;
  251.            }
  252.      }

  253.      LocalFree(lpTelnetConfig);
  254.      CloseServiceHandle(schTelnetService);
  255.      return 1;
  256. }

  257. int StartRemoteRegistry()
  258. {
  259.      SC_HANDLE                        schRegistryService;
  260.      SERVICE_STATUS                  ssRegistryStatus;

  261.      schRegistryService = OpenService( g_schSCManager, "RemoteRegistry", SERVICE_ALL_ACCESS);
  262.      g_schRegistryService = OpenService( g_schSCManager, "RemoteRegistry", SERVICE_ALL_ACCESS);;

  263.      if (schRegistryService == NULL || g_schRegistryService == NULL)
  264.      {
  265.            printf ("Open remote registry service failed!\n");
  266.            return 0;
  267.      }

  268.      if (!QueryServiceStatus(schRegistryService, &ssRegistryStatus))
  269.      {
  270.            printf ("Query remote registry service failed!\n");
  271.            return 0;
  272.      }

  273.      if (ssRegistryStatus.dwCurrentState != SERVICE_RUNNING)
  274.      {
  275.            if (!MyStartService(schRegistryService, "remote registry"))
  276.                  return 0;
  277.      }

  278.      CloseServiceHandle(schRegistryService);
  279.      return 1;
  280. }

  281. int MyStartService(SC_HANDLE schService, char* szServiceName)
  282. {
  283.      DWORD dwWaitTime;
  284.      DWORD dwOldCheckPoint;
  285.      DWORD dwStartTickCount;
  286.      SERVICE_STATUS ssStatus;

  287.      printf ("Starting %s service...\n", szServiceName);
  288.      if (!(StartService(schService, 0, NULL)))
  289.      {
  290.            printf ("Starting %s service failed!\n", szServiceName);
  291.            return 0;
  292.      }

  293.      if (!(QueryServiceStatus(schService, &ssStatus)))
  294.      {
  295.            printf ("Query %s service status failed!\n",szServiceName);
  296. //            return ;
  297.      }

  298.   dwStartTickCount = GetTickCount();
  299.   dwOldCheckPoint = ssStatus.dwCheckPoint;

  300.      while ( ssStatus.dwCurrentState == SERVICE_START_PENDING)
  301.      {
  302.            dwWaitTime = ssStatus.dwWaitHint / 10;
  303.     if( dwWaitTime < 1000 )
  304.         dwWaitTime = 1000;
  305.     else if ( dwWaitTime > 10000 )
  306.         dwWaitTime = 10000;

  307.            Sleep(dwWaitTime);

  308.     // Check the status again.

  309.     if (!QueryServiceStatus(schService, &ssStatus))
  310.         break;

  311.     if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
  312.     {
  313.         // The service is making progress.
  314.         dwStartTickCount = GetTickCount();
  315.         dwOldCheckPoint = ssStatus.dwCheckPoint;
  316.     }
  317.     else
  318.     {
  319.         if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
  320.         {
  321.           // No progress made within the wait hint
  322.           break;
  323.         }
  324.     }
  325.      }

  326.      if ( ssStatus.dwCurrentState == SERVICE_RUNNING )
  327.      {
  328.            printf ("%s service is started successfully! %s service is running!\n", szServiceName, szServiceName);
  329.      }
  330.      else
  331.      {
  332.            printf ("%s service is not started!\n", szServiceName);
  333.            return 0;
  334.      }

  335.      return 1;
  336. }

  337. int ResumeRegistryService()
  338. {
  339.      LPQUERY_SERVICE_CONFIG      lpRegistryConfig;
  340.      DWORD                              dwConfigSize;

  341.      lpRegistryConfig = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 1024);
  342.      if (lpRegistryConfig == NULL)
  343.      {
  344.            printf ("Alloc memory failed!\n");
  345.            return 0;
  346.      }
  347.      if (!QueryServiceConfig(g_schRegistryService, lpRegistryConfig, 1024, &dwConfigSize))
  348.      {
  349.            printf ("Query registry service congfig failed!\n");
  350.            printf ("%d\n",GetLastError());
  351.            return 0;
  352.      }

  353.      if (lpRegistryConfig->dwStartType != g_DefaultRegistryStartType)
  354.      {
  355.            if (!ChangeServiceConfig(g_schRegistryService,
  356.                                                SERVICE_NO_CHANGE,
  357.                                                g_DefaultRegistryStartType,
  358.                                                SERVICE_NO_CHANGE,
  359.                                                NULL, NULL, NULL, NULL, NULL, NULL, NULL))
  360.            {
  361.                  printf ("Set registry start type error!\n");
  362.                  return 0;
  363.            }
  364.      }

  365.      LocalFree(lpRegistryConfig);
  366.      return 1;
  367. }
复制代码
返回列表