How to find out programs communicate? - bots

I will try to explain with example;
#include <windows.h>
#include <iostream>
using namespace std;
int main(){
HWND hwndCalc;
hwndCalc = FindWindow(NULL, "Calculator");
if(!hwndCalc)
return 1;
SendMessage(hwndCalc, WM_COMMAND, MAKEWPARAM(0x84, BN_CLICKED), 0);
return 0;
}
In calculator, when you click a button SendMessage function works. I can find it with ollydbg. But with ollydbg, I can't debug other, normal programs.(exceptions, violations, crashes...)
So i am wondering whether there is any other way to find out how programs work?

Related

error:argument type 'xx' is incomplete for hiding call to ptrace example code

I'm testing an antidebug solution with ptrace method; and i compile the program by using ndk21e cross-compile.
The problem is that it compiles successfully with gcc, but fails with ndk cross-compile.
ndk cross-compile compiles all other programs without problems
#include <stdlib.h>
#include <stdio.h>
#include<sys/ptrace.h>
#include <dlfcn.h>
#include <string.h>
int main(int argc, char **argv) {
void *handle;
long (*go)(enum __ptrace_request request, pid_t pid);
// get a handle to the library that contains 'ptrace'
handle = dlopen ("/lib/x86_64-linux-gnu/libc.so.6", RTLD_LAZY);
// reference to the dynamically-resolved function 'ptrace'
go = dlsym(handle, "ptrace");
if (go(PTRACE_TRACEME, 0) < 0) {
puts("being traced");
exit(1);
}
puts("not being traced");
// cleanup
dlclose(handle);
return 0;
}
And it shows the error like the picture as follow:
gcc compileresult and cross-compile error result
How can i solve this problem. Thanks.

Problems when reading integers with scanf

I know that it might be a stupid question but can you tell me why the following patch of code fails? I see nothing wrong. I am trying to read integers using scanf. I have included the necessary library, but when I run the program it crashes after I read the first s. Thank you.
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <vector>
using namespace std;
int main()
{
int n, x;
scanf("%d", &n); scanf("%d", &x);
vector< pair<int, int> > moments;
for(int i = 0; i < n; ++i)
{
int f, s;
scanf("%d", &f);
scanf("%d", &s );
moments[i].first = f;
moments[i].second = s;
}
return 0;
}
That is not the way to assign values to moments since moments[i] does not yet exist. Try:
pair<int, int> thing;
thing = make_pair(f,s);
moments.push_back(thing);
instead of your assignements to moments elements.

linux ptrace() get function information

i want to catch information from user defined function using ptrace() calls.
but function address is not stable(because ASLR).
how can i get another program's function information like gdb programmatically?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/user.h>
#include <sys/wait.h>
#include <sys/ptrace.h>
#include <dlfcn.h>
#include <errno.h>
void error(char *msg)
{
perror(msg);
exit(-1);
}
int main(int argc, char **argv)
{
long ret = 0;
void *handle;
pid_t pid = 0;
struct user_regs_struct regs;
int *hackme_addr = 0;
pid = atoi(argv[1]);
ret = ptrace(PTRACE_ATTACH, pid, NULL, NULL);
if(ret<0)
{
error("ptrace() error");
}
ret = waitpid(pid, NULL, WUNTRACED);
if(ret<0)
{
error("waitpid ()");
}
ret = ptrace(PTRACE_GETREGS, pid, NULL, &regs);
if(ret<0)
{
error("GETREGS error");
}
printf("EIP : 0x%x\n", (int)regs.eip);
ptrace(PTRACE_DETACH, pid, NULL, NULL);
return 0;
}
ptrace is a bit ugly, but it can be useful.
Here's a ptrace example program; it's used to make I/O-related system calls pause.
http://stromberg.dnsalias.org/~strombrg/slowdown/
You could of course also study gdb, but ISTR it's pretty huge.
You might also check out strace and ltrace, perhaps especially ltrace since it lists symbols.
HTH
You probably want to call a function that resides in a specific executable (probably, a shared object). So, first, you will have to find the base address this executable is mapped on using
/proc/pid/maps
After that, you need to find the local offset of the function you are interested in, and you can do this in two ways:
Understand the ELF file format (Linux native executable format), and searching the desired function using the mapped file (This requires some specialty)
Using a ready to use elfparser (probably readelf tool) to get the function offset under the executable. Note that you will have to figure out the real local offset since this tool usually gives you the address as if the executable was mapped to a specific address

How to make a C++ program run maximized automatically?

I need to know whether there is a code for a C++ program to automatically maximize the program window since I always have to maximize the window when I run the program.
I'm using Windows 7.
I am very much new to C++.
Can someone help me? Thanks.
Try this It will Work
#include "stdafx.h"
#include "conio.h"
#include "Windows.h"
#include "tchar.h"
int _tmain(int argc, _TCHAR* argv[])
{
//Write Your Code HERE//
HWND hWnd;
SetConsoleTitle(_T("test"));
hWnd = FindWindow(NULL, _T("test"));
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD NewSBSize = GetLargestConsoleWindowSize(hOut);
SMALL_RECT DisplayArea = {0, 0, 0, 0};
SetConsoleScreenBufferSize(hOut, NewSBSize);
DisplayArea.Right = NewSBSize.X - 1;
DisplayArea.Bottom = NewSBSize.Y - 1;
SetConsoleWindowInfo(hOut, TRUE, &DisplayArea);
ShowWindow(hWnd, SW_MAXIMIZE);
_getch();
return 0;
}
It Will show your Output in Maximized Window.
Try ShowWindow(SW_MAXIMIZED). You would have to run a program you created, FindWindow(your target) and then invoke ShowWindow(SW_MAXIMIZED) on it. Note that this is achievable through AutoHotkey and no C++.
If you wanna maximize your program when it runs you can use this code in your Main Form
__fastcall TMainForm::TMainForm(TComponent* Owner) : TForm(Owner)
{
WindowState = wsMaximized;
}
Or if you want to maximize your program during codes e.g. pressing a button then you can use this code if it's in you're Main form:
ShowWindow(this->Handle, SW_SHOWMAXIMIZED);
Or this one if you're in a child one :
ShowWindow(Application->Handle, SW_SHOWMAXIMIZED);
This worked for me.
#include <windows.h>
void maximizeWindow(){
HWND hwnd = GetConsoleWindow();
ShowWindow(hwnd, SW_SHOWMAXIMIZED);
}

Array of SlimReaderWriter (SRW) Locks (using new operator)

Maybe this sounds stupid, but I want to create a vector/array of SRWLocks dynamically during runtime. The following code compiles both ways, but the array thing doesn’t work. Probably the question should be, can I create an SRWLock object using the new operator?
If this is not how it’s done, what’s the right way to create an array of SRWLock objects?
#include "stdafx.h"
#include <ppl.h>
#include <Windows.h>
#include <iostream>
using namespace std;
#define NOT_WORKING
int _tmain(int argc, _TCHAR* argv[])
{
#ifdef NOT_WORKING
// The following doesn't work
SRWLOCK *lock = new SRWLOCK[2];
int a = 10;
AcquireSRWLockExclusive(&lock[0]);
cout<<"Exclusive Lock Acquired";
a++;
ReleaseSRWLockExclusive(&lock[0]);
// The following doesn't work either
/*
SRWLOCK *lock_n = new SRWLOCK;
AcquireSRWLockExclusive(lock_n);
cout<<"Exclusive Lock Acquired";
a++;
ReleaseSRWLockExclusive(lock_n);
*/
#else
// This works
SRWLOCK lock;
int a = 10;
AcquireSRWLockExclusive(&lock);
cout<<"Exclusive Lock Acquired";
a++;
ReleaseSRWLockExclusive(&lock);
#endif
cout<<"Value of a:"<<a;
cin.get();
return 0;
}
User1577008's own answer:
Got it! One needs to use the InitializeSRWLock() function when lock objects are created dynamically. Somebody paste this answer, and help me close this question.

Resources