473,406 Members | 2,378 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Win32 Buffer Overflow

I am writing a GUI for a program which scrambles text messages, but I believe I am running into buffer overflows somewhere and have tried various ways of stopping the overflows from happening. When the user input text, it encrypts fine, but when the user decrypts text, they will get various strings appended to the end from memory, such as windir=C:\Windows, and other random strings.

Expand|Select|Wrap|Line Numbers
  1. #define IDB_BUTTON 101
  2. #define IDB_EDIT 102
  3. #define IDB_EDITDATA 103
  4. #define BTE_DECRYPT 104
  5. #include <windows.h>
  6. #include <string>
  7.  
  8.  
  9.  
  10. using namespace std;
  11. string toencrypt;
  12. string key;
  13. int decryptencrypt = 1;
  14. char* encrypted;
  15. char* decrypted;
  16. string todecrypt;
  17. int length;
  18.     HWND       decryptbutton;           
  19.     HWND       encryptedfield;
  20.     HWND       databoxLabel;
  21.     HWND       datafield;
  22.     HWND       keyLabel;
  23.     HWND       KEY;
  24.     HWND       BUTTON;
  25.     MSG        Msg;
  26.     HWND       hWnd;
  27.     HRESULT       hRet;
  28.     WNDCLASSEX WndClsEx;
  29.  
  30. void encrypt()
  31. {
  32.      if(toencrypt=="" || toencrypt==" ")
  33.      {}
  34.      else
  35.      {
  36.      int maxlen = key.length();
  37.     string encrypte = "%";
  38.           decryptencrypt = 0;
  39.                    for(int x = 0;x<key.length();x++)
  40.                    {
  41.                            if(key[x] == '1')
  42.                            {
  43.                                toencrypt.assign(toencrypt.rbegin(), toencrypt.rend());
  44.                            }
  45.                            else if(key[x] == '2')
  46.                            {
  47.                               for(int x = 0;x<toencrypt.length();x++)
  48.                               {
  49.                                  if(toencrypt[x]==' ')
  50.                                  {
  51.                                    toencrypt.replace(x,1,encrypte);
  52.                                    }
  53.                                    else{}
  54.                               }
  55.                            }
  56.                            else if(key[x] == '3')
  57.                            {
  58.  
  59.                            }
  60.                            else if(key[x] == '4')
  61.                            {
  62.  
  63.                            }
  64.                            else if(key[x] == '5')
  65.                            {
  66.                            }
  67.                            else if(key[x] == '6')
  68.                            {
  69.                            }
  70.                            else if(key[x] == '7')
  71.                            {
  72.                            }
  73.                            else if(key[x] == '8')
  74.                            {
  75.                            }
  76.                            else if(key[x] == '9')
  77.                            {
  78.                            }
  79.                            else if(key[x] == '0')
  80.                            {
  81.                            }
  82.                            else{MessageBox(NULL, "INVALID KEY", "ERROR", MB_OK);}
  83.                            }
  84.                    encrypted = new char[toencrypt.length() + 10];
  85.                    for(int x = 0;x<toencrypt.length() + 1;x++)
  86.                    {
  87.                       encrypted[x] = toencrypt[x];
  88.                    }
  89.               SetDlgItemText(hWnd, IDB_EDITDATA, encrypted);
  90.               toencrypt = "";
  91.               }
  92.  
  93.  
  94. }
  95. void decrypt()
  96. {
  97.     if(todecrypt=="" || todecrypt==" ")
  98.     {}
  99.     else
  100.     {
  101.   string encrypte2 = " ";
  102.          decryptencrypt = 1;
  103.      key.assign(key.rbegin(), key.rend());
  104.      for(int x = 0;x<key.length();x++)
  105.                    {
  106.                            if(key[x] == '1')
  107.                            {
  108.                                todecrypt.assign(todecrypt.rbegin(), todecrypt.rend());
  109.                            }
  110.                            else if(key[x] == '2')
  111.                            {
  112.                                 for(int x = 0;x<todecrypt.length();x++)
  113.                               {
  114.                                  if(todecrypt[x]=='%')
  115.                                  {
  116.                                    todecrypt.replace(x,1,encrypte2);
  117.                                    }
  118.                                    else{}
  119.                                    }
  120.                            }
  121.                            else if(key[x] == '3')
  122.                            {
  123.  
  124.                            }
  125.                            else if(key[x] == '4')
  126.                            {
  127.  
  128.  
  129.                            }
  130.                            else if(key[x] == '5')
  131.                            {
  132.                            }
  133.                            else if(key[x] == '6')
  134.                            {
  135.                            }
  136.                            else if(key[x] == '7')
  137.                            {
  138.                            }
  139.                            else if(key[x] == '8')
  140.                            {
  141.                            }
  142.                            else if(key[x] == '9')
  143.                            {
  144.                            }
  145.                            else if(key[x] == '0')
  146.                            {
  147.                            }
  148.                            else{MessageBox(NULL, "INVALID KEY", "ERROR", MB_OK);}
  149.                    }
  150.                    decrypted = new char[todecrypt.length() + 10];
  151.                    for(int x = 0;x<todecrypt.length();x++)
  152.                    {
  153.                      decrypted[x] = todecrypt[x];
  154.                      }
  155.                    SetDlgItemText(hWnd, IDB_EDITDATA, decrypted);
  156.                    todecrypt = "";
  157.                    }
  158.  
  159. }
  160. LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
  161.                WPARAM wParam, LPARAM lParam);
  162.  
  163. INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  164.                LPSTR lpCmdLine, int nCmdShow)
  165. {
  166.  
  167.  
  168.     // Populate the WNDCLASSEX structure
  169.     WndClsEx.cbSize        = sizeof(WNDCLASSEX);
  170.     WndClsEx.style         = CS_HREDRAW | CS_VREDRAW;
  171.     WndClsEx.lpfnWndProc   = WndProcedure;
  172.     WndClsEx.cbClsExtra    = 0;
  173.     WndClsEx.cbWndExtra    = 0;
  174.     WndClsEx.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
  175.     WndClsEx.hCursor       = LoadCursor(NULL, IDC_ARROW);
  176.     WndClsEx.hbrBackground = (HBRUSH)(COLOR_WINDOW);
  177.     WndClsEx.lpszMenuName  = NULL;
  178.     WndClsEx.lpszClassName = "ENCRYPTER";
  179.     WndClsEx.hInstance     = hInstance;
  180.     WndClsEx.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
  181.  
  182.     // Register the class
  183.     RegisterClassEx(&WndClsEx);
  184.  
  185.     // Create the window object
  186.     hWnd = CreateWindow("ENCRYPTER",
  187.               "ENCRYPTER",
  188.               WS_OVERLAPPEDWINDOW,
  189.               0,
  190.               0,
  191.               800,
  192.               400,
  193.               NULL,
  194.               NULL,
  195.               hInstance,
  196.               NULL);
  197.     decryptbutton = CreateWindow(
  198.     "BUTTON",
  199.     "Decrypt",
  200.     WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
  201.     500,
  202.     300,
  203.     150,
  204.     50,
  205.     hWnd,
  206.     (HMENU)BTE_DECRYPT,
  207.     hInstance,
  208.     NULL
  209.     );
  210.     databoxLabel = CreateWindow(
  211.     "STATIC",
  212.     "DATA",
  213.     WS_VISIBLE | WS_CHILD,
  214.     18,
  215.     35,
  216.     100,
  217.     100,
  218.     hWnd,
  219.     NULL,
  220.     hInstance,
  221.     NULL
  222.     );
  223.     datafield = CreateWindow(
  224.     "EDIT",
  225.     "",
  226.     WS_VISIBLE | WS_CHILD | WS_BORDER | ES_LEFT | ES_MULTILINE | WS_VSCROLL,
  227.     60,
  228.     35,
  229.     700,
  230.     250,
  231.     hWnd,
  232.     (HMENU)IDB_EDITDATA,
  233.     hInstance,
  234.     NULL
  235.     );
  236.     keyLabel = CreateWindow(
  237.     "STATIC",
  238.     "KEY",
  239.     WS_VISIBLE | WS_CHILD,
  240.     18, 
  241.     10, 
  242.     100, 
  243.     20,
  244.     hWnd,
  245.     NULL, 
  246.     hInstance,
  247.     NULL
  248.     );
  249.  
  250.     KEY = CreateWindow(
  251.     "EDIT",
  252.     "",
  253.     WS_VISIBLE | WS_CHILD | ES_LEFT | WS_BORDER | ES_NUMBER,
  254.     60, 
  255.     10, 
  256.     700, 
  257.     20,
  258.     hWnd,
  259.     (HMENU)IDB_EDIT,
  260.     hInstance,
  261.     NULL
  262.     );
  263.  
  264.     BUTTON = CreateWindow( 
  265.     "BUTTON",                                   
  266.     "Encrypt",                                        
  267.     WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,                                   
  268.     300,                                 
  269.     300,                   
  270.     150,                                      
  271.     50,                   
  272.     hWnd,                                     
  273.     (HMENU)IDB_BUTTON,                              
  274.     hInstance,                                   
  275.     NULL                                       
  276. );
  277.  
  278.     if ( !BUTTON)
  279.        return 0;
  280.     // Verify window creation
  281.     if( !hWnd ) // If the window was not created,
  282.         return 0; // stop the application
  283.  
  284.     // Show the window
  285.     ShowWindow(hWnd, SW_SHOWNORMAL);
  286.  
  287.     UpdateWindow(hWnd);
  288.     // our message pump
  289.     while( (hRet = GetMessage( &Msg, NULL, 0, 0 )) != 0)
  290.     { 
  291.         if (hRet == -1)
  292.         {
  293.         // handle the error and possibly exit
  294.         }
  295.         else
  296.         {
  297.             TranslateMessage(&Msg); 
  298.             DispatchMessage(&Msg); 
  299.         }
  300.     }
  301. }
  302.  
  303. //////////////////
  304. // WndProcedure //
  305. //////////////////
  306.  
  307. LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
  308.                WPARAM wParam, LPARAM lParam)
  309. {
  310.     switch(Msg)
  311.     {
  312.     case WM_COMMAND:
  313.          switch(LOWORD(wParam))
  314.          {
  315.             case IDB_BUTTON:
  316.                 length = GetWindowTextLength(GetDlgItem(hWnd, IDB_EDITDATA)) + 1;
  317.                 char temp[length];
  318.                 GetDlgItemText(hWnd, IDB_EDITDATA, temp, length);
  319.                 toencrypt.assign(temp);
  320.                 delete [] temp;
  321.                 length = GetWindowTextLength(GetDlgItem(hWnd, IDB_EDIT)) + 1;
  322.                 char temp2[length];
  323.                 GetDlgItemText(hWnd, IDB_EDIT, temp2, length);
  324.                 key.assign(temp2);
  325.                 delete [] temp2;
  326.                 encrypt();
  327.                 break;
  328.             case BTE_DECRYPT:
  329.                  length = GetWindowTextLength(GetDlgItem(hWnd, IDB_EDITDATA)) + 1;
  330.                  char temp3[length + 1];
  331.                  GetDlgItemText(hWnd, IDB_EDITDATA, temp3, length);
  332.                  todecrypt.assign(temp3);
  333.                  delete [] temp3;
  334.                  length = GetWindowTextLength(GetDlgItem(hWnd, IDB_EDIT)) + 1;
  335.                  char temp4[length + 1];
  336.                  GetDlgItemText(hWnd, IDB_EDIT, temp4, length);
  337.                  key.assign(temp4);
  338.                  delete [] temp4;
  339.                  decrypt();
  340.                  break;
  341.             default:
  342.                     break;
  343.          }
  344.    break;
  345.     case WM_DESTROY:
  346.         // user wants to exit
  347.         PostQuitMessage(WM_QUIT);
  348.         break;
  349.     default:
  350.         // Hand off unprocessed messages to DefWindowProc
  351.         return DefWindowProc(hWnd, Msg, wParam, lParam);
  352.     }
  353.  
  354.     return 0;
  355. }
Sep 21 '07 #1
3 1976
weaknessforcats
9,208 Expert Mod 8TB
Part of it is here:
for(int x = 0;x<toencrypt.length() + 1;x++)
{
encrypted[x] = toencrypt[x];
}
You are running off the end of the string. The loop should be:

Expand|Select|Wrap|Line Numbers
  1. size_t length =  toencrypt.size()
  2. for(int x = 0;x<lkength;x++)
  3.                    {
  4.                       encrypted[x] = toencrypt[x];
  5.                    }
  6.  
The method string::length() is deprecated. You should be using string::size().

Writing loops using STL containers is safer if you use iterators.

Then there's this:
[quote=newguy194]
char temp2[length];
GetDlgItemText(hWnd, IDB_EDIT, temp2, length);
key.assign(temp2);
delete [] temp2;
[/code]

Here you are deleting buffer temp2 that you never allocated. This is a guaranteed run-time crash. Ditto for temp3.

Other observations are:
1) Why all the global variables???
2) What the the 10 for?? Code should not have magic numbers:
decrypted = new char[todecrypt.length() + 10];
3) Lotta string literals that should be in a resource string table. I would have expected an IDS_etc...
4)These belong in a resource table
#define IDB_BUTTON 101
#define IDB_EDIT 102
#define IDB_EDITDATA 103
5) This should be a const unsigned int:
#define BTE_DECRYPT 104
Sep 21 '07 #2
Thanks for the help, I'm 'trying' to teach myself win32 from online tutorials and some old books belonging to my brother, the books are dated from 1999 through 2002, so I might need a newer one.
Sep 21 '07 #3
Studlyami
464 Expert 256MB
If your using Win32 those books are fine. I can't really think anything that would change from 2000 on. There was a .dll update that allowed ownerdraw for a few extra controls. Win32 is old and is well on its way out for new development.
Sep 21 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

13
by: Ioannis Vranos | last post by:
If we want our programs to be protected against buffer overflows, must we check the size of the various containers explicitly? E.g. #include <iostream> #include <string> int main()
98
by: jacob navia | last post by:
<< QUOTE It is NOT a C compiler, because it doesn't conform to any commonly accepted C specification (K&R, C89, C99). You have no right to call it a C compiler until you get it to conform quote...
2
by: Tim::.. | last post by:
Hi... I have a major problem with a web application I am about to launch and just can't find out what the problem is... I believe it might be a Buffer Overflow problem but can't pin point the...
2
by: jay | last post by:
I am attempting to impersonate an account in ASPNET. I am using aspnet_setreg to store the username and passwords. I have given the ASPNET account permisision to read the registry values. However,...
2
by: Chris | last post by:
I have experienced the "Blank Message Box" problem when using McAfee 8 with Visual Studio and VB. I can disable buffer overflow protection and it fixes the problem, but it is only temporary as my...
13
by: Susan Beebe | last post by:
I have downloaded the code described in Microsoft article Q154078. I am trying to send raw ZPL (zebra barcode printer) code from Microsoft access. It works just fine if I hard code the actual...
9
by: Notebooker | last post by:
Hello, I'm an intermediate noob reading-in data from ascii-file using an ifstream object. I have specified a c-style string buffer with size of type size_t and I am specifying to use this...
5
by: Henaro | last post by:
Hello~ I am having trouble setting environment variables in C++ on win32. The code that is not working is: char prxy; char pf_cmd1 = "set http_proxy="; ....
4
by: raashid bhatt | last post by:
do buffer overflow happens with global variables
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.