Implementing the PT_DENY_ATTACH anti-piracy code - iphone

I've been tryign to implement the following anti piracy code from this wiki:
http://theiphonewiki.com/wiki/index.php?title=Bugging_Debuggers
But despite following it to the letter my app exits with a
Program exited with status value:45.
When i test it. If i comment out the function call disable_gdb(); the app runs as normal.
What is it that I'm doing wrong. Or is it that the code is doing as it should.. and exits while xcode is attached?
#import <UIKit/UIKit.h>
#import <dlfcn.h>
#import <sys/types.h>
typedef int (*ptrace_ptr_t)(int _request, pid_t _pid, caddr_t _addr, int _data);
#if !defined(PT_DENY_ATTACH)
#define PT_DENY_ATTACH 31
#endif // !defined(PT_DENY_ATTACH)
int main(int argc, char *argv[])
{
NSLog(#"Main Called ");
disable_gdb();
NSLog(#"After cracker code");
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
void disable_gdb()
{
void* handle = dlopen(0, RTLD_GLOBAL | RTLD_NOW);
ptrace_ptr_t ptrace_ptr = dlsym(handle, "ptrace");
ptrace_ptr(PT_DENY_ATTACH, 0, 0, 0);
dlclose(handle);
}
int main3(int argc, char *argv[])
{
return -1;
}
Kindest Regards,
-Code

The code is functioning as intended. That said, I should tell you that you're wasting your time. This approach will only work if gdb and the attacker/programmer are cooperative. The whole point of tools like gdb is that they are extremely versatile and if a simple "bug" like this stopped them dead in their tracks, someone would fix it very quickly. :)
As described on this page, you can just do the following from within gdb:
(gdb) break ptrace
commands 1
return
continue
end

Related

Xcode Instruments CoreGraphics Leak

Xcode Instruments is saying that I have a CGColor Leak.
Responsible Library: CoreGraphics
Responsible Frame: CGTypeCreateInstance
The stack is tracing it back to "Main" and the code below is highlighted as the error.
int main(int argc, char *argv[])
{
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([PDCAppDelegate class]));
}
}
Does anyone know how to fix this? Thank you all!
Likely, your PDCAppDelegate, or something it owns, is creating a CGColorRef and failing to release it.

How to determine whether code is running during a unit test run

I need to have some kind of if statement to ignore certain bits of code e.g. pop dialog if the coding is being run as part of a unit test.
Does anyone have any idea have to do this - similar to debug?
I prefer runtime solution, not a one based on preprocessor:
int main(int argc, char* argv[]) {
#autoreleasepool {
BOOL tests = NO;
for (int i = 0; i < argc; i++) {
NSString* argument = [NSString stringWithCString:argv[i] encoding:NSASCIIStringEncoding];
if ([argument isEqualToString:#"-SenTest"]) {
tests = YES;
break;
}
}
if (tests) {
//save YES to a global variable and use it whenewer you want
}
UIApplicationMain(...)
}
}
I am actually using this to have a different UIApplicationDelegate when unit tests are run, so no UI code (DB opening, notifications started etc) collides with my test cases.

XCode 4.2 : when app crashes, threads rarely display callstack

Since I've installed the last xCode (my previous one was the 3.xx), a have hard times to debug my crashing apps. Indeed, the callstack is often empty. And the displayed method is
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, #"MyAppDelegate");
[pool drain];
return retVal;
}
Ex :
Have anyone noticed this ? It was working perfectly on the same project with previous XCode. Is there any solution ?
Try setting Exception Breakpoint on Breakpoint panel:
Notice that you can change the number of items displayed in the stack frames view by dragging the slider at the bottom of the view.

Sigabrt on Main function

I've just began to learn iOS development and now I have a sigabrt on my main function. The error is triggered by calling:
int retVal = UIApplicationMain(argc, argv, nil, nil);
As I am very new to iOS programming I have no idea on what could by causing this error. One thing that I have found on my callstack is the following exception being raised: dyld_stub_objc_exception_throw
What could be causing this error?
I was having this problem in X-Code 4.2 and the issue was it couldn't find my Storyboard. The frustrating part was that there was no indication of what the actual exception was. I was able to get the exception in the log by adding a try / catch to my main function. (Note I'm using ARC, so if you aren't using ARC your main will look a little different with try catch)
int main(int argc, char *argv[])
{
int retVal = 0;
#autoreleasepool {
NSString *classString = NSStringFromClass([sortaAppDelegate class]);
#try {
retVal = UIApplicationMain(argc, argv, nil, classString);
}
#catch (NSException *exception) {
NSLog(#"Exception - %#",[exception description]);
exit(EXIT_FAILURE);
}
}
return retVal;
}
UIApplicationMain basically kicks off the rest of your application, so the actual cause of the crash could be anywhere in your application. You may need to look further down the call stack.
However, I note that you're passing nil as the final two parameters. The first nil is the principalClassName argument, which can legally be nil if you want UIApplication to be the principal class. The second nil is the application delegate name; you should pass nil if you load the delegate from the main nib. Do you? If you don't, that might be the problem (I can't say I've ever called this function with both of those arguments nil).
It was just a typical error of someone learning a new language/API. I forgot to properly set up the view.
UIView *controllersView = [myViewController view];
[window addSubview:controllersView];

How to catch sigpipe in iphone app?

How can i catch sigpipe in iphone/objective-c?
thanks
One important fact for testing the SigPipeHandler:
For me it did not work when the debugger was atached. So when running an app directly from XCode the Handler is not called.
Meanwhile, on a device without the debugger attached the handler works as expected.
Use old good POSIX code:
#include <signal.h>
void SigPipeHandler(int s);
void SigPipeHandler(int s)
{
// do your handling
}
Init in some place (main.m?) with
signal(SIGPIPE, SigPipeHandler);
Try setting SO_NOSIGPIPE as documented here:
How to prevent SIGPIPEs (or handle them properly)
The first answer doesn't work.
Also I'm trying to use solution described in reference of second post:
int main(int argc, char *argv[ ]) {
struct sigaction mySigAction;
mySigAction.sa_handler = SIG_IGN;
sigemptyset(&mySigAction.sa_mask);
sigaction(SIGPIPE, &mySigAction, NULL);
...
}
but this code doesn't work too. Anybody know solution of this problem?
The first answer works fine. but you should put every thing in the main.mm file.
And in static class(Singleton) , it also works.
#import <UIKit/UIKit.h>
#import <sys/signal.h>
#if TARGET_IPHONE_SIMULATOR
#endif
void SigPipeHandler(int s)
{
NSLog(#"We Got a Pipe Single :%d____________",s);
}
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
signal(SIGPIPE, SigPipeHandler);
int retVal = UIApplicationMain(argc, argv, nil, #"FullAppDelegate");
[pool release];
return retVal;
}