<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ali Pourhadi on Medium]]></title>
        <description><![CDATA[Stories by Ali Pourhadi on Medium]]></description>
        <link>https://medium.com/@ali.pourhadi?source=rss-2f2590e70cf2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*66jxgPVtFXznD4XI</url>
            <title>Stories by Ali Pourhadi on Medium</title>
            <link>https://medium.com/@ali.pourhadi?source=rss-2f2590e70cf2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 23:00:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ali.pourhadi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Swift Compiler: What we can learn]]></title>
            <link>https://medium.com/@ali.pourhadi/swift-compiler-what-we-can-learn-96872ea4b1b8?source=rss-2f2590e70cf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/96872ea4b1b8</guid>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[mobile]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <dc:creator><![CDATA[Ali Pourhadi]]></dc:creator>
            <pubDate>Mon, 04 Nov 2019 13:34:51 GMT</pubDate>
            <atom:updated>2019-11-04T13:36:03.569Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9B3bjtyJXyOq5utStW1IHA.png" /></figure><p>This article is about my journey in swift. My journey has begun with a simple question:<em> How method dispatch works in swift?</em></p><p>I was trying to understand what will go into V-Table &amp; what will go to Witness-Table. And there are many articles about method dispatch in swift and I am not gonna cover this subject. Instead, while I was trying to see what is the effect of each situation, I got into a different story which, I believe, could be very interesting to you too. Some will say why not reading swift documents and source code. I kind of agree, but that may take much more time for you, plus you don’t need to have C knowledge.</p><p>First of all, How swift compiler works:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3AR5KnzyqPuDfALG-6QSQQ.png" /><figcaption>Swift Compile ( before getting into LLVM )</figcaption></figure><ul><li>Your swift file will turn to AST ( Abstract Syntax Tree )</li><li>The generated tree will turn to SIL ( Swift Intermediate Language )</li><li>Your intermediate language will turn to IR ( Intermediate Representation )</li></ul><p>At this point, LLVM will take care of rest which generating assembly for the machine which is out of our scope. Also, there is an optimization that I did not include in the graph.</p><h4>SwiftC / Swift ?</h4><p>There are two different compiler tools that can use and they are almost identical. SwiftC compiles your code and will give you the result whereas Swift can also execute swift codes.</p><p>The beautiful part is that with SwiftC you can have a result of each step compile with using different options. If you see all the options that you have you can simply do :</p><p>&gt; swiftc -h</p><p>So, let&#39;s begin with the first step which is transforming our swift code into AST.</p><h4>What is AST?</h4><p>AST is a tree representation of the abstract syntactic structure of source code. Each node in this tree denotes a construct. If it’s not clear yet, don’t worry, we will see a sample soon. Basically you can have AST with three different option:</p><ul><li><strong>dump-ast</strong>: Parse and type-check input file(s) and dump AST(s)</li><li><strong>dump-parse</strong>: Parse input file(s) and dump AST(s)</li><li><strong>print-ast</strong>: Parse and type-check input file(s) and pretty print AST(s)</li></ul><p>I know when you do print it, it’s not like a real tree :)). But let’s have our fun. I have given these codes as an input :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5d88bc8c9b19ce7562e930b12de04acb/href">https://medium.com/media/5d88bc8c9b19ce7562e930b12de04acb/href</a></iframe><p>It’s just two classes with a function that passed through inheritance. Let’s generate the tree and see the generated codes:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*XL6nhc57bPFZVHFRfWAtJg.png" /></figure><p>If you just read the result you will learn a lot about what is happening behind the scene. I just mention some of the interesting things:</p><blockquote><em>“aFunction(param:)” interface type=’(AClass) -&gt; (Int) -&gt; ()’ access=internal<br>(parameter “self” interface type=’AClass’)<br>(parameter_list<br>(parameter “param” apiName=param interface type=’Int’)</em></blockquote><p>Every time that you create a function swift will pass a parameter which is “self” that’s why we have access to other functions.</p><p>I also used a <strong>#function</strong> to print the function’s name. And the result of the transformation was very interesting:</p><blockquote><strong>magic_identifier_literal_expr</strong> type=’String’ location</blockquote><p>If you do search for “magic_identifier_literal_expr” you will find it in “ParsePattern.cpp” in Swift GitHub repo, And you can see other options like line, file… And finally, the part that my journey has begun because of that: Inheritance</p><blockquote><strong>class_decl</strong> “BClass” interface type=’BClass.Type’ access=internal non-resilient inherits: AClass</blockquote><p>That’s cool, but it didn’t give the information that I was looking for. So then I decided to see the next level which is generating SIL. If you check the help there are two different methods of generating SIL:</p><ul><li><strong>-emit-silgen</strong>: Emit raw SIL file(s)</li><li><strong>-emit-sil</strong>: Emit canonical SIL file(s)</li></ul><p>Raw SIL and Canonical SIL are almost identical. Technically some optimization will be applied on raw SIL and Canonical SIL will be generated.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CixaYTfRZL7ea-BgPoFCjA.png" /><figcaption>Generated SIL codes</figcaption></figure><p>And finally, I found what I was looking for. I could see what exactly happening when I define a function as Final or what will happen in V-Table when I define add a function to class by extension. I highly recommend you try and see.</p><p>There is something interesting in SIL codes. They are comments in the code! which can be very useful to read. Beside Swift does <strong>name mangling</strong>. So if you want to read names you have to use Swift Demangler:</p><p><a href="https://github.com/apple/swift/blob/master/tools/swift-demangle/swift-demangle.cpp">apple/swift</a></p><p>There is also an online version that you can use:</p><p><a href="https://www.swiftdemangler.com/">Swift Demangler</a></p><p>At this point, I had got what I needed but just to satisfy my curiosity I tried to generate IR Codes. This time we have only one option to generate LLVM Intermediate Representation:</p><ul><li><strong>-emit-ir</strong>: Emit LLVM IR file(s)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*A3XdCWeOatCKdi7AR0f6iw.png" /><figcaption>Generated IR file</figcaption></figure><p>As you see since we getting closer to LLVM it becomes less human-readable. From this point, LLVM takes responsibility and in the end, you will have your assembly.</p><p>That was my journey to learn how things works, hope you also enjoyed it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96872ea4b1b8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[IPC — Mach Message]]></title>
            <link>https://medium.com/@ali.pourhadi/ipc-mach-message-cab64ff1b569?source=rss-2f2590e70cf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/cab64ff1b569</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[mobile-app-development]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[mobile]]></category>
            <dc:creator><![CDATA[Ali Pourhadi]]></dc:creator>
            <pubDate>Fri, 27 Sep 2019 12:11:33 GMT</pubDate>
            <atom:updated>2019-10-02T18:31:53.224Z</atom:updated>
            <content:encoded><![CDATA[<h3>IPC — Mach Message</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mQXITptpB0L_IuADrTCpzw.png" /></figure><p>If you like me want to have a deep understanding of things follow me in this tutorial coz I am gonna talk about low-level IPC in OSX/iOS.</p><p>I have published a <a href="https://medium.com/@ali.pourhadi/fun-with-xpc-153fd772d409">tutorial for XPC-IPC</a>. We succeeded to make a simple service and send a message from a client and get the expected response. Now we want to see what’s happening under the hood. What is happening when you send a request.</p><p>Before starting I have to say that XPC is not the way of IPC communication. We also have MIG/CFPort/DO ( it’s a bit old ). The mechanism of IPC call for all is almost the same. I just made CFPort sample for myself and it wasn’t that much complicated. If you do research you will find that the base of all of them is the same. They all are using Mach Messages in order to communicate.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*7jijXdFrvGWwYUGAmoHPsg.png" /></figure><p><strong>Why XNU?</strong><br>Perhaps you know that OSX and iOS are based on XNU. So technically Mach Messages will be created on XNU layer. Mach IPC is derived from the Mach microkernel.</p><p>There is some concept that knowing them will help you to continue understanding how Mach Message works: <em>Tasks, Threads, Port. </em>I recommend you to have a look into apple documents for <a href="https://developer.apple.com/library/archive/documentation/Darwin/Conceptual/KernelProgramming/Mach/Mach.html">Mach Overview</a>.</p><p><strong>How is a Mach Message looks like?</strong></p><p>To see Mach Message’s structure lets take a look into a document:</p><p><a href="https://opensource.apple.com/source/xnu/xnu-792/osfmk/mach/message.h.auto.html">message.h</a></p><p>There are three important parts that we have to take a look into them.</p><pre><strong>typedef</strong>	<strong>struct</strong> <br>{<br>  mach_msg_bits_t	msgh_bits;<br>  mach_msg_size_t	msgh_size;<br>  mach_port_t		msgh_remote_port;<br>  mach_port_t		msgh_local_port;<br>  mach_msg_size_t 	msgh_reserved;<br>  mach_msg_id_t		msgh_id;<br>} mach_msg_header_t;</pre><pre><strong>typedef</strong> <strong>struct</strong><br>{<br>        mach_msg_size_t msgh_descriptor_count;<br>} mach_msg_body_t;</pre><pre><strong>typedef</strong> <strong>struct</strong><br>{<br>        mach_msg_header_t       header;<br>        mach_msg_body_t         body;<br>} mach_msg_base_t;</pre><p>Let’s begin header:</p><ul><li><strong>msgh_bits</strong>: It’s a bitmap who specifies some properties of the message like the message is simple or complex ( will talk about it later )</li><li><strong>msgh_size</strong>: Size of the message which includes header and body</li><li><strong>msgh_remote_port</strong>: Port right of where we are going to send the message.</li><li><strong>msgh_local_port</strong>: Specifies an auxiliary port right, which is conventionally used as a reply port by the recipient of the message</li><li><strong>msgh_reserved -&gt; msgh_voucher_port </strong>: The msgh_reserved field has become msgh_voucher_port with the introduction of vouchers. Vouchers are used to pass arbitrary data in messages over key-value pairs</li><li><strong>msgh_id</strong>: 32-bit field message-id.</li></ul><p>In the message body, we see something about “descriptor”</p><pre><strong>typedef</strong> <strong>union</strong><br>{<br>  mach_msg_port_descriptor_t		port;<br>  mach_msg_ool_descriptor_t		out_of_line;<br>  mach_msg_ool_ports_descriptor_t	ool_ports;<br>  mach_msg_type_descriptor_t		type;<br>} mach_msg_descriptor_t;</pre><ul><li><strong>mach_msg_port_descriptor_t</strong> : Embedding a port into the message</li><li><strong>mach_msg_ool_descriptor_t: </strong>Attaching ool data to the message</li><li><strong>mach_msg_ool_ports_descriptor_t: </strong>Adding<strong> </strong>OOL ports array in a message</li><li><strong>mach_msg_type_descriptor_t: </strong>is gonna be of these types.</li></ul><pre>#<strong>define</strong> <strong>MACH_MSG_PORT_DESCRIPTOR</strong> 		0<br>#<strong>define</strong> <strong>MACH_MSG_OOL_DESCRIPTOR</strong>  		1<br>#<strong>define</strong> <strong>MACH_MSG_OOL_PORTS_DESCRIPTOR</strong> 		2<br>#<strong>define</strong> <strong>MACH_MSG_OOL_VOLATILE_DESCRIPTOR</strong>  	3</pre><p>Based on names and former descriptions we the names almost clear about what they are doing except <strong>MACH_MSG_OOL_VOLATILE_DESCRIPTOR </strong>which is used for Sending volatile data in a message. Could be very handy.</p><blockquote>If the message is complex, it contains <strong>count of type descriptors</strong> and the <strong>type descriptors</strong> themselves (mach_msg_descriptor_t). Then the size of the message must be specified in bytes, and includes the message header, descriptor count, descriptors,and inline data.</blockquote><p>now let’s make our hands dirty. I going to create a very simple application to test sending a very simple Mach Message. basically, it’s just a header without content.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1a2ab03b8694466600ccac9aaa347c85/href">https://medium.com/media/1a2ab03b8694466600ccac9aaa347c85/href</a></iframe><p>In our test app, at the beginning we create a port to establish communication, Then simply change message id for each message that we sending and listening to messages.</p><p>That’s it. it seems complicated at the start what when you jump into it’s not that hard. I tried to explain things as an iOS Dev point of view.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=cab64ff1b569" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fun with XPC]]></title>
            <link>https://medium.com/@ali.pourhadi/fun-with-xpc-153fd772d409?source=rss-2f2590e70cf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/153fd772d409</guid>
            <category><![CDATA[xpc]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <dc:creator><![CDATA[Ali Pourhadi]]></dc:creator>
            <pubDate>Fri, 06 Sep 2019 03:29:09 GMT</pubDate>
            <atom:updated>2019-09-06T03:29:09.889Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*TIG7788qpKI0sT8UHICRCQ.png" /></figure><p>For a long time I was curious to learn how IPC works on OSX. if you do search about it most of the results are related to how jailbreak? how to make an exploit? and so on. But i could not find a simple tutorial to learn how actually this things work? how I can make my own service? In this tutorial I am not going to talk about “launchd” or “Mach Messages” , “DO” , “Pasteboard”, …. We are just going to make simple service and see how it can be cool and useful to break your app to different services.</p><p>So what is XPC?</p><p><a href="https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html">https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingXPCServices.html</a></p><p>XPC is a way for interprocess communication ( IPC ). what that means? let’s say we break our app to one main application and couple of services. let’s say these services all <strong>always</strong> ( technically it’s does not happen ) running as separate apps. There are many ways to cominucate between them. XPC is one them.</p><p>We are going to make a very simple one to see how this thing works. Create a new Cocoa App ( I made it in Objective-C but there many samples on github base on Swift ) . Then add new target and select XPC.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/703/1*4lRDKocWIQxSBCg16CBasw.png" /></figure><p>While creating XPC target be careful about Bundle Identifier we will need it later. In my case it’s “<strong>pourhadi.ali.xpctest</strong>”.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*uFyLY9EsHgl3BdKOfzZmTQ.png" /></figure><p>So in my case these 5 files has been added into project. let’s begin with <em>main.m</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b5940d47060d67b2529d4c9ebffa7bcf/href">https://medium.com/media/b5940d47060d67b2529d4c9ebffa7bcf/href</a></iframe><p>basically you will find how your service will be initialized and listen to requests that comes and will answer to them. The important part of file is our <strong>ServiceDelegate </strong>which defines to which requests we have to listen and passing our service object to. <strong>exportedInterface </strong>says<strong> </strong>through which kind of interface others can talk to me in our example is gonna be “<strong>xpctestProtocol</strong>”. That make sense. your app and your service should talk in a same language and then what we passing as exported object is the object that is going to handle our request. In our example it’s <strong>xpctest </strong>whom implementing xpctestProtocol.</p><p>let’s take a look into xpctest.m file</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6dcb757b077df249ace935c0074e4c03/href">https://medium.com/media/6dcb757b077df249ace935c0074e4c03/href</a></iframe><p>it’s just a simple method whom turn a string to uppercase string and return it inside of a block.</p><p>Now, our service is ready to use. we have to make a connection from our host app to our service app.</p><p>It’s just a sample, so please don&#39;t judge me for putting eveything in viewcontroller :)).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ff92f3389fc148dab3a0084650d50596/href">https://medium.com/media/ff92f3389fc148dab3a0084650d50596/href</a></iframe><p>Basically we need a connection to our server. This is the point that we need our bundle id. It’s gonna be our service name. Exported object in service is gonna be our remote object here. We need to know through which interface we can talk to our remote object. We can also track if our connection interrupted or did not happen. You can define your call backs. So at this point I guess it’s clear that the connection between our app and service is not always there.</p><p>There you go. run you app and pass a string and get the uppercased one. this is a very simple easy XPC connection.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=153fd772d409" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS App Security — 5]]></title>
            <link>https://medium.com/@ali.pourhadi/before-starting-ios-app-reverse-engineering-i-started-to-learn-how-apple-protect-applications-91343c79e26b?source=rss-2f2590e70cf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/91343c79e26b</guid>
            <category><![CDATA[jailbreak]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Ali Pourhadi]]></dc:creator>
            <pubDate>Wed, 24 Oct 2018 15:16:32 GMT</pubDate>
            <atom:updated>2018-10-24T15:23:18.811Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/712/1*U_OysDug6Hpe5X5yjaGIAQ.png" /></figure><p>Before starting iOS App reverse engineering I started to learn how Apple protect applications. From my understanding when you purchase an application the binary will be encrypted by your private key plus Apple will inject a special byte into header. At install time, iOS tries to decrypt the header with your key, which will succeed if the app was downloaded from the App Store with matching credentials.</p><p>So in order to taking look into binary file we have to decrypt it first. I found a tool which is <strong>Clutch.</strong></p><p><a href="https://github.com/KJCracks/Clutch">https://github.com/KJCracks/Clutch</a></p><p>basicly what it does is dumping memory in runtime to have a decrypted version of App. it does not mean that after that you can run app on your phone! because it’s still need to be singed by your private key, anyway with dont need it.</p><p>When you have your Mach-O binary file you can start doing reverse enginnering. There are couple of tools in order to see the flow of application. When I was student I was using IDA which is very well-known reverse engineering tool and the expensive one. how ever it has a free version that you can use it and has enough features for our job.</p><p><a href="https://www.hex-rays.com/product%E2%80%A6">https://www.hex-rays.com</a></p><p>But I personally prefer to use Hooper which has Personal License with cost of 133 CAD. Also you can download limited demo version for free</p><p><a href="https://www.hopperapp.com/">Hopper</a></p><p>Ok. lets take a look into hooper to see how we have to use it.</p><p>As target I took calculator app from my phone and open it into hooper to see what is happening inside. lets take a look into application flow.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1a8bvsGd25-11gfwAGXbFw.png" /></figure><p>You can select Proc to see list of all functions and subroutines. On the menu at the top select CFG Mode which gives a graph of calling function also you can select Pseudo code Mode to have Pseduo code of functions which make thing easier to read.</p><p>If you are looking for information from classes and properties or iVars you can search for <strong><em>_metaclass.</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4uEa3RvIrC4GPH6jjlHtcA.png" /></figure><p>We will see how we can use these kind of informations later.</p><p>But since in this article i jumped in to tool lets take a look into class_dump. what class_dump does : It generates declarations for the classes, categories and protocols. At the time that it developed it was for Objective-C runtime information.</p><p><a href="http://stevenygard.com/projects/class-dump/">http://stevenygard.com/projects/class-dump/</a></p><p>but there is a fork of repo with supporting swift which is interesting to take a look</p><p><a href="https://github.com/0xced/class-dump/tree/swift-binaries">0xced/class-dump</a></p><p>As a sample application i used YahooWeather which i believe is one the most beautiful apps.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*XZorWzzK-oOGyLJ6_5f1RA.png" /></figure><p>As you can see it’s based on obj-c and you see all headers. That would be enough to manipulating an App. We will continue our reverse-engineering with using these tools.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=91343c79e26b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS App Security — 4]]></title>
            <link>https://medium.com/@ali.pourhadi/ios-app-security-4-aaa88a850d07?source=rss-2f2590e70cf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/aaa88a850d07</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[objective-c]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[jailbreak]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Ali Pourhadi]]></dc:creator>
            <pubDate>Wed, 17 Oct 2018 00:49:38 GMT</pubDate>
            <atom:updated>2018-10-17T13:57:21.578Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S-e_q6Q6SY9V-knl5xpmAg.jpeg" /></figure><h3>iOS App Security — 4</h3><p>This part has to be the most interesting part. We are going through <strong>Cycript</strong>. what is Cycript ?</p><blockquote>Cycript allows developers to explore and <strong>modify running applications</strong> on either iOS or Mac OS X using a hybrid of Objective-C++ and JavaScript syntax through an interactive console that features syntax highlighting and tab completion.</blockquote><p>woho! modifying running applications!! yeah that’s true. In order to play with it finally i found an old device ( iPohne 5c — iOS 10.3.3 )in our company to jailbreak it and prepare it for security test. Since doing jailbreak is different for each iOS or each device ( 32bit / 64bit ) I am not gonna cover that part. you will find lots of tutorial regarding that. If you are familiar with Cydia you can easily find the Cycript page in it’s default sources.</p><p>if you want to connect to your phone you have to install <strong>openSSH</strong> and <strong>openSSL </strong>which are in default source of Cydia.</p><p>So I simply start to connecting to my device with</p><blockquote>ssh root@youripaddress</blockquote><p>Default password for root is “<strong>alpine</strong>” unless you changed it. Now we have to open our target application. I have few applications on my phone ( you can also target embedded applications ). Let the App be open on the phone. In order to hook to app we have to find it’s process. so lets check all running processes. in your terminal run :</p><blockquote>ps aux</blockquote><p>it will list all processes. if you know what are looking for you use grep to filter them, like for me since my target app is starbuks I use it like this :</p><blockquote>ps aux | grep “Starbucks”</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eF7bVo_B4gz0QFlfPp-YcQ.png" /></figure><p>now lets hook into app :</p><blockquote>cycript -p processNumber ( like 405 for me )</blockquote><p>let begin our fun. Now you can use Objc + JScripts commands. I will try to print all subviews of keywindow. You can check Cycript website to read <a href="http://www.cycript.org/manual/">manual</a>.</p><blockquote>[[[UIWindow keyWindow] rootViewController] _printHierarchy]</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1cSMc-hbj28yspdZ79vHPw.png" /></figure><p>so just for fun i want to add badge to gift tabbar item. in the list i find tabbar. in order to keep the rerence of tabbarcontroller i define a variable</p><blockquote>var tabbarController = new Instance<strong>(</strong>0x15fc1c00)</blockquote><p>now i try to find the tabbar</p><blockquote>[[tabbarController view]subviews]</blockquote><blockquote>var tabbar = new Instance<strong>(</strong>0x1602f980<strong>)</strong></blockquote><blockquote>var gift = tabbar.items[3]</blockquote><blockquote>gift.badgeValue = @”10&quot;</blockquote><figure><img alt="" src="https://cdn-images-1.medium.com/max/320/1*uM54mxzR7iwqIWRYM8gJ9A.png" /></figure><p>It’s just the beginning. We will have more fun with Learing Class-Dump and Using Reverse Engineering tools.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=aaa88a850d07" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS App Security — 3]]></title>
            <link>https://medium.com/@ali.pourhadi/ios-app-security-3-89683a1d8d35?source=rss-2f2590e70cf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/89683a1d8d35</guid>
            <category><![CDATA[jailbreak]]></category>
            <category><![CDATA[swift]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Ali Pourhadi]]></dc:creator>
            <pubDate>Tue, 16 Oct 2018 15:56:41 GMT</pubDate>
            <atom:updated>2018-10-16T15:56:41.345Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S-e_q6Q6SY9V-knl5xpmAg.jpeg" /></figure><h3>iOS App Security — 3</h3><p>I was continuing my investigation for iOS app Security and next thing that i found interesting was tracking requests and responses from an application. I knew that with setting up a proxy you can do it easily but I have never tried it by myself. I searched a bit to find the best existing solution and obviously free one.</p><p>At the end I found <a href="https://portswigger.net/burp">Burp</a>. It has 3 different version but my penetration, community version was enough. let’s look how to prepare it for our test</p><p>open Burp and then select <strong>Temporary project</strong> and go to next step. <strong>select Use Burp defaults. </strong>now<strong> </strong>are in main screen. In order to setup our proxy in top menu select <strong>Proxy </strong>then select <strong>Options </strong>in<strong> </strong>submenu. now select your localhome and your ip as listener in <strong>proxy listeners</strong>. I am using port 8888 but can have different port.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*R54uP-LOQDsWqBJlE68zjg.png" /></figure><p>now open Intercept tab in submenu and enable intercept. why that? normally if you just want to listen to http requests you dont have to do that. but since I want to have all kind of comminutions. somehow we have to do Man-In-Middle hack. ok. we are ready now. Next step is preparing our iPhone. open your wifi setting and add <strong>HTTP PRoxy. </strong>This has to be your listener ip and port which we have setup in Burp. Lets do final step which is adding a certificate to iPhone. You can follow instruction that is provided in portswigger website:</p><p><a href="https://support.portswigger.net/customer/en/portal/articles/1841109-installing-burp-s-ca-certificate-in-an-ios-device">Installing Burp&#39;s CA Certificate in...</a></p><p>now, lets have some fun. I tried to check google map to see all it’s traffic.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*aTcEdBp1TPXhpAbJnhq9gg.png" /></figure><p>so next time that you are designing your REST api think about that maybe some one could take information from it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=89683a1d8d35" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS App Security — 2]]></title>
            <link>https://medium.com/@ali.pourhadi/ios-app-security-2-6a418353d5ba?source=rss-2f2590e70cf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/6a418353d5ba</guid>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[security]]></category>
            <category><![CDATA[jailbreak]]></category>
            <dc:creator><![CDATA[Ali Pourhadi]]></dc:creator>
            <pubDate>Tue, 09 Oct 2018 00:49:31 GMT</pubDate>
            <atom:updated>2018-10-16T15:59:38.272Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S-e_q6Q6SY9V-knl5xpmAg.jpeg" /></figure><h3>iOS App Security — 2</h3><p>Next big thing for me as a developer is Data. Where we are saving our data ? Who can read them ? They are encrypted ?</p><p>I decided to check Apple’s documents to understand which kind of security they are using for files. So if you are curious like me check <a href="https://www.apple.com/business/site/docs/iOS_Security_Guide.pdf">Apple Security Guide</a>. In <strong>File Data Protection </strong>you will learn about how they manage to encrypt all files then decrypt them when they are needed. This is soooooo cooool. So is it possible to have use it’s feature in our application? well yeah, why not! you probably have seen Capabilities tab in project settings. if you look it carefully you will data protection section.</p><p>So let’s check <a href="https://developer.apple.com/documentation/uikit/core_app/protecting_the_user_s_privacy/encrypting_your_app_s_files">Apple’s document</a> again :</p><blockquote>Data protection is an iOS feature that you use to secure your app’s files and prevent unauthorized access to them. Data protection is enabled automatically when the user sets an active passcode for the device. You read and write your files normally, but the system encrypts and decrypts your content behind the scenes. The encryption and decryption processes are automatic and hardware accelerated.</blockquote><p>This is verrrryyyyy useful and interesting. so you will have four kind of options for protection level. But if you read it carefully it says files will be decrypted after unlocking the phone. it mean this awesome technology save our data from thieves but probably not all hackers. If I Jailbreak my phone i could have access to files and everything when <strong><em>device is unlocked</em></strong>.</p><p>The only possible solution that i found is encrypting my data by my own to avoid hackers to have access to data. If you are using CoreData for saving data it’s not that easy to handle everything. but i found a library which can help you alot</p><p><a href="https://github.com/project-imas/encrypted-core-data">project-imas/encrypted-core-data</a></p><p>but if you are using Realm like us. it can be way easier for you</p><p><a href="https://academy.realm.io/posts/tim-oliver-realm-cocoa-tutorial-on-encryption-with-realm/">https://academy.realm.io/posts/tim-oliver-realm-cocoa-tutorial-on-encryption-with-realm/</a></p><p>But it’s not all. Normally in our applications we are using <strong><em>Userdefault</em></strong> to save and retrieve data. Userdefault is basically a plist file in your application folder under /Library/Preferences/. That mean in jailbreaked phone i could have access to it and read it very easily. So please <strong>dont use it as a database</strong>!</p><p>Lets continue with <strong>Keychain</strong>. I can i imaging that you are saying hell no. this is secure for sure. ok lets see what is keychain first. base on <a href="https://developer.apple.com/documentation/security/keychain_services">apple’s document</a> :</p><blockquote>Securely store small chunks of data on behalf of the user.</blockquote><p>First of all. it does not use as a Database. The perfect way of using keychain is saving your database encryption key in it. I was doing research to find if there is a way to get all data from Keychain and it end up to<strong> </strong><a href="https://github.com/ptoomey3/Keychain-Dumper"><strong>Keychain-Dumper</strong></a><strong>.</strong></p><p>its a tool that make a dump from keychain. so lets see how it works on my jailbreaked phone :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GxzNJWMhc-ew8R1IyGzUUA.png" /></figure><p>As you can see i dont have that much application on my iPhone. The interesting part is that still all keychain data are encrypted but are they breakable ? Base on my understanding they are encrypted by passcode. Maybe that’s why having 4digits passcode is not a good idea. but as you can see you have all info about connected wifi. In case that you forgot a wifi pass that may help you :P</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6a418353d5ba" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[iOS App Security — 1]]></title>
            <link>https://medium.com/@ali.pourhadi/ios-app-security-1-b39336f8964a?source=rss-2f2590e70cf2------2</link>
            <guid isPermaLink="false">https://medium.com/p/b39336f8964a</guid>
            <category><![CDATA[ios-app-development]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Ali Pourhadi]]></dc:creator>
            <pubDate>Fri, 05 Oct 2018 21:17:42 GMT</pubDate>
            <atom:updated>2018-10-16T16:00:08.322Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*S-e_q6Q6SY9V-knl5xpmAg.jpeg" /></figure><h3>iOS App Security — 1</h3><p>After doing more research about security for iOS apps. I decided to dig deeply into each thing that may affect an iOS app security. So I started with Snapshot. that was very surprising coz before I did not how it works.</p><p>referring to <a href="https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html">Apple’s Document</a> :</p><blockquote><em>Remove sensitive information from views before moving to the background. When an app transitions to the background, the system takes a snapshot of the app’s main window, which it then presents briefly when transitioning your app back to the foreground. Before returning from your </em><a href="https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622997-applicationdidenterbackground"><em>applicationDidEnterBackground:</em></a><em> method, you should hide or obscure passwords and other sensitive personal information that might be captured as part of the snapshot.</em></blockquote><p>So, basically what is happening is that when you tap on homebutton ( if you have iPhoneX good for you :P ) iOS take an snapshot for multitasking preview. but do you really need to have a jailbreaked device in order to test it. Nah. I really recommend you to install Watchdog:</p><p><a href="https://pypi.org/project/watchdog/">watchdog</a></p><p>it logs every disk events like creating a file or folder, deleting , …</p><p>Where to watch? on my Xcode which is Xcode 9. simulator folder is /Library/Developer/CoreSimulator. could be different on older versions. if you are thinking is it that much important, I have to say depend on your application Yes. Imaging your in shopping cart and you have entered your card information then you put your app in background to check your account before buying if an application take all snapshots regularly… .</p><p>So how you can avoid it. base on <a href="https://developer.apple.com/enterprise/documentation/Configuration-Profile-Reference.pdf">Apple Configuration Profile Reference </a>: you can set allowScreenShot to false to not letting iOS take a snapshot</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b39336f8964a" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>