I just tried the following code snippet for shellcode testing purposes:-
#include<iostream>
using namespace std;
char sc[] = ""; #i've removed the shellcode
int main() {
int (*func)();
func = (int(*)())sc;
(int)(*func)();
}
I get a build error on compilation :-
------ Build started: Project: shellcoderunner, Configuration: Debug Win32 ------
Build started 10/15/2011 12:51:16 PM.
InitializeBuildStatus:
Touching "Debug\shellcoderunner.unsuccessfulbuild".
ClCompile:
blah.cpp
c:\users\reverser\documents\visual studio 2010\projects\shellcoderunner\shellcoderunner\blah.cpp(7): error C2440: 'type cast' : cannot convert from 'char [149]' to 'int (__cdecl *)(void)'
There is no context in which this conversion is possible
Build FAILED.
Time Elapsed 00:00:01.99
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Something obvious that I'm doing wrong?
To execute a shellcode in your C/C++ program with VS, the simplest way is embedding an Assembly code like this example below:
char* buffer="blah blah blah";
int main() {
__asm{
lea eax, buffer
call eax
}
}
Hope this help!
[
At the time I am answering the question is about why compilation fails for …
#include<iostream>
using namespace std;
char sc[] = ""; #i've removed the shellcode
int main() {
int (*func)();
func = (int(*)())sc;
(int)(*func)();
}
This code is an attempt to execute data bytes as machine code. However, the OP calls this a “code snippet for shellcode testing purposes”, which is unrelated. And so I am including this original context.
]
You may have success using a void* as intermediary.
In the formal even that should not compile, because in the formal a data pointer cannot be converted to a function pointer or vice versa.
However, reportedly Posix requires the ability to do that conversion, and it's old existing practice, so I believe most if not all compilers support it.
Note that you are in UB-land as regarding effects.
Also, note that anti-virus software and page level execute permission checking may disagree a bit with trying to execute the bytes in a string as machine code, so at that higher level yes you're doing something obviously wrong. ;-)
By the way, if what you are trying to achieve is to execute a shell script, then look into the system function.
What command to pass in the system call would depend on your system, so if you change your question be sure to include information about that.
Cheers & hth.,
I think the following should work:
char sc[] = ""; // i've removed the shellcode
int main()
{
int (*func)() = (int(*)())sc; // C++
int (*func)() = sc; /* C */
func();
}
It's technically undefined behaviour, but then again that's the whole point of shellcode.
You cannot cast an array to a function pointer. You have to first acquire a pointer to the array, which then can be cast:
func = (int(*)())≻
Related
When i include uhd/usb_control.hpp in my main.cpp :
#include <uhd/transport/usb_control.hpp>
/* Some other includes */
int main (void)
{
uhd::transport::usb_control::sptr usbSpeed;
usbSpeed = uhd::transport::usb_control::make(handle, 0);
/* `handle` is a `usb_device_handle::vid_pid_pair_t` */
}
I got error from here:
static sptr make(usb_device_handle::sptr handle, const int interface);
Error:
unexpected token struct. Did you forget a ';'
struct: missing tag name
And another strange error in:
usbSpeed = uhd::transport::usb_control::make(handle, 0);
Error:
Cannot convert argument 2 from int to const int
The only implementation that i find for uhd::transport::usb_control::make is uhd/transport/usb_dummy_impl.cpp which only throw an exception.
Environment information:
Compiler: MS Visual Studio 2017
OS: MS Windows 10
C++ Standard: 17
How to fix those errors ? I only what to detect the USRP usb type. For this i read the uhd source code and i find the uhd/transport/usb_control.hpp, But I have encountered those errors.
maybe the cause of this unexpected behavior is related to your included files and a conflict between some of them, as you mentioned in addition of #include <uhd/transport/usb_control.hpp> you have some other includes. i suggest move this include line upper and lower of other includes and test your code again.
wish my suggest be useful.
Im starting to learn c++ and was under the impression that by putting const is means that the value wont change but i wrote the following code:
#include<iostream>
int main()
{
const int a = 1;
a += 1;
std::cout << a << std::endl;
return 0;
}
and it prints out 2 while i thought it would have given me an error for changing a const int value.
I am using MSVS as my compiler
EDIT: I get a compiler warning saying C4530: C++ exception handler used, but unwind semantics are not enabled, specify /EHsc
It works now and gives me the correct error but does anyone know what this means
This program cannot be compiled using GNU GCC 4.8:
alioth% g++ x.cpp
x.cpp: In function ‘int main()’:
x.cpp:6:7: error: assignment of read-only variable ‘a’
a += 1;
Either your compiler is broken or you are doing something wrong (like compiling different project).
This program cannot be compiled on VS2013:
1>------ Build started: Project: SOTesting, Configuration: Release Win32 ------
1> Source.cpp
1>Source.cpp(6): error C3892: 'a' : you cannot assign to a variable that is const
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========`
The posted code:
#include<iostream>
int main()
{
const int a = 1;
a += 1;
std::cout << a << std::endl;
return 0;
}
The claim that this code compiled and produced "2" as output, is incorrect.
You can easily get the impression of something like that by inadvertently compiling a different program, or not noticing that a compilation failed and then running an existing executable.
when compiling a C++ program which contains a "call" to the pthread_cleanup_pop(E) macro, the following error is thrown by g++:
error: second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
Now, the apparent problem here is that the macro above expands to
(*pthread_getclean() = _pthread_cup.next, (E?_pthread_cup.func((pthread_once_t *)_pthread_cup.arg):0));} where the second expression is a call to a function returning void, but the third expression is simply 0.
Althoug I get the basic issue here, I really don't see why the warning occurs in this particular use, as the "result" of the condition is not assigned to anything.
For the record: I compiled the C++ program using MinGW-w64 3.3.0 (GCC 4.9.2) both with and without -std=c++98. In both cases, the error occurs. If I compile the same code as a C program (both with or without -std=c99), there is no error.
Does anyone know how I could get rid of that error in C++, other than by editing pthread.h?
Many thanks in advance!
EDIT: Here is some example code for reference:
#include <pthread.h>
static int cancelled = 0;
static void test_thread_cleanup(void *_arg){
cancelled = 1;
}
static void* test_thread(void *_arg){
pthread_cleanup_push(&test_thread_cleanup, NULL); // push cleanup handler on stack
while (1){ // never left unless cancelled via pthread_cancel() from main()
pthread_testcancel(); // just test for pthread_cancel() having been called
}
pthread_cleanup_pop(1); // pop cleanup handler from stack
return NULL; // actually never reached
}
int main(void){
pthread_t th;
pthread_create(&th, NULL, &test_thread, NULL);
pthread_cancel(th);
pthread_join(th, NULL);
return cancelled;
}
I was compiling the following code in c++, Visual Studio 2012 (Professional, Update 4)
class dum {
stringstream *ss;
~dum() {
delete ss;
}
public:
dum() : ss(NULL) {}
};
int main()
{
dum a;
return 0;
}
Now I know that the private destructor would force heap allocated objects only, but I would expect a compile error for that. Instead I get a window titled "Microsoft (R) C/C++ Optimizing compiler" saying
Microsoft (R) C/C++ Optimizing compiler has stopped working. Windows can check online for a solution to the problem
and then the usual prompts to go online where nothing happens (or gets resolved). Am I doing something wrong or have I stumbled upon a bug in the compiler?
EDIT
The code I'm posting is all that's present in a win32 console program (even the main() has this no arguments form) and the only header included is sstream.
If you move the destructor to the public section of the class we no longer have a crash, but as I mentioned above this should be a cause for compilation error (namelly cannot access private member declared in dum) and not for this pop-up. The question's targeted to people that can provide an intrinsic or two about what's the problem with the compiler here, I've seen similar problems before, but that's the smallest code segment that caused such a thing.
Trying to compile the fixed version:
#include <sstream>
using namespace std;
class dum {
stringstream *ss;
~dum() {
delete ss;
}
public:
dum() : ss(NULL) {}
};
int main()
{
dum a;
return 0;
}
gives following compile error for me.
Tried this with VS2012 Ultimate Version 11.061030.00 Update4.
1>------ Build started: Project: dum, Configuration: Debug Win32 ------
1> dum.cpp
1>c:\users\randmaniac\documents\visual studio 2012\projects\dum\dum\dum.cpp(19): error C2248: 'dum::~dum' : cannot access private member declared in class 'dum'
1> c:\users\randmaniac\documents\visual studio 2012\projects\dum\dum\dum.cpp(8) : see declaration of 'dum::~dum'
1> c:\users\randmaniac\documents\visual studio 2012\projects\dum\dum\dum.cpp(6) : see declaration of 'dum'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
No crash for me on a fairly recent installation of VS2012.
I found the following snippet of code and am trying to create an ISAPI .DLL using it.
#include <windows.h>
#include <httpfilt.h>
#include "tchar.h"
#include "strsafe.h"
// Portion of HttpOnly
DWORD WINAPI HttpFilterProc(
PHTTP_FILTER_CONTEXT pfc,
DWORD dwNotificationType,
LPVOID pvNotification) {
// Hard coded cookie length (2k bytes)
CHAR szCookie[2048];
DWORD cbCookieOriginal = sizeof(szCookie) / sizeof(szCookie[0]);
DWORD cbCookie = cbCookieOriginal;
HTTP_FILTER_SEND_RESPONSE *pResponse =
(HTTP_FILTER_SEND_RESPONSE*)pvNotification;
CHAR *szHeader = "Set-Cookie:";
CHAR *szHttpOnly = "; HttpOnly";
if (pResponse->GetHeader(pfc,szHeader,szCookie,&cbCookie)) {
if (SUCCEEDED(StringCchCat(szCookie,
cbCookieOriginal,
szHttpOnly))) {
if (!pResponse->SetHeader(pfc,
szHeader,
szCookie)) {
// Fail securely - send no cookie!
pResponse->SetHeader(pfc,szHeader,"");
}
} else {
pResponse->SetHeader(pfc,szHeader,"");
}
}
return SF_STATUS_REQ_NEXT_NOTIFICATION;
}
I created a new C++ project using VS 2010 Express. I get the following error when I build the project:
------ Build started: Project: ISAPIHttpOnly, Configuration: Debug Win32 ------
HttpOnly.cpp
c:\documents and settings\bob\my documents\visual studio 2010\projects\isapihttponly\isapihttponly\httponly.cpp(25): error C2664: 'StringCchCatW' : cannot convert parameter 1 from 'CHAR [2048]' to 'STRSAFE_LPWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I'm not sure how to resolve this. :S
Your program is being compiled as Unicode, then STRSAFE_LPWSTR, which is the type from the first parameter in StringCchCat, is failing against an char array type, which is not Unicode.
To solve this, you have two choices, one is to declare the erroneous string as an TCHAR array, so it can be preprocessed to an wchar_t array. But then you'd have to change a lot of things in your code, like literal trings would need the TEXT("") macro, etc.
But as it seems, your program was not done to use Unicode strings, so the other choice is to compile your program as Multi-Byte, then you don't need to change anything in your code because StringCchCat will have a STRSAFE_LPSTR parameter, which will be preprocessed to char *.
To compile as Multi-Byte, just go to Project Settings->General->Character Set.